// // Example 11.6. This program reads sentences and displays // the number of words. It also splits sentences before each // verb and later splits before each connector. The verb or // connector must be in a list. A sentence must end in a // period. Any punctuation immediately follows a word. // #include "sentence.h" int main(void) { void directions(void); // prototypes boolean_t ProcessMore(void); void DisplaySplitWords(const char [][MAX_LENGTH + 1], int); const char verbs[][MAX_LENGTH + 1] = // list of verbs { "eat", "eats", "ate", "run", "runs", "ran", "sleep", "sleeps", "slept", "laugh", "laughs", "laughed", "am", "are", "is", "was", "were" }; const char connectors[][MAX_LENGTH + 1] = // list of connectors { "and", "but", "or" }; int NumVerbs = sizeof(verbs)/(MAX_LENGTH + 1), NumConnectors = sizeof(connectors)/(MAX_LENGTH + 1); directions(); SentenceAnalyzer sent; // sentence analyzer object do { sent.ReadSentence(); cout << "Number of words in sentence = " << sent.NumSentenceWords() << "\n\n"; cout << "List of verbs:\n"; DisplaySplitWords(verbs, NumVerbs); cout << "\nSentence split before verbs:\n"; sent.SplitSentence(verbs, NumVerbs); cout << "List of connectors:\n"; DisplaySplitWords(connectors, NumConnectors); cout << "\nSentence split before connectors:\n"; sent.SplitSentence(connectors, NumConnectors); } while (ProcessMore()); return 0; } // // Function to print program directions // Pre: none // Post: Directions were displayed. // void directions(void) { cout << "This program reads sentences and displays the\n"; cout << "number of words, the sentence split at each verb,\n"; cout << "and the sentence split at each connector.\n"; cout << "A sentence must end in a period.\n"; cout << "Any punctuation immediately follows a word.\n"; } // // Function to determine if the user wishes to process more // sentences and to return an answer, TRUE or FALSE // Pre: none // Post: TRUE was returned if the user wishes to process // more, FALSE otherwise. // boolean_t ProcessMore(void) { char answer[MAX_LINE]; // continue? do { cout << "Would you like to process another sentence? (y/n): "; cin.getline(answer, MAX_LINE); } while (answer[0] != 'y' && answer[0] != 'Y' && answer[0] != 'n' && answer[0] != 'N'); return (tolower(answer[0]) == 'y'); } // // Function to print an array of strings left-justified, 5 across // Pre: words is an array of strings. // NumSplitWords is the number of strings in words. // Post: The strings in words have been printed. // void DisplaySplitWords(const char words[][MAX_LENGTH + 1], int NumSplitWords) { cout.setf(ios::left, ios::adjustfield); for(int i = 0; i < NumSplitWords; i++) { cout << setw(12) << words[i]; if ((i % 5 == 4) || (i == NumSplitWords - 1)) cout << endl; } }