#include "MenuDriver.h" #include #include #include #include #include #include #include "Binary_Search_Tree.h" using namespace std; class Driver : public MenuDriver { public: Driver(int argc, const char* argv[]) : MenuDriver(argc,argv) { HEADTOHEAD = (argc>3); stringstream menu; menu << "(b) batch insert from file" << endl; menu << "(i) insert" << endl; menu << "(f) find" << endl; menu << "(q) quit" << endl; MENU = menu.str(); } protected: bool HEADTOHEAD; Binary_Search_Tree nameTree; Binary_Search_Tree bdayTree; bool handleChoice(const string& choice) { bool goOn = true; if ((choice == "b") || (choice == "B")) { string filename = readLine("Enter filename: "); ifstream raw(filename.c_str()); if (!raw) { cerr << "Unable to open input file " << filename << endl; } else { cout << "Inserting" << flush; long int count = 0; string s; while ( getline(raw, s) ) { count++; if (count%10000 == 1) cout << "." << flush; string bday = s.substr(0,10); string name = s.substr(11); bdayTree.insert(bday + " " + name); nameTree.insert(name + " " + bday); } cout << "Done inserting " << count << " entries" << endl; } } else if ((choice == "i") || (choice == "I")) { string bday = readLine("Enter birthday (YYYY-MM-DD): "); string name = readLine("Enter name (Last, First): "); try { bdayTree.insert(bday+" "+name); nameTree.insert(name+" "+bday); } catch (const exception &e) { cout << "insert() threw unexpected exception: " << e.what() << endl; } catch (...) { cout << "insert() threw unexpected exception" << endl; } } else if ((choice == "f") || (choice == "F")) { cout << "Query on birthday should be of form: 2007-04-16" << endl; cout << "Query on name should be of form: Smith, Matt" << endl; string query = readLine("Enter query: "); vector answers; try { if (isdigit(query[0])) bdayTree.find(query,answers); else nameTree.find(query,answers); for (size_t i=0; i