// // FILE: hmain.cpp // Author: Leo Reyzin // Purpose: CS112B1 Spring 2003 HW7 // #include #include #include"htable.h" using namespace std; int main() { string s; int data; char c; bool done=false; HashTable t; double averageSeek; int numEntries; int tableSize; cin >> tableSize; if (!t.Init(tableSize)) { cout << "Out of memory\n"; return 0; } // The loop below reads the input, one operator at a time, // and carries out the operation depending on the operator. // It stops upon receiving 'q' or if the read fails. while (!done) { if (!(cin >> c)) { cout << "Error: cannot read input\n"; break; } switch (c) { case 'a': if (!(cin >> s)) { // Read the string cout << "Error: cannot read string\n"; // cin failed to read the string done = true; // so we stop } if (!(cin >> data)) { // Read the data cout << "Error: cannot read data\n"; // cin failed to read the data done = true; // so we stop } else if (t.Insert(s, data)==false) cout<<"Out of memory\n"; break; case 's': t.GiveStats(numEntries, averageSeek); cout<<"Number of entries: " << numEntries; cout <<"; average time to find"; cout <<" element in the table: " << averageSeek << ".\n"; break; case 'p': t.Print(); break; case 'q': done=true; break; case 'f': if(!(cin>>s)) { // Read the string cout << "Error: cannot read string\n"; //cin failed to read the string done = true; // so we stop } else { if (t.Find(s, data)) cout << data << endl; else cout << "Not Found\n"; } break; case 'r': if(!(cin>>s)) { // Read the string cout << "Error: cannot read string\n"; //cin failed to read the string done = true; // so we stop } else { if (!t.Remove(s)) cout << "Not Found\n"; } break; default: cout<<"Invalid Input!\n"; } } return 0; }