// FILE: 00a3.cpp // PURPOSE: 2000 A Exam: Question 3 //--------------------------------------------------------------------- // COMPILATION INSTRUCTIONS: g++ and gxx // 1. FILES: apvector.h, apvector.cpp must be in folder // or directory in which you compile // 2. COMMAND LINE: // g++ (or gxx) 00a3.cpp //-------------------------------------------------------------------- // PROGRAMMING INSTRUCTIONS: // Print out 2000 A Exam Question 3 and implement your solutions // to part a, part b, and part c as specified //---------------------------------------------------------------- #include #include #include "apvector.h" class WordCollection { public: int Size() const; void Insert(const string &word); void Remove(const string &word); string FindKth(int k) const; void Print(void) const; WordCollection(void) : mySize(0) {} private: apvector myWords; int mySize; }; //=================================================== // Part a. Complete Occurrences(), as started below. //=================================================== int Occurrences(const WordCollection &C, const string &word) { return -1; } //======================================================== // Part b. Complete RemoveDuplicates(), as started below. //======================================================== void RemoveDuplicates(WordCollection &C, const string &word) { } //======================================================== // Part c. Complete MostCommon(), as started below. //======================================================== string MostCommon (WordCollection &C) { return "**##!!"; } void testOccurrences(const WordCollection &); void testRemoveDuplicates(const WordCollection &); void testMostCommon(const WordCollection &); void initialize(WordCollection &); int main() { string title("2000 A Question 3"); cout << "\n\n\n\n\n\n" << setw(39 + title.length()/2) << title << endl << endl; WordCollection X; initialize(X); testOccurrences(X); testRemoveDuplicates(X); testMostCommon(X); return 0; } int WordCollection::Size() const { return mySize; } void WordCollection::Insert(const string &word) { mySize++; myWords.resize(mySize); myWords[mySize-1] = word; } void WordCollection::Remove(const string &word) { const int SIZE = mySize; bool found(false); int k(0); for (; k < SIZE && !found; k++) { if ( myWords[k] == word ) { found = true; break; } } if ( !found ) return; for (; k < SIZE - 1; k++) myWords[k] = myWords[k+1]; mySize--; } string WordCollection::FindKth(int k) const { if ( k < 1 || k > Size() ) { cerr << "ERROR: FindKth() index must be: 1 <= k <= Size()\n"; abort(); } k--; apvector myWords2 = myWords; const int SIZE = mySize; for (int pos = 0; pos < SIZE - 1; pos++) { int min = pos; for (int k = pos + 1; k < SIZE; k++) if ( myWords2[k] < myWords2[min] ) min = k; string tmp = myWords2[min]; myWords2[min] = myWords2[pos]; myWords2[pos] = tmp; } return myWords2[k]; } void WordCollection::Print(void) const { for (int k = 0; k < mySize; k++) cout << k+1 << ". " << myWords[k] << ( (k < mySize - 1) ? " " : "\n" ); } void initialize(WordCollection &C) { C.Insert("at"); C.Insert("bad"); C.Insert("all"); C.Insert("at"); C.Insert("bun"); C.Insert("bad"); C.Insert("at"); } void testOccurrences(const WordCollection &C) { string A[] = {"at", "bad", "all", "bun"}; const int SIZE = sizeof(A)/sizeof(string); cout << "Part a. Testing Occurrences():\n"; cout << "Current WordCollection: "; C.Print(); for (int k = 0; k < SIZE; k++) cout << "Occurrences(C, \"" << A[k] << "\") = " << Occurrences(C, A[k]) << endl; cout << endl; } void testRemoveDuplicates(const WordCollection &Y) { WordCollection C = Y; cout << "Part b. Testing RemoveDuplicates():\n"; cout << "Current WordCollection: "; C.Print(); RemoveDuplicates(C, "at"); cout << "After RemoveDuplicates(C, \"" << "at" << "\"):\n"; C.Print(); RemoveDuplicates(C, "all"); cout << "After RemoveDuplicates(C, \"" << "all" << "\"):\n"; C.Print(); cout << endl; } void testMostCommon(const WordCollection &Y) { WordCollection C = Y; cout << "Part c. Testing MostCommon():\n"; cout << "Current WordCollection: "; C.Print(); cout << "MostCommon(C) == " << MostCommon(C) << endl; C.Remove("at"); C.Remove("at"); cout << "Current WordCollection: "; C.Print(); cout << "MostCommon(C) == " << MostCommon(C) << endl; cout << endl; }