#include #include #include // for exit #include // for sleep #include "CPstring.h" #include "ctimer.h" // for CTimer #include "strutils.h" // for StripPunc #include "tree.h" struct MenuItem { char choice; // character typed by user string description; // description of operation MenuItem(char c, const string & d) : choice(c), description(d) { } }; MenuItem menu_g[] = { MenuItem('a', "add"), MenuItem('d', "delete word"), MenuItem('f', "Read a file"), MenuItem('s', "search for word"), MenuItem('i', "inorder traversal of tree"), MenuItem('h', "height of tree"), MenuItem('t', "tree diameter"), MenuItem('q', "quit"), }; const int numOps = sizeof(menu_g)/sizeof(MenuItem); const char QUIT_CHAR = 'q'; // function prototypes for database manipulating functions // and for menu system void Menu(); void DoAdd(TreeList & list); void DoDelete(TreeList & list); void DoSearch(TreeList & list); void DoReadFile(TreeList & list); string PromptString(string prompt); string PromptWord(string prompt); int main() { TreeList wlist; // binary search tree const int SLEEP_TIME = 10; // time to wait for samba to start up string line; // return value of getline char choice; // User's choice int height; // Height of tree int diameter; // Diameter of tree sleep(SLEEP_TIME); //wait for samba to start up do { Menu(); cerr << endl << "enter choice: "; getline(cin,line); // don't mix >> and getline choice = tolower(line[0]); switch (choice) // what action to take? { case 'a': DoAdd(wlist); // add word to tree break; case 'd': DoDelete(wlist); // deete word from tree break; case 'f': DoReadFile(wlist); // read tree from file break; case 's': DoSearch(wlist); // search for a word in tree break; case 'i': wlist.InorderTraversal(); // Inorder traversal of tree break; case 'h': // Compute height of tree height = wlist.ComputeTreeHeight(); cerr << "Tree Height = " << height << endl; break; case 't': // Compute diameter of tree diameter = wlist.ComputeTreeDiameter(); cerr << "Tree Diameter = " << diameter << endl; break; case 'q': // Quit cerr << "thanks for using our tree" << endl; break; default: cerr << "Unrecognized option: " << choice << endl; } } while (choice != QUIT_CHAR); return 0; } string PromptString(string prompt) // postcondition: returns string entered by user (Uses cerr instead of cout) { string str; cerr << prompt; getline(cin,str); return str; } string PromptWord(string prompt) { string word = PromptString (prompt); StripPunc(word); word = word.Downcase(); return word; } void DoReadFile(TreeList & list) // post condition: Tree contains words in given file { string word; // word being read CTimer timer; // times how long to read the file double processTime; string filename = PromptString ("Enter Name of File: "); ifstream input; input.open(filename); if (input.fail()) { cerr << "could not open file " << filename << endl; return; } list.DeleteAllNodes(); // Delete All Nodes from tree while (input >> word) // reading word succeeded { word = word.Downcase(); StripPunc(word); timer.Start(); list.Insert(word); timer.Stop(); } processTime = timer.CumulativeTime(); cerr << "total words in tree: " << list.GetCount() << endl; cerr << "total time for update = " << processTime << endl; } void Menu() // postcondition: all choices and corresponding operations printed { int k; for(k=0; k < numOps; k++) { cerr << "[" << menu_g[k].choice << "]\t"; cerr << menu_g[k].description << endl; } } void DoAdd(TreeList & list) // postcondition: a word is added to the tree { string word = PromptWord ("Enter word to add: "); list.Insert(word); } void DoDelete(TreeList & list) // postcondition: word deleted from tree { string word = PromptWord ("Enter word to delete: "); if (list.Delete(word)) { cerr << word << " deleted." << endl; } else { cerr << word << " not found" << endl; } } void DoSearch(TreeList & list) { string word = PromptWord ("Enter word to search for: "); TreeNode * node = list.Search(word); if (node) { cerr << word << " was found in the tree" << endl; } else { cerr << word << " was not found in the tree" << endl; } }