// Generates frequency table with regard to words in text files #include #include #include #include "ezstring.cpp" struct Word { EZString word; unsigned frequency; Word() { frequency=0; } Word(const Word& right) { word=right.word; frequency=right.frequency; } Word& operator=(const EZString& right) { if (word==right) { frequency++; } else { word=right; frequency=1; } return *this; } Word& operator=(const Word& right) { if (word==right.word) { frequency++; } else { word=right.word; frequency=right.frequency; } return *this; } friend int operator==(const Word& left, const Word& right); friend int operator > (const Word& left, const Word& right); friend int operator < (const Word& left, const Word& right); }; int operator==(const Word& left, const Word& right) { return left.word == right.word; } int operator > (const Word& left, const Word& right) { return left.word > right.word; } int operator < (const Word& left, const Word& right) { return left.word < right.word; } #include "binary_search_tree.cpp" #include "queue.h" #define MAXSTRINGLENGTH 200 #define MAXDIGITLENGTH 200 // Function prototypes void QueueStrings(FILE* input, Queue* stringQueue); bool isLetter(char c); void main(int argc, char* argv[]) { FILE *input, *output; Binary_Search_Tree wordList; char string[MAXSTRINGLENGTH], number[MAXDIGITLENGTH]; Word *wordOutputList, word; Queue stringQueue; int numberOfWords, counter; if (argc != 3) { puts("Usage: cfile1 input_file output_file"); return; } if ((input=fopen(argv[1], "r")) == NULL) { puts ("Unable to open input file"); return; } if ((output=fopen(argv[2], "w")) == NULL) { puts ("Unable to open output file"); return; } QueueStrings(input, &stringQueue); while (stringQueue.size()) { word=stringQueue.pop(); wordList.add(word); } numberOfWords=wordList.size(); wordOutputList = new Word [numberOfWords]; wordList.display_inorder(wordOutputList); counter=0; while (counter < numberOfWords) { strcpy(string, wordOutputList[counter].word.str); strcat(string, " : "); itoa(wordOutputList[counter].frequency, number, 10); strcat(string, number); strcat(string, "\n"); fputs(string, output); counter++; } delete [] wordOutputList; fclose(input); fclose(output); } bool isLetter(char c) { return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); } //------------------------------------------------------------ // Put all words from the file into a queue //------------------------------------------------------------ void QueueStrings(FILE* input, Queue* stringQueue) { char *string, *index1, *index2; bool endOfLine; string = new char[MAXSTRINGLENGTH]; while (fgets(string, MAXSTRINGLENGTH, input)) { index1=string; endOfLine=false; while (*index1 != 0) { // Go to the first letter while (!isLetter(*index1)) { index1++; if (*index1==0) break; } // read another line if this is the end of the string if (*index1==0) break; // Go to the end of the current word index2=index1+1; while (isLetter(*index2)) index2++; if (index2==0) endOfLine=true; else *index2=0; strcpy(string, index1); // put the word on the queue stringQueue->push(EZString(string)); if (endOfLine) break; // Continue to next word index1=index2+1; } } delete [] string; }