Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. class Kopiec
  2. {
  3. public:
  4. int liczbaElementow;
  5. private:
  6. int *w;
  7. string gornyRog, dolnyRog, pion;
  8. public:
  9. Kopiec()
  10. {
  11. gornyRog = dolnyRog = pion = " ";
  12. gornyRog[0] = 218; gornyRog[1] = 196;
  13. dolnyRog[0] = 192; dolnyRog[1] = 196;
  14. pion[0] = 179;
  15. liczbaElementow = 0;
  16. w = new int[liczbaElementow + 1];
  17. }
  18. ~Kopiec()
  19. {
  20. liczbaElementow = 0;
  21. delete[] w;
  22.  
  23. }
  24. void zapelnijLosowo(int ile)
  25. {
  26. liczbaElementow = ile;
  27. w = new int[liczbaElementow + 1];
  28. for (int i = 1; i <= liczbaElementow; i++)
  29. w[i] = ((rand() % 2000) - 1000);
  30. budujKopiec();
  31. }
  32.  
  33. void zbudujZPliku(string nazwa)
  34. {
  35. string s;
  36. int i = 0;
  37. int *t;
  38. nazwa = nazwa + ".txt";
  39. ifstream plik(nazwa);
  40.  
  41. if (!plik)
  42. {
  43. cout << "Nie mozna otworzyc pliku" << endl;;
  44.  
  45. }
  46. else
  47. {
  48. getline(plik, s);
  49. int n = atoi(s.c_str());
  50. liczbaElementow = n;
  51. t = new int[liczbaElementow];
  52. while (!plik.eof())
  53. {
  54. if (i <= liczbaElementow)
  55. {
  56. getline(plik, s);
  57. t[i] = atoi(s.c_str());
  58. i++;
  59. }
  60. else break;
  61. }
  62. plik.close();
  63. for (i; i <= liczbaElementow; i++)
  64. {
  65. t[i] = 0;
  66. }
  67. w = t;
  68. budujKopiec();
  69. }
  70. }
  71. void budujKopiec()
  72. {
  73. for (int i = 2; i <= liczbaElementow; i++)
  74. {
  75. int pozycjaElementu, pozycjaPrzodka, element;
  76. pozycjaElementu = i;
  77. pozycjaPrzodka = (pozycjaElementu) / 2;
  78. element = w[i];
  79. while ((pozycjaPrzodka > 0) && (w[pozycjaPrzodka] < element))
  80. {
  81. w[pozycjaElementu] = w[pozycjaPrzodka];
  82. pozycjaElementu = pozycjaPrzodka;
  83. pozycjaPrzodka = (pozycjaElementu) / 2;
  84. }
  85. w[pozycjaElementu] = element;
  86. }
  87. }
  88. void wyswietl(string tekst1, string tekst2, int nrWezla)
  89. {
  90. if (liczbaElementow == 0)
  91. cout << "Kopiec jest pusty" << endl;
  92. string tekst;
  93.  
  94. if (nrWezla <= liczbaElementow)
  95. {
  96. tekst = tekst1;
  97. if (tekst2 == gornyRog)
  98. tekst[tekst.length() - 2] = ' ';
  99.  
  100. wyswietl(tekst + pion, gornyRog, 2 * nrWezla + 1);
  101.  
  102. tekst = tekst.substr(0, tekst1.length() - 2);
  103.  
  104. cout << tekst << tekst2 << w[nrWezla] << endl;
  105.  
  106. tekst = tekst1;
  107. if (tekst2 == dolnyRog)
  108. tekst[tekst.length() - 2] = ' ';
  109. wyswietl(tekst + pion, dolnyRog, 2 * nrWezla);
  110. }
  111. }
  112. bool szukaj(int liczbaDoZnalezienia)
  113. {
  114. bool czyJest = false;
  115. for (int i = 1; i <= liczbaElementow; i++)
  116. {
  117. if (liczbaDoZnalezienia == w[i])
  118. {
  119. czyJest = true;
  120. break;
  121. }
  122. }
  123. return czyJest;
  124.  
  125. }
  126. void dodaj(int liczbaDoDodania)
  127. {
  128. licznik = 0;
  129. czasStart();
  130. liczbaElementow++;
  131. w[liczbaElementow] = liczbaDoDodania;
  132. int pozycjaNowegoElementu = liczbaElementow;
  133. while ((w[pozycjaNowegoElementu] > w[pozycjaNowegoElementu / 2]) && (pozycjaNowegoElementu > 1))
  134. {
  135. int temp = w[pozycjaNowegoElementu / 2];
  136. w[pozycjaNowegoElementu / 2] = w[pozycjaNowegoElementu];
  137. w[pozycjaNowegoElementu] = temp;
  138. pozycjaNowegoElementu = (pozycjaNowegoElementu / 2);
  139. }
  140. pobierzCzas();
  141. }
  142. void relokuj()
  143. {
  144. int *t = new int[liczbaElementow + 1];
  145. for (int i = 1; i <= liczbaElementow; i++)
  146. {
  147. t[i] = w[i];
  148. }
  149. w = new int[liczbaElementow + 1];
  150. for (int i = 1; i <= liczbaElementow; i++)
  151. {
  152. w[i] = t[i];
  153. }
  154. delete[]t;
  155. }
  156. void usun(int liczbaDoUsuniecia)
  157. {
  158.  
  159. if (liczbaElementow == 0)
  160. cout << "Kopiec jest pusty, nie mozna nic usunac" << endl;
  161. else
  162. {
  163. if (!szukaj(liczbaDoUsuniecia))
  164. cout << "W kopcu nie ma takiej liczby, nie mozna usunac!" << endl;
  165. else
  166. {
  167. licznik = 0;
  168. czasStart();
  169. int i;
  170. for (i = 1; i <= liczbaElementow; i++)
  171. {
  172. if (w[i] == liczbaDoUsuniecia) break;
  173. }
  174. if (i == liczbaElementow)
  175. {
  176. w[i] = NULL;
  177. liczbaElementow--;
  178. pobierzCzas();
  179. }
  180. if (i < liczbaElementow)
  181. {
  182. w[i] = w[liczbaElementow];
  183. w[liczbaElementow] = NULL;
  184. liczbaElementow--;
  185. if (2 * i > liczbaElementow)
  186. {
  187. while (i > 1)
  188. {
  189. if (w[i] > w[i / 2])
  190. {
  191. int temp = w[i / 2];
  192. w[i / 2] = w[i];
  193. w[i] = temp;
  194. }
  195. i = i / 2;
  196. }
  197. }
  198. else
  199. {
  200. while (i <= liczbaElementow / 2)
  201. {
  202. if ((w[i] < (w[2 * i])) || (w[i] < (w[2 * i + 1])))
  203. {
  204. if (w[2 * i] > w[2 * i + 1])
  205. {
  206. int temp = w[i];
  207. w[i] = w[2 * i];
  208. w[2 * i] = temp;
  209. i = 2 * i;
  210. }
  211. else
  212. {
  213. int temp = w[i];
  214. w[i] = w[2 * i + 1];
  215. w[2 * i + 1] = temp;
  216. i = 2 * i + 1;
  217. }
  218. }
  219. else break;
  220. }
  221.  
  222. }
  223. }
  224. pobierzCzas();
  225. relokuj();
  226. }
  227. }
  228. }
  229. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement