1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
void terminateProgram()
{
cout << "Press any character and <Enter> to continue"<< endl;
char chAnyChar;
cin >> chAnyChar;
return;
}
int main()
{
// Create a string with the objetive sentence
char obj[31];
cout << "Type a Short sentences: (no more than 30 characteres)\n" << "******************************" << endl;
cin.get(obj, 31);
// Show the objetive sentences
cout << "\nYour objetive sentence is:\n" << obj << endl;
cout << endl;
//Generate a random sentence using letters and space
//Letters and space that will be used
static char cLetters [30] = {' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\0'};
int iRandNumber;
// Use the computer time as a seed for the numbers
srand((unsigned)time(NULL));
cout << "Your initial random sentence is:\n" ;
static char NewS [31];
//Use the letters of the array insted of the random number
for (int i = 0; i <30; i++)
{
iRandNumber = rand() % 27;
NewS[i] = cLetters[iRandNumber];
cout << NewS[i];
}
cout << "\n" << endl;
do
{
static char New2S [31];
cout << "Your mutation sentence is: \n" ;
for (int i=0; i < 30 ; i++)
{
New2S [i] = NewS [i];
}
int randpos = rand() % 30;
New2S[randpos] = cLetters[iRandNumber];
for (int i=0; i < 30 ; i++)
{
cout << New2S[i];
}
cout << endl;
int iDifOS = 0;
for (int i=0; i < 30 ; i++)
{
if (obj[i]!= NewS[i])
iDifOS += 1;
}
int iSimOS = 30 - iDifOS;
cout << endl;
cout << "the similarity between the objetive sentence and the first random is:" <<iSimOS << endl;
int iDifO2S = 0;
for (int i=0; i < 30 ; i++)
{
if (obj[i]!= New2S[i])
iDifO2S += 1;
}
int iSimO2S = 30 - iDifO2S;
cout << endl;
cout << "the similarity between the objetive sentences and the mutation sentence is:" <<iSimO2S << endl;
for (int i=0; i < 30 ; i++)
{
if (iSimO2S > iSimOS)
{
NewS[i] = New2S[i];
}
}
cout << endl;
cout << "The mutation sentence that will stay as Random Parental is:";
cout << endl;
for (int i=0; i < 30 ; i++)
{
cout << NewS[i];
}
cout << endl;
while (iSimOS < 30);
}
terminateProgram();
return 0;
}
|