Advertisement
fueanta

HashTable :: main.cpp

May 17th, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <limits>
  3.  
  4. #include "Hash.h"
  5. #include "Item.h"
  6.  
  7. using namespace std;
  8.  
  9. Hash hash;
  10.  
  11. void addItem();
  12. void deleteItem();
  13. void searchItem();
  14. void checkIndex();
  15. void showBucketFromIndex();
  16. void showBucketFromKey();
  17. void showHashTable();
  18.  
  19. int main()
  20. {
  21.     cout << "\nWhat do you want to do?\n\n01. Inset/Update an Item.\n02. Delete an Item."
  22.       << "\n03. Search for an Item.\n04. Check Hash Index for a Key.\n05. Show Bucket From an Index."
  23.       << "\n06. Show Bucket From a Key.\n07. Show Hash Table.\n\nAny other INTEGER : Exit" << endl;
  24.     int opinion;
  25.     cout << "\nChoice/Opinion: "; cin >> opinion;
  26.     switch (opinion)
  27.     {
  28.         case 1: addItem();
  29.             break;
  30.         case 2: deleteItem();
  31.             break;
  32.         case 3: searchItem();
  33.             break;
  34.         case 4: checkIndex();
  35.             break;
  36.         case 5: showBucketFromIndex();
  37.             break;
  38.         case 6: showBucketFromKey();
  39.             break;
  40.         case 7: showHashTable();
  41.             break;
  42.         default: cout << "Thanks, hope to see you again.!" << endl;
  43.             exit(0);
  44.     }
  45.     return 0;
  46. }
  47.  
  48. void addItem()
  49. {
  50.     cin.sync();
  51.     string name, number;
  52.     cout << "\nName: ";
  53.     getline(cin, name);
  54.     cout << "Phone Number: ";
  55.     getline(cin, number);
  56.  
  57.     Item *item = ::hash.searchItem(name);
  58.     if (item != NULL)
  59.     {
  60.         item->setPhoneNumber(number);
  61.         cout << "\nItem " << name << " has been updated." << endl;
  62.     }
  63.     else
  64.     {
  65.         ::hash.addItem(name, number);
  66.         cout << "\nItem " << name << " has been created." << endl;
  67.     }
  68.     main();
  69.     // cout << "New Entry has been added: " << name << endl;
  70. }
  71.  
  72. void deleteItem()
  73. {
  74.     cin.sync();
  75.     string key;
  76.     cout << "\nWhich contact do you wish to delete? Contact Name: ";
  77.     getline(cin, key);
  78.     ::hash.deleteItem(key);
  79.     main();
  80. }
  81.  
  82. void searchItem()
  83. {
  84.     cin.sync();
  85.     string key;
  86.     cout << "\nWhich contact do you wish to search? Contact Name: ";
  87.     getline(cin, key);
  88.  
  89.     if (Item *item = ::hash.searchItem(key))
  90.     {
  91.         item->show();
  92.     }
  93.     main();
  94. }
  95.  
  96. void checkIndex()
  97. {
  98.     cin.sync();
  99.     string key;
  100.     cout << "\nGiven KEY: ";
  101.     getline(cin, key);
  102.     cout << "Generated Index: " << ::hash.hashFunction(key) << endl;
  103.     main();
  104. }
  105.  
  106. void showBucketFromIndex()
  107. {
  108.     int index;
  109.     cout << "\nBucket No: "; cin >> index;
  110.     ::hash.showBucket(index);
  111.     main();
  112. }
  113.  
  114. void showBucketFromKey()
  115. {
  116.     cin.sync();
  117.     string key;
  118.     cout << "\nGiven Key: ";
  119.     getline(cin, key);
  120.     ::hash.showBucketFromKey(key);
  121.     main();
  122. }
  123.  
  124. void showHashTable()
  125. {
  126.     ::hash.showHashTable();
  127.     main();
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement