Czapek

Kopiec który chyba działa

Jun 3rd, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdio.h>
  4.  
  5.  
  6. int n=0;
  7. using namespace std;
  8. void dodaj_elemet_do_kopca(int *tab, int wartosc)
  9. {
  10.     cout << wartosc << endl;
  11.     int i,j;
  12.     i = n++;
  13.     j = (i - 1) / 2;
  14.  
  15.     while(i > 0 && tab[j] < wartosc)
  16.     {
  17.         tab[i] = tab[j];
  18.         i = j;
  19.         j = (i - 1) / 2;
  20.     }
  21.     tab[i] = wartosc;
  22. }
  23. void wyswietl_kopiec(int *T, int v)
  24. {
  25.   if(v < n)
  26.   {
  27.     wyswietl_kopiec(T, 2 * v + 2);
  28.     cout << T[v] << endl;
  29.     wyswietl_kopiec(T, 2 * v + 1);
  30.   }
  31. }
  32. int main()
  33. {
  34.     int *tab;
  35.     tab=new int;
  36.     int wybor;
  37.     srand(time(NULL));
  38.     do
  39.         {
  40.             cout << "Co zrobic?" << endl;
  41.             cout << "1. Dodaj losowy element" << endl;
  42.             cout << "2. Wyswietl drzewo" << endl;
  43.             cout << "4. Wyjscie" << endl;
  44.             cin >> wybor;
  45.             cout <<  endl << endl;
  46.             switch(wybor){
  47.             case 1: dodaj_elemet_do_kopca(tab, rand()%75); break;
  48.             case 2:
  49.                 {
  50.                    wyswietl_kopiec(tab, 0);
  51.                   break;
  52.                 }
  53.             case 4: break;
  54.             default : cout << "bledny wybor" << endl; break;
  55. }
  56.         }
  57.     while (wybor != 4);
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment