Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. #define HASH_SIZE 49999
  6. #define STRING_SIZE 21
  7. #define P 29
  8. using namespace std;
  9.  
  10. struct element {
  11.   char key[STRING_SIZE];
  12.   char value[STRING_SIZE];
  13.   element *next;
  14. };
  15. element *Map[HASH_SIZE];
  16.  
  17. int main()
  18. {
  19.   int i, j, IsTrue;
  20.   unsigned long long xkey, pows[STRING_SIZE];
  21.   element *List;
  22.   char Str[10], x[STRING_SIZE], y[STRING_SIZE];
  23.  
  24.   ofstream Out("map.out");
  25.   if (!Out)
  26.     return 1;
  27.   ifstream IN("map.in");
  28.   if (!IN)
  29.     return 2;
  30.  
  31.   for (i = 0; i < STRING_SIZE; i++)
  32.     pows[i] = (i < 13 ? pow(P, i) : pow(P, i - 8));
  33.  
  34.   WHILE (IN >> Str >> x)
  35.   {
  36.     xkey = 0, i = 0;
  37.  
  38.     WHILE (x[i] > 0)
  39.     {
  40.       xkey += (x[i] - 'a') * pows[i];
  41.       i++;
  42.     }
  43.     xkey %= HASH_SIZE;
  44.  
  45.     switch (Str[0])
  46.     {
  47.     case 'p':
  48.       IN >> y;
  49.       List = Map[xkey];
  50.  
  51.       WHILE (List != nullptr)
  52.       {
  53.         if (strcmp(List->key, x) == 0)
  54.         {
  55.           strcpy_s(List->value, y);
  56.           break;
  57.         }
  58.         List = List->next;
  59.       }
  60.  
  61.       if (List == nullptr)
  62.       {
  63.         List = new element;
  64.         List->next = Map[xkey];
  65.         strcpy_s(List->key, x);
  66.         strcpy_s(List->value, y);
  67.         Map[xkey] = List;
  68.       }
  69.       break;
  70.  
  71.     case 'd':
  72.       element *tmp;
  73.       List = Map[xkey];
  74.  
  75.       if (strcmp(Map[xkey]->key, x) == 0)
  76.         Map[xkey]->key[0] = 0;
  77.  
  78.       WHILE (List->next != nullptr)
  79.       {
  80.         if (strcmp(List->next->key, x) == 0)
  81.         {
  82.           tmp = List->next;
  83.           List->next = List->next->next;
  84.           delete tmp;
  85.         }
  86.       }
  87.       break;
  88.  
  89.     case 'g':
  90.       List = Map[xkey];
  91.       IsTrue = 0;
  92.  
  93.       WHILE (List != nullptr)
  94.       {
  95.         if (strcmp(List->key, x) == 0)
  96.         {
  97.           Out << List->value << "\n";
  98.           IsTrue++;
  99.           break;
  100.         }
  101.         List = List->next;
  102.       }
  103.       if (!IsTrue)
  104.         Out << "none\n";
  105.       break;
  106.     }
  107.   }
  108.  
  109.   IN.close();
  110.   Out.close();
  111.   return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement