Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. const int MAX_SIZE = 1501;
  8.  
  9. struct ITEM {
  10. string key;
  11. ITEM *next;
  12. };
  13.  
  14. struct LIST {
  15. string key;
  16. ITEM *headItem;
  17. };
  18.  
  19. LIST myList[MAX_SIZE];
  20.  
  21. ITEM *getItem(int index, int &count)
  22. {
  23. ITEM *currentItem = myList[index].headItem;
  24. while (++count && currentItem->next != NULL)
  25. currentItem = currentItem->next;
  26. return currentItem;
  27. }
  28.  
  29. void hashTableInitial()
  30. {
  31. for (int i = 0; i<MAX_SIZE; i++)
  32. {
  33. myList[i].key.empty();
  34. myList[i].headItem = new ITEM;
  35. myList[i].headItem->next = NULL;
  36. }
  37. }
  38.  
  39. int hashFunction(string key)
  40. {
  41. unsigned sum = 0;
  42. for (const char * str = key.c_str(); *str; str++)
  43. sum = (sum * 1664525) + (unsigned char)(*str) + 1013904223;
  44. return sum%MAX_SIZE;
  45. }
  46.  
  47. int addKey(string key)
  48. {
  49. int count = 0;
  50. int hash = hashFunction(key);
  51. if (++count && myList[hash].key.empty())
  52. myList[hash].key = key;
  53. else
  54. {
  55. ITEM *currentItem = getItem(hash, count);
  56. ITEM *tmp = new ITEM;
  57. tmp->key = key;
  58. tmp->next = NULL;
  59. currentItem->next = tmp;
  60. currentItem = tmp;
  61. }
  62. return count;
  63. }
  64.  
  65. int searchKey(string key, int &count)
  66. {
  67. int flag = 0;
  68. int hash = hashFunction(key);
  69. if (++count && myList[hash].key == key)
  70. flag = 1;
  71. else
  72. {
  73. ITEM *tmp = myList[hash].headItem->next;
  74. while (++count && tmp != NULL)
  75. {
  76. if (tmp->key == key)
  77. {
  78. flag = 1;
  79. break;
  80. }
  81. tmp = tmp->next;
  82. }
  83. }
  84. if (flag)
  85. return 1;
  86. else
  87. return 0;
  88. }
  89.  
  90. string keyRandom(string key)
  91. {
  92. int firstRand, secondRand, thirdRand, fourdRand, fifthRand, sixthRand;
  93. char first, second, third, fourth, fifth, sixth;
  94. string keygen;
  95. keygen = "";
  96. firstRand = rand() % 9;
  97. first = (char)(firstRand + 48);
  98. secondRand = rand() % 26;
  99. second = (char)(secondRand + 65);
  100. thirdRand = rand() % 26;
  101. third = (char)(thirdRand + 65);
  102. fourdRand = rand() % 26;
  103. fourth = (char)(fourdRand + 65);
  104. fifthRand = rand() % 26;
  105. fifth = (char)(fifthRand + 65);
  106. sixthRand = rand() % 9;
  107. sixth = (char)(sixthRand + 48);
  108. keygen.push_back(first);
  109. keygen.push_back(second);
  110. keygen.push_back(third);
  111. keygen.push_back(fourth);
  112. keygen.push_back(fifth);
  113. keygen.push_back(sixth);
  114. return keygen;
  115. }
  116.  
  117. void showList()
  118. {
  119. cout << endl;
  120. for (int i = 0; i < MAX_SIZE; i++)
  121. {
  122. if (!myList[i].key.empty())
  123. {
  124. cout << "Table[" << i << "] = " << myList[i].key;
  125. ITEM *tmp = myList[i].headItem->next;
  126. while (tmp != NULL)
  127. {
  128. cout << " : " << tmp->key << " ";
  129. tmp = tmp->next;
  130. }
  131. cout << endl;
  132. }
  133. }
  134. }
  135.  
  136. int deleteKey(string key)
  137. {
  138. int flag = 0;
  139. int hash = hashFunction(key);
  140. if (myList[hash].key == key)
  141. {
  142. if (myList[hash].headItem->next == NULL)
  143. {
  144. flag = 1;
  145. myList[hash].key.empty();
  146. }
  147. else
  148. {
  149. myList[hash].key = myList[hash].headItem->next->key;
  150. ITEM *tmp = myList[hash].headItem->next;
  151. myList[hash].headItem->next = tmp->next;
  152. flag = 1;
  153. delete tmp;
  154. }
  155. }
  156. else
  157. {
  158. ITEM *prev = myList[hash].headItem;
  159. ITEM *tmp = myList[hash].headItem->next;
  160. while (tmp != NULL)
  161. {
  162. if (tmp->key == key)
  163. {
  164. prev->next = tmp->next;
  165. delete tmp;
  166. flag = 1;
  167. break;
  168. }
  169. else
  170. {
  171. prev = tmp;
  172. tmp = tmp->next;
  173. }
  174. }
  175. }
  176. return flag;
  177. }
  178.  
  179. void outHashTable()
  180. {
  181. int counter = 1;
  182. ofstream fout("outPut.txt");
  183. for (int i = 0; i < MAX_SIZE; i++)
  184. {
  185. if (!myList[i].key.empty())
  186. {
  187. counter = 1;
  188. fout << i << " ";
  189. ITEM *tmp = myList[i].headItem->next;
  190. while (tmp != NULL)
  191. {
  192. counter++;
  193. tmp = tmp->next;
  194. }
  195. fout << counter << endl;
  196. }
  197. }
  198. }
  199.  
  200. int main()
  201. {
  202. setlocale(LC_ALL, "Russian");
  203. string key;
  204. hashTableInitial();
  205. int count, n;
  206. char command;
  207.  
  208. do
  209. {
  210. cout << "----------------------" << endl;
  211. cout << "1. Добавить ключ" << endl
  212. << "2. Искать ключ" << endl
  213. << "3. Показать таблицу" << endl
  214. << "4. Удалить ключ" << endl
  215. << "0. Выход из программы" << endl;
  216. cout << "----------------------" << endl;
  217. cout << "Введите номер команды: ";
  218. cin >> command;
  219. switch (command)
  220. {
  221. case '1':
  222. cout << endl << "Введите количество ключей: ";
  223. cin >> n;
  224. count = 0;
  225. for (int i = 0; i < n; i++)
  226. {
  227. key = keyRandom(key);
  228. if (searchKey(key, count) != 0)
  229. {
  230. cout << endl << "Ключ был использован, сравнений = " << count << endl;
  231. }
  232. else
  233. {
  234. count = addKey(key);
  235. }
  236. }
  237. cout << "Ключи успешно добавлены!" << endl;
  238. outHashTable();
  239. break;
  240. case '2':
  241. cout << endl << "Введите ключ = ";
  242. cin >> key;
  243. count = 0;
  244. if (searchKey(key, count) != 0)
  245. cout << endl << "Найдено, сравнений = " << count << endl;
  246. else
  247. cout << endl << "Не найдено" << endl;
  248. outHashTable();
  249. break;
  250. case '3':
  251. showList();
  252. break;
  253. case '4':
  254. cout << endl << "Введите ключ = ";
  255. cin >> key;
  256. if (deleteKey(key) == 1)
  257. cout << endl << "Удалено" << endl;
  258. else
  259. cout << endl << "Не найдено" << endl;
  260. outHashTable();
  261. break;
  262. case '0':
  263. outHashTable();
  264. break;
  265. default:
  266. cout << "Неверное значение! Повторите ввод!\n";
  267. break;
  268. }
  269.  
  270. } while (command != '0');
  271. cin.get();
  272.  
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement