#include #include #include #include #include "MyUtil.h" #include "ActionT.h" #include "WordT.h" using namespace std; const size_t MAX_WORDS{5000}; size_t Find(const WordT ary[], size_t count, string key); void Swap(WordT & a, WordT & b); void Sort(WordT ary[], size_t count); string GetFileName(); string FixWord(string word); void PrintArray(const WordT ary[], size_t count); void ReadWords( WordT wordAry[], size_t & size); 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); Sort(words, wordCount); PrintArray(words, wordCount); while (not done) { action = GetUserAction(); PerformUserAction(action, done); } return 0; } void Swap(WordT & a, WordT & b) { WordT tmp; tmp = a; a = b; b = tmp; return; } void Sort(WordT ary[], size_t count){ size_t current, smallest, search; for (current = 0; current < count-1; current ++) { smallest = current; for(search= current +1 ; search < count; search ++) { if (ary[smallest].Word() > ary[search].Word()) { smallest = search; } } if (smallest != current){ Swap(ary[smallest], ary[current]); } } } size_t Find(const WordT ary[], size_t count, string key){ size_t current; // size_t rv{MAX_WORDS}; // for(current = 0; current < count; current++) { // if (ary[current].Word() == key) { // rv = current; // } // } // // && & and // current = 0; while (current < count and ary[current].Word() != key) { current++; } return current; } void PrintArray(const WordT ary[], size_t count){ size_t i; for(i =0; i < count; i++) { cout << setw(20) << ary[i].GetWord() << setw(4) << ary[i].GetCount() << endl; } return; } string GetFileName() { string name; cout << "Enter the name of the input file: "; cin >> name; cout << endl; return name; } string FixWord(string word) { string fixedWord; fixedWord = Strip(word); fixedWord = MakeLower(fixedWord); return fixedWord; } void AddWord(WordT wordAry[], size_t & size, string newWord) { size_t pos; newWord = FixWord(newWord); if (newWord != "") { pos = Find(wordAry, size, newWord); if (pos < size ) { wordAry[pos].IncrementCount(); } else if (size < MAX_WORDS) { wordAry[size].SetWord(newWord); size++; } else { cout << "No space in the array for " << newWord << endl; } } return; } void ReadWords( WordT wordAry[], size_t & size){ string fileName; ifstream inFile; string word; fileName = GetFileName(); inFile.open(fileName); if (inFile) { size = 0; inFile >> word; while (inFile) { AddWord(wordAry, size, word); 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; }