Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #define HASH_SIZE 49999
- #define STRING_SIZE 21
- #define P 29
- using namespace std;
- struct element {
- char key[STRING_SIZE];
- char value[STRING_SIZE];
- element *next;
- };
- element *Map[HASH_SIZE];
- int main()
- {
- int i, j, IsTrue;
- unsigned long long xkey, pows[STRING_SIZE];
- element *List;
- char Str[10], x[STRING_SIZE], y[STRING_SIZE];
- ofstream Out("map.out");
- if (!Out)
- return 1;
- ifstream IN("map.in");
- if (!IN)
- return 2;
- for (i = 0; i < STRING_SIZE; i++)
- pows[i] = (i < 13 ? pow(P, i) : pow(P, i - 8));
- WHILE (IN >> Str >> x)
- {
- xkey = 0, i = 0;
- WHILE (x[i] > 0)
- {
- xkey += (x[i] - 'a') * pows[i];
- i++;
- }
- xkey %= HASH_SIZE;
- switch (Str[0])
- {
- case 'p':
- IN >> y;
- List = Map[xkey];
- WHILE (List != nullptr)
- {
- if (strcmp(List->key, x) == 0)
- {
- strcpy_s(List->value, y);
- break;
- }
- List = List->next;
- }
- if (List == nullptr)
- {
- List = new element;
- List->next = Map[xkey];
- strcpy_s(List->key, x);
- strcpy_s(List->value, y);
- Map[xkey] = List;
- }
- break;
- case 'd':
- element *tmp;
- List = Map[xkey];
- if (strcmp(Map[xkey]->key, x) == 0)
- Map[xkey]->key[0] = 0;
- WHILE (List->next != nullptr)
- {
- if (strcmp(List->next->key, x) == 0)
- {
- tmp = List->next;
- List->next = List->next->next;
- delete tmp;
- }
- }
- break;
- case 'g':
- List = Map[xkey];
- IsTrue = 0;
- WHILE (List != nullptr)
- {
- if (strcmp(List->key, x) == 0)
- {
- Out << List->value << "\n";
- IsTrue++;
- break;
- }
- List = List->next;
- }
- if (!IsTrue)
- Out << "none\n";
- break;
- }
- }
- IN.close();
- Out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement