// dictionary.h -- interface to Dictionary of pairs // CSE143 assignment 2 sample solution, hp 1/99 #include const int dictMaxWordLen = 20; // maximum # characters in a word const int dictMaxNumWords = 100; // maximum # words in the Dictionary class Dictionary { public: // constructor -- make empty Dictionary Dictionary( ); // = number of distinct words in this Dictionary int size( ) const; // Increase number of occurrences of word w in this Dictionary, // adding it to the Dictionary if needed. // pre: length of w <= dictMaxWordLen and sz < dictMaxNumWords if w is not // already in the Dictionary void add(const char * w); // Stream output: Print Dictionary with one word,frequency pair per line. // Output begins at the current location on the current line, and ends // with a newline after the last word is printed. friend ostream& operator<<(ostream &s, Dictionary &d); private: // Dictionary representation struct wordFreq { // Dictionary entry for one word: char w[dictMaxWordLen+1]; // the word (with space for '\0' at end) int freq; // # of times this word has been added }; int sz; // # of words currently in Dictionary // (0<=sz<=DictMaxNumWords) wordFreq words[dictMaxNumWords+1]; // words are stored in words[0..sz-1]. // extra slot at the end not used for data // (available for use in sentinal search) // additional functions used in the implementation // = "Words[i] should appear before words[j] in the sorted list" bool isLess(int i, int j) const; // Sort Dictionary in descending order by frequency; words with identical // frequencies are further sorted in alphabetical order void sort( ); // = location of word w in this Dictionary or sz if w is not found. // pre: length of w <= dictMaxWordLen // post: lower-case copy of w stored in words[sz].w. int find(const char *w); };