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
|
// Example program
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int main()
{
char response;
while (true) {
short unsigned int amount[5] = { 0 };
//Get a dictionary of the words that will be used, and thus will be checked for relation
//Made them all hold 4 elements for simplicity reasons, down below
const string article[4] = { "A", "THE", "", "" };
const string adjective[4] = { "LARGE", "BROWN", "RED", "BLUE" };
const string noun[4] = { "FOX", "BRIDGE", "DOG", "FIELD" };
const string preposition[4] = { "NEAR", "ACROSS", "", ""};
const string verb[4] = { "RUN", "", "", "" };
string result = "";
string check = "";
string sentence;
cout << "Enter a sentence ";
getline(cin, sentence); //Take in whole sentence with spaces
while(true) {
for(unsigned int index = 0; index < sentence.size(); index++) {
//Checks if it is not equal to a blank space in order to continue the same word.
if(sentence[index] != ' ') {
sentence[index] = toupper(sentence[index]);
check += sentence[index];
//This is where setting all the arrays above to 4 come in handy.
for(unsigned int argCycle = 0; argCycle < 4; argCycle++) {
if (amount[0] < 2 && check == article[argCycle]) {
if(check == "A" && sentence[index + 1] == ' ') {
amount[0]++;
result += 'A';
}
else if(check == "THE") {
amount[0]++;
result += 'A';
}
}
else if(amount[1] < 2 && check == adjective[argCycle]) {
amount[1]++;
result += 'a';
}
else if (amount[2] < 2 && check == noun[argCycle]) {
amount[2]++;
result += 'n';
}
else if (amount[3] < 1 && check == verb[argCycle]) {
amount[3]++;
result += 'v';
}
else if (amount[4] < 1 && check == preposition[argCycle]) {
amount[4]++;
result += 'p';
}
}
}
else {
//Reset the word if it a blank char appears, because it means another word is next
check = "";
}
}
break;
}
if (result == "AanvpAan") {
cout << "\nA valid sentence\n";
}
else {
cout << "\nNot a valid sentence\n";
}
}
cin >> response;
return 0;
}
|