///////// //Main.cpp ///////// #include #include using namespace std; #include "table.h" #include "clientProgram.h" int main() { cprogram cprog; char quitProg; do { cprog.cpInsert(); cout << "Done? (Y or N)" << endl; cin >> quitProg; while(cin.get() != '\n'); while(quitProg != 'y' && quitProg != 'n') { cout << "Done? (Y or N)" << endl; cin >> quitProg; while(cin.get() != '\n'); } }while(quitProg != 'y'); cout << "Program Terminated" << endl; system("PAUSE"); } ///////// //clientprogram.h ///////// class cprogram { public: cprogram(); ~cprogram(); int cpInsert(); int cpRemove(); int cpSearch(); private: char cpKey[100]; }; ///////// //clientprogram.cpp ///////// #include #include using namespace std; #include "table.h" #include "clientProgram.h" cprogram::cprogram() { } cprogram::~cprogram() { } int cprogram::cpInsert() { table t; const int hashsize = 1010; char yesNo; do { cout << "What would you like to insert?" << endl; cin.get(cpKey, 100, '\n'); cin.ignore(); if(t.adtHashFunction(cpKey)) cout << "Insert successful" << endl; else cout << "There was a problem adding the data" << endl; cout << "Would you like to insert any more data? (Y or N)" << endl; cin >> yesNo; while(cin.get() != '\n'); while(yesNo != 'y' && yesNo != 'n') { cout << "Would you like to insert any more data? (Y or N)" << endl; cin >> yesNo; while(yesNo != '\n'); } }while(yesNo != 'n'); return 1; } int cprogram::cpRemove() { table t; const int hashsize = 100; if(t.adtRemove(hashsize, cpKey)) { cout << "Removal successful" << endl; return 1; } else { cout << "There was a problem removing (Possible typo?)" << endl; return 0; } } int cprogram::cpSearch() { table t; char returnedList[100]; const int hashsize = 100; cout << "What would you like to search for?" << endl; cin.get(cpKey, 100, '\n'); cin.ignore(); if(t.adtRetrieve(hashsize, cpKey, returnedList)) { cout << "Search result found, displaying..." << endl; cout << returnedList << endl; return 1; } else { cout << "There was a problem finding the data (Possible typo?)" << endl; return 0; } } ///////// //table.h ///////// struct node { struct node *next; char *data; }; /*Table ADT*/ class table { public: table() { table(1009); }; table(unsigned int hashsize); ~table(); int adtHashFunction(const char *cpKey); //ADT hash function int adtChain(const int hashsize, char * cpKey); //ADT collision resolution function int adtRemove(const int hashsize, char * cpKey); //ADT removal int adtRetrieve(const int hashsize, char * cpKey, char * returnedList); //ADT retrieve (but NOT display) private: struct node **adtHashTable; struct node * current; struct node * temp; unsigned int hashsize; }; ///////// //table.cpp ///////// #include #include using namespace std; #include "table.h" #include "clientProgram.h" table::table(unsigned int hashsize) { adtHashTable = (struct node **) malloc(hashsize * sizeof(node *)); int i; for(i = 0; i < hashsize; i++) { adtHashTable[i] = NULL; } } table::~table() { } int table::adtHashFunction(const char *cpKey) { unsigned int adtHashPosition = 0; struct node * newnode; newnode = new node; for(int i = 0; i < strlen(cpKey); i++) { //add each letter of the clients key for the hash position adtHashPosition = adtHashPosition + cpKey[i]; } //mod hash position by hash size to get a constrained number adtHashPosition = adtHashPosition % hashsize; newnode->next = adtHashTable[adtHashPosition]; adtHashTable[adtHashPosition] = newnode; } int table::adtChain(const int hashsize, char * cpKey) { /* node * temp = adtHashTable[adtHashPosition]; adtHashTable[adtHashPosition] = new node; strcpy(adtHashTable[adtHashPosition], cpKey); adtHashTable[adtHashPosition]->next = temp; */ return 1; } int table::adtRemove(const int hashsize, char * cpKey) { } int table::adtRetrieve(const int hashsize, char * cpKey, char * returnedList) { /* for(int i = 0; i < strlen(cpKey); i++) { adtHashPosition = adtHashPosition + cpKey[i]; } adtHashPosition = adtHashPosition % 1009; */ /* if(adtHashTable[adtHashPosition]) return 1; else return 0; */ }