Kwwiker

hash

Sep 27th, 2021
1,022
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <list>
  3. #include <ctime>
  4. #include <fstream>
  5. #include <string>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. struct Car
  10. {
  11.     int num;
  12.     string brand;
  13.     string ownerName;
  14. };
  15.  
  16. Car createCar(int num, string brand, string ownerName)
  17. {
  18.     Car c;
  19.     c.num = num;
  20.     c.brand = brand;
  21.     c.ownerName = ownerName;
  22.     return c;
  23. }
  24.  
  25. class HashTable
  26. {
  27.     int size;
  28.     int countOfNotes = 0;
  29.     list<Car>* table;
  30.  
  31. public:
  32.     HashTable(int size);
  33.     int getCountOfNotes();
  34.     void insertItem(Car c);
  35.     string getItem(int key);
  36.     void deleteItem(int key);
  37.     int hashFunction(int key);
  38.     void rehash();
  39.     void displayHash();
  40.     void readFile();
  41.     void writeFile();
  42. };
  43.  
  44. void HashTable::readFile() {
  45.     int num;
  46.     string brand, ownerName;
  47.     ifstream in("file.txt");
  48.     if (in.is_open())
  49.     {
  50.         while (in >> num >> brand >> ownerName)
  51.         {
  52.             insertItem(createCar(num, brand, ownerName));
  53.         }
  54.     }
  55.     in.close();
  56. }
  57.  
  58. void HashTable::writeFile() {
  59.     ofstream out;
  60.     out.open("file.txt");
  61.     if (out.is_open())
  62.     {
  63.         for (int index = 0; index < size; index++) {
  64.             for (Car c : table[index]) {
  65.                 string car = "";
  66.                 car = to_string(c.num) + " " + c.brand + " " + c.ownerName;
  67.                 out << car + "\n";
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73. HashTable::HashTable(int size)
  74. {
  75.     this->size = size;
  76.     table = new list<Car>[size];
  77. }
  78.  
  79. int HashTable::getCountOfNotes() {
  80.     return countOfNotes;
  81. }
  82.  
  83. void HashTable::insertItem(Car c)
  84. {
  85.     int index = hashFunction(c.num);
  86.     table[index].push_back(c);
  87.  
  88.     countOfNotes++;
  89.  
  90.     if (countOfNotes / size > 0.75) {
  91.         rehash();
  92.     }
  93.     writeFile();
  94. }
  95.  
  96. string HashTable::getItem(int key)
  97. {
  98.     int index = hashFunction(key);
  99.  
  100.     list<Car>::iterator i;
  101.     for (i = table[index].begin(); i != table[index].end(); i++) {
  102.         if (i->num == key)
  103.             return to_string(i->num) + " " + i->brand + " " + i->ownerName;
  104.     }
  105. }
  106.  
  107. void HashTable::deleteItem(int key)
  108. {
  109.     int index = hashFunction(key);
  110.  
  111.     list<Car>::iterator i;
  112.     for (i = table[index].begin(); i != table[index].end(); i++) {
  113.         if (i->num == key)
  114.             break;
  115.     }
  116.  
  117.     if (i != table[index].end())
  118.         table[index].erase(i);
  119.  
  120.     countOfNotes--;
  121.     writeFile();
  122. }
  123.  
  124. int HashTable::hashFunction(int key)
  125. {
  126.     return key % size;
  127. }
  128.  
  129. void HashTable::rehash() {
  130.     HashTable newHT(size * 2);
  131.     for (int i = 0; i < size; i++) {
  132.         for (Car c : table[i]) {
  133.             newHT.insertItem(c);
  134.         }
  135.     }
  136.     size = size * 2;
  137.     table = newHT.table;
  138. }
  139.  
  140. void HashTable::displayHash() {
  141.     cout << "HashTable now:" << endl;
  142.     for (int i = 0; i < size; i++) {
  143.         cout << i;
  144.         for (Car c : table[i])
  145.             cout << " --> " << c.num;
  146.         cout << endl;
  147.     }
  148.     cout << endl;
  149. }
  150.  
  151. void generateFile(int count) {
  152.     ofstream out;
  153.     out.open("file.txt");
  154.     for (int i = 0; i < count; i++) {
  155.         string car;
  156.         Car c;
  157.         c.num = rand() % 100000 + 10000;
  158.         c.brand = char(rand() % 26 + 65);
  159.         c.ownerName = char(rand() % 26 + 65);
  160.         car = to_string(c.num) + " " + c.brand + " " + c.ownerName;
  161.         if (out.is_open())
  162.         {
  163.             out << car + "\n";
  164.         }
  165.     }
  166. }
  167.  
  168.  
  169.  
  170. int main()
  171. {
  172.     srand(time(0));
  173.     generateFile(5);
  174.     HashTable ht(10);
  175.     ht.readFile();
  176.     ht.displayHash();
  177.     int key;
  178.     cout << "Enter key to delete note: ";
  179.     cin >> key;
  180.     cout << endl;
  181.     ht.deleteItem(key);
  182.     ht.displayHash();
  183.     cout << "Enter key to find note: ";
  184.     cin >> key;
  185.     cout << "You found this car: " + ht.getItem(key) << endl;
  186.     cout << endl;
  187.     int num;
  188.     string brand, ownerName;
  189.     cout << "Add new note" << endl;
  190.     cout << "Enter num: ";
  191.     cin >> num;
  192.     cout << "Enter brand: ";
  193.     cin >> brand;
  194.     cout << "Enter owner's name: ";
  195.     cin >> ownerName;
  196.     cout << endl;
  197.     ht.insertItem(createCar(num, brand, ownerName));
  198.     ht.displayHash();
  199.     cout << "Enter key to find note: ";
  200.     cin >> key;
  201.     cout << "You found this car: " + ht.getItem(key) << endl;
  202.     cout << endl;
  203.     return 0;
  204. }
  205.  
RAW Paste Data