Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Tacka3D
  5. {public:
  6.  
  7. int x, y, z;
  8.  
  9. Tacka3D(int x = 0, int y = 0, int z = 0)
  10. {
  11. this->x = x;
  12. this->y = y;
  13. this->z = z;
  14. }
  15. };
  16.  
  17. class OtvorenoHesiranje
  18. {
  19. int brojac;
  20. int max_vel;
  21. Tacka3D* niz;
  22. bool* zauzeto;
  23.  
  24. bool Pun() { return brojac == max_vel; }
  25. bool Prazan() { return brojac == 0; }
  26. int hashFunkcija(Tacka3D k, int i=0)
  27. {
  28. int pozicija = (int)(sqrt(pow((k.x + k.y + k.z), 2)) + i * sqrt(pow(k.x, 2) / 3 + pow(k.y, 2) / 3 + pow(k.z, 2) / 3)) % max_vel;
  29. return pozicija;
  30. }
  31.  
  32. public:
  33. OtvorenoHesiranje(int max = 10)
  34. {
  35. brojac = 0;
  36. max_vel = max;
  37. niz = new Tacka3D[max];
  38. zauzeto = new bool[max];
  39. for (int i = 0; i < max; i++)
  40. {
  41. zauzeto[i] = false;
  42. }
  43. }
  44.  
  45. bool Dodaj(Tacka3D t)
  46. {
  47. if (Pun())
  48. return false;
  49. int pozicija = hashFunkcija(t);
  50. int brojPokusaja = 0;
  51.  
  52. while (zauzeto[pozicija])
  53. {
  54. brojPokusaja++;
  55. pozicija = hashFunkcija(t, brojPokusaja);
  56. }
  57.  
  58. niz[pozicija] = t;
  59. brojac++;
  60. zauzeto[pozicija] = true;
  61. return true;
  62. }
  63.  
  64. bool Ukloni(Tacka3D t)
  65. {
  66. if (Prazan())
  67. return false;
  68.  
  69. int pozicija = hashFunkcija(t);
  70. int brojPokusaja = 0;
  71.  
  72. while (brojPokusaja<max_vel)
  73. {
  74. if (zauzeto[pozicija] && niz[pozicija].x == t.x && niz[pozicija].y == t.y && niz[pozicija].z == t.z)
  75. {
  76. zauzeto[pozicija] = false;
  77. brojac--;
  78. return true;
  79. }
  80.  
  81. brojPokusaja++;
  82. pozicija = hashFunkcija(t, brojPokusaja);
  83. }
  84.  
  85. return false;
  86. }
  87.  
  88. bool Pretrazi(Tacka3D t)
  89. {
  90. if (Prazan())
  91. return false;
  92.  
  93. int pozicija = hashFunkcija(t);
  94. int brojPokusaja = 0;
  95.  
  96. while (brojPokusaja < max_vel)
  97. {
  98. if (zauzeto[pozicija] && niz[pozicija].x == t.x && niz[pozicija].y == t.y && niz[pozicija].z == t.z)
  99. {
  100. return true;
  101. }
  102.  
  103. brojPokusaja++;
  104. pozicija = hashFunkcija(t, brojPokusaja);
  105. }
  106.  
  107. return false;
  108. }
  109. };
  110.  
  111.  
  112. int main()
  113. {
  114. Tacka3D t1(1, 2, 3), t2(4, 5, 6), t3(7, 8, 9);
  115. OtvorenoHesiranje h;
  116. if (h.Dodaj(t1))
  117. cout << "Uspjesno" << endl;
  118. if (h.Dodaj(t2))
  119. cout << "Uspjesno" << endl;
  120. if (h.Dodaj(t3))
  121. cout << "Uspjesno" << endl;
  122.  
  123. if (h.Ukloni(t3))
  124. cout << "Uspjesno" << endl;
  125.  
  126. if (!h.Pretrazi(t3))
  127. cout << "Nije uspjesno" << endl;
  128. system("pause>0");
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement