#include #include #include #include #include "MyUtil.h" #include "WordT.h" #include "ActionT.h" using namespace std; const size_t MAX_WORDS{5000}; string GetFileName(); void FixWord(string & word); void InsertWord(string word, WordT words[], size_t & wordCount); size_t FindWord(WordT words[], size_t wordCount, string key) { void ReadWords( WordT words[], size_t & wordCount); void PrintWords(const WordT words[], size_t wordCount); ActionT GetUserAction(); void PerformUserAction(ActionT action, bool & done); int main() { bool done{false}; ActionT action{ActionT::QUIT}; WordT words[MAX_WORDS]; size_t wordCount{0}; ReadWords(words, wordCount); while (not done) { PrintWords(words, wordCount); action = GetUserAction(); PerformUserAction(action, done); } return 0; } void PrintWords(const WordT words[], size_t wordCount){ size_t i; for(i = 0; i < wordCount; i++) { cout << setw(20) << words[i].GetWord() << setw(10) << words[i].GetCount() << endl; } cout << endl; } string GetFileName() { string name; cout << "Enter the name of a file to process: " ; cin >> name; cout << endl; return name; } void FixWord(string & word) { word = Strip(word); word = MakeLower(word); return; } size_t FindWord(WordT words[], size_t wordCount, string key) { size_t i; i = 0; while (i < wordCount and words[i].GetWord() != key) { i++; } return i; } void InsertWord(string word, WordT words[], size_t & wordCount) { size_t pos; FixWord(word); if (word != "") { pos = FindWord(words, wordCount, word); if (pos < wordCount) { words[pos].IncrementCount(); } else if (wordCount < MAX_WORDS) { words[wordCount].SetWord(word); wordCount++; } else { cout << "The array is full, can not insert another word" << endl; } } return; } void ReadWords( WordT words[], size_t & wordCount){ string fileName; ifstream inFile; string word; fileName = GetFileName(); inFile.open(fileName); if (inFile) { wordCount = 0; inFile >> word; while (inFile) { InsertWord(word, words, wordCount); inFile >> word; } inFile.close(); } return; } ActionT GetUserAction(){ cout << "Getting the user action" << endl; return ActionT::QUIT; } void PerformUserAction(ActionT action, bool & done) { cout << "Performing the user action" << endl; done = action == ActionT::QUIT; return; }