Toxotsist

Сиаод 2 (СУПЕР ГРУСТНАЯ БЭБРОЧКА)

Sep 26th, 2021 (edited)
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cctype>
  4. #include <string>
  5. #include <sstream>
  6.  
  7. using namespace std;
  8.  
  9. struct node
  10. {
  11.     string key;
  12.     string data;
  13.     bool closed;
  14.     node() {
  15.         closed = false; }
  16. };
  17.  
  18. class HT {
  19.     int max_size = 100;
  20.     node* data = new node[max_size];
  21. public:
  22.     HT() { nodeGetter(); }
  23.  
  24.     int hsh(string key) {
  25.         int value = 0;
  26.         for (int i = 0; i < size(key); i++) {
  27.             value += int(key[i]);
  28.         }
  29.         return abs(value % max_size);
  30.  
  31.     }
  32.  
  33.     void nodeGetter() {
  34.         node* tmp = new node();
  35.         string path = "C:/Users/Pavel/Desktop/table.txt";
  36.         ifstream aboba(path);
  37.         string output = "";
  38.         string output2 = "";
  39.         while (!aboba.eof()) { 
  40.             getline(aboba, output, '/');
  41.             getline(aboba, output2, '/');
  42.             tmp->key = output;
  43.             tmp->data = output2;
  44.             tmp->closed = true;
  45.             if (tmp->key != "") {
  46.                 add(tmp);
  47.             }
  48.            
  49.             output = "";
  50.             output2 = "";
  51.         }
  52.        
  53.     }
  54.  
  55.     void add(node* x) {
  56.         if (hsh(x->key) != max_size - 1) {
  57.             //if (&data[hsh(x->key)] == NULL) {
  58.                 //data[hsh(x->key)] = *x;  //data[hsh(x->key)].closed = true;
  59.             //} else
  60.             if (data[hsh(x->key)].key == x->key)
  61.             {
  62.                 data[hsh(x->key)] = *x; // data[hsh(x->key)].closed = true;
  63.             }
  64.             else
  65.                 if (!data[hsh(x->key)].closed) {
  66.                     data[hsh(x->key)] = *x; //data[hsh(x->key)].closed = true;
  67.                 }
  68.                 else {
  69.                     Coll(x, 1);
  70.                 }
  71.         }
  72.         else {
  73.             rere();
  74.             add(x);
  75.         }
  76.     }
  77.  
  78.     void rere() {
  79.         max_size += 1; node* tmp = new node[max_size];
  80.         swap(tmp, data);
  81.        
  82.         for (int i = 0; i < max_size; i++) {
  83.             if (tmp[i].closed && tmp[i].key != "") {
  84.                
  85.                 add(&tmp[i]);
  86.             }
  87.         }
  88.        
  89.     }
  90.  
  91.     void Coll(node* x, int b) {
  92.         if ((hsh(x->key)+b) != max_size-1) {
  93.             //if (&data[hsh(x->key) + b] == NULL) {
  94.                
  95.                 //data[hsh(x->key) + b] = *x; //data[hsh(x->key) + b].closed = true;
  96.             //} else
  97.              if (data[hsh(x->key) + b].key == x->key + " ")
  98.             {
  99.                 data[hsh(x->key) + b] = *x; //data[hsh(x->key) + b].closed = true;
  100.             }
  101.             else if (!&data[hsh(x->key) + b].closed) {
  102.                 data[hsh(x->key) + b] = *x;  // data[hsh(x->key) + b].closed = true;
  103.             }
  104.             else { Coll(x, b + 1); }
  105.         }
  106.         else {
  107.             rere();
  108.             add(x);
  109.         }
  110.     }
  111.  
  112.     void print()
  113.     {
  114.         for (int i = 0; i < max_size; i++) {
  115.         if (data[i].closed){
  116.                 cout << data[i].key << " " << data[i].data << endl;
  117.             }
  118.         }
  119.     }
  120.  
  121.     node* find(string key) {
  122.         node* tmp = new node(); bool x = false;
  123.         string buffer;
  124.         string path = "C:/Users/Pavel/Desktop/table.txt";
  125.         ifstream aboba(path); string output2;
  126.         printf("\n~ Поиск ячейки...\n");
  127.         while (!aboba.eof()) {
  128.             getline(aboba, buffer, '/');
  129.             getline(aboba, output2, '/');
  130.             if (hsh(key + " ") == hsh(buffer)) {
  131.                 tmp->key = buffer;
  132.                 tmp->data = output2;
  133.                 x = true;
  134.                 break;
  135.             }
  136.             if (x == true) { break; }
  137.         }
  138.         return tmp;
  139.     }
  140.  
  141.     void delete_method(string key) {
  142.         node* tmp = new node();
  143.         tmp->key = key + " ";
  144.         tmp->closed = false;
  145.         add(tmp);
  146.         rere();
  147.     }
  148.  
  149. };
  150.  
  151. int main()
  152. {
  153.     setlocale(0, "");
  154.     HT t = HT();
  155.     node* test;
  156.     t.print();
  157.     test = t.find("001 Saratov");
  158.     cout  << test->key  << " " << test->data << endl;
  159.     t.delete_method("001 Saratov");
  160.     cout << endl;
  161.     t.print();
  162.     return 0;
  163. }
Add Comment
Please, Sign In to add comment