/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Filename : p2.cpp * * Author : Owen Fowles Updated by Dr. Spiegel * * Course : CSc 136 * * Assignment : Project #2 * * * * Description : Creation and Implementation of an Object Type. * * Alphabetically organize the first N unique words * * in a file, taking note of their multiplicities. * * The test application will then provide access to * * specific commands. * * * * File : Test Driver of the WordRec class. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include #include #include #include #include "WordRec.h" using namespace std; // Global array limit const int MAX_WORDS = 25; // Prototypes /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Description : Opens a file specified by the user for reading purposes. * * * * Parameter(s) : &tokenFile -> (IMPORT/EXPORT) A file stream passed by * * reference. * * : &fileName -> () The user defined name of token file * * * * Return(s) : true -> If true is returned, then the file is able to * * be read and processed. * * : false -> If false is returned, the user specified file * * was unable to be opened. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ bool openFile(ifstream &tokenFile, string &fileName); /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Description : Reads data from a file stream and fills wordList until full * * * * Parameter(s) : &tokenFile -> (IMPORT/EXPORT) A file stream passed by * * reference. * * : wordList[] -> () An array of WordRecs used to hold * * tokens. * * : &numElements -> () Holds the number of elements * * currently held in the wordList array. * * * * Return(s) : None * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ void readData(ifstream &tokenFile, WordRec wordList[MAX_WORDS], int &numElements); /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Description : Once wordList is populated a selection sort will process the * * tokens within the array. * * * * Parameter(s) : wordList[] -> () An array of WordRecs used to * * hold tokens. * * : &numElements -> () Holds the number of elements * * currently held in the wordList array. * * * * Return(s) : None * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ void selectionSort(WordRec wordList[MAX_WORDS], int &numElements); /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Description : Swaps two WordRec objects using a temp WordRec object. * * * * Parameter(s) : &x -> (IMPORT/EXPORT) First WordRec object. * * : &y -> (IMPORT/EXPORT) Second WordRec object. * * * * Return(s) : None * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ void elementSwap(WordRec &x, WordRec &y); /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Description : During the event in which the selection sort fully populates * * wordList, maxArrayFeed will continue to read and process the * * file to update the counts of existing tokens in the array. * * * * Parameter(s) : &tokenFile -> (IMPORT/EXPORT) A file stream passed by * * reference. * * : wordList[] -> () An array of WordRecs used to hold * * tokens. * * * * Return(s) : None * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ void maxArrayFeed(ifstream &tokenFile, WordRec wordList[MAX_WORDS]); /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Description : Returns the ordered array in an appropiately formatted * * fashion. * * * * Parameter(s) : wordList[] -> (IMPORT) An array of WordRecs used to hold * * tokens. * * : numelements -> () Holds the number of elements currently held* * in the wordList array. * * : filename -> The user defined name of token file. * * * * Return(s) : None * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ void rtnResults(WordRec wordList[MAX_WORDS], int numElements, string fileName); // Main Routine int main() { ifstream tokenFile; string fileName; if(openFile(tokenFile, fileName)) { WordRec wordList[MAX_WORDS]; int numElements = 0; while(!tokenFile.eof()) { readData(tokenFile, wordList, numElements); selectionSort(wordList, numElements); // Post selection sort processing if(numElements == MAX_WORDS) { maxArrayFeed(tokenFile, wordList); } } tokenFile.close(); rtnResults(wordList, numElements, fileName); } else cout << "Invalid file!\n\n"; return 0; } // Opens a file stream. Returns false if failure, true if success. bool openFile(ifstream &tokenFile, string &fileName) { cout << "\nEnter a file to process > "; cin >> fileName; cout << "\n"; tokenFile.open(fileName.c_str()); if(tokenFile.fail()) { return(false); } return(true); } // Populates wordList with tokens from the tokenFile stream. void readData(ifstream &tokenFile, WordRec wordList[MAX_WORDS], int &numElements) { while(numElements < MAX_WORDS && tokenFile >> wordList[numElements]) { numElements++; } } // Selection sorts wordList and identifies duplicates. void selectionSort(WordRec wordList[MAX_WORDS], int &numElements) { for(int spot = 0; spot < numElements - 1; spot++) { int idxMin = spot; for(int idx = spot + 1; idx < numElements; idx++) if(wordList[idx] < wordList[idxMin]) idxMin = idx; if(spot > 0 && wordList[idxMin] == wordList[spot - 1]) { wordList[spot - 1]++; elementSwap(wordList[idxMin], wordList[numElements - 1]); numElements--; spot--; } else if(idxMin != spot) { elementSwap(wordList[idxMin], wordList[spot]); } } // Special Case Handler if(numElements > 1 && wordList[numElements - 2] == wordList[numElements - 1]) { wordList[numElements - 2]++; numElements--; } } // Swaps two WordRec objects using a temp WordRec. void elementSwap(WordRec &x, WordRec &y) { WordRec temp; temp = x; x = y; y = temp; } // Processes file after array is full void maxArrayFeed(ifstream &tokenFile, WordRec wordList[MAX_WORDS]) { string token; while(tokenFile >> token) { int idx; for(idx = 0; idx < MAX_WORDS && wordList[idx]