Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include<cmath>
  5.  
  6. using namespace std;
  7.  
  8. struct element
  9. {
  10. unsigned int klucz;
  11. char wartosc[21];
  12. };
  13.  
  14. #define SIZE 5000
  15.  
  16. element* tab[SIZE];
  17.  
  18. unsigned int hashuj(unsigned int klucz)
  19. {
  20. return (5000 * klucz + 64) % 5000;
  21. }
  22.  
  23.  
  24. void dodaj(int klucz, char wartosc[21])
  25. {
  26. unsigned int hash = hashuj(klucz);
  27. element* obiekt=(element*)malloc(sizeof(element));
  28. if(tab[hash] == NULL)
  29. {
  30. obiekt->klucz = klucz;
  31. strcpy(obiekt->wartosc, wartosc);
  32. tab[hash] = obiekt;
  33. return;
  34. }else
  35. {
  36. for(int i=1; i<50; i++)
  37. {
  38. if(tab[hash+(i^2)] == NULL)
  39. {
  40. tab[hash+(i^2)] = obiekt;
  41. return;
  42. }
  43. else if(tab[hash-(i^2)] == NULL)
  44. {
  45. tab[hash-(i^2)] = obiekt;
  46. return;
  47. }
  48. }
  49. }
  50. }
  51.  
  52. void szukaj(unsigned int klucz)
  53. {
  54. unsigned int hash = hashuj(klucz);
  55.  
  56. if(tab[hash] == NULL)
  57. {
  58. cout<<"-";
  59. return;
  60. }
  61. if(tab[hash]->klucz == klucz)
  62. {
  63. cout<<tab[hash]->wartosc;
  64. return;
  65. }
  66. else
  67. {
  68. for(int i=1; i<50; i++)
  69. {
  70. if(tab[hash+(i^2)]->klucz == klucz)
  71. {
  72. cout<<tab[hash+(i^2)]->wartosc;
  73. return;
  74. }else if(tab[hash+(i^2)] == NULL)
  75. cout<<"-";
  76. else if(tab[hash-(i^2)]->klucz == klucz)
  77. {
  78. cout<<tab[hash-(i^2)]->wartosc;
  79. return;
  80. }else if(tab[hash-(i^2)] == NULL)
  81. cout<<"-";
  82. }
  83. }
  84.  
  85.  
  86. }
  87.  
  88.  
  89. int main()
  90. {
  91. #ifndef ONLINE_JUDGE
  92. freopen("insert.in","r",stdin);
  93. freopen("out2.txt","w",stdout);
  94. #endif
  95. unsigned int klucz;
  96. char instrukcja = 'a', wartosc[21];
  97. //struct element obiekt;
  98. for(int i=0; instrukcja!=EOF; i++)
  99. {
  100. cin>>instrukcja;
  101. if(instrukcja == NULL)
  102. return 0;
  103. if(instrukcja == 'I')
  104. {
  105. scanf("%d", &klucz);
  106. scanf("%s", &wartosc);
  107. dodaj(klucz, wartosc);
  108. }else if(instrukcja == 'S')
  109. {
  110. cin>>klucz;
  111. szukaj(klucz);
  112. }
  113. }
  114.  
  115. return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement