Advertisement
luk_per

lab 9

Jan 9th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4.  
  5. int konstruktor=0;
  6. int konstruktor2=0;
  7. int pokaz=0;
  8.  
  9.  
  10. class Kwadrat
  11. {
  12. public:
  13.     char v;
  14.  
  15.     Kwadrat()
  16.     {
  17.         v=rand()%126;
  18.         cout<<v<<" ";
  19.         konstruktor2++;
  20.     }
  21. };
  22.  
  23. class Kulka
  24. {
  25. public:
  26.  
  27.     int v;
  28.  
  29.     Kulka()
  30.     {
  31.         v=rand()%100;
  32.         cout<<v<<" ";
  33.         konstruktor++;
  34.     }
  35.     ~Kulka()
  36.     {
  37.         cout<<"Usuwam kulke"<<endl;
  38.     }
  39. };
  40.  
  41. template <class K>
  42. class Wagonik
  43. {
  44. public:
  45.     K *reka;
  46.     Wagonik *lewy;
  47.     Wagonik *prawy;
  48.     Wagonik()
  49.     {
  50.  
  51.     }
  52.     Wagonik(K *nowa)
  53.     {
  54.         reka=nowa;
  55.         lewy=NULL;
  56.         prawy=NULL;
  57.     }
  58.  
  59.     ~Wagonik()
  60.     {
  61.         cout<<"Usuwam wagonik."<<endl;
  62.     }
  63.     void dodaj_wagonik(Wagonik *nowy)
  64.     {
  65.         if ((nowy->reka->v)<(reka->v))
  66.         {
  67.             if (lewy)
  68.                 lewy->dodaj_wagonik(nowy);
  69.             else
  70.                 lewy=nowy;
  71.         }
  72.         else
  73.         {
  74.             if(prawy)
  75.                 prawy->dodaj_wagonik(nowy);
  76.             else
  77.                 prawy=nowy;
  78.         }
  79.     }
  80.  
  81.     void show()
  82.     {
  83.         if (lewy!=NULL)
  84.             lewy->show();
  85.         cout<<reka->v<<" ";;
  86.         pokaz++;
  87.         if (prawy!=NULL)
  88.             prawy->show();
  89.     }
  90. };
  91.  
  92. template <class K>
  93. class Pojemnik
  94.  
  95. {
  96. public:
  97.     Wagonik<K> *root;
  98.     Wagonik<K> *nowy;
  99.  
  100.  
  101.     Pojemnik(K *nowy)
  102.     {
  103.         root=NULL;
  104.         nowy=NULL;
  105.     }
  106.  
  107.  
  108.     void dodaj(K *nowa)
  109.  
  110.     {
  111.         if (root==NULL)
  112.             root=new Wagonik<K>(nowa);
  113.         else
  114.         {
  115.             nowy=new Wagonik<K>(nowa);
  116.             root->dodaj_wagonik(nowy);
  117.         }
  118.     }
  119.  
  120. };
  121.  
  122.  
  123. int main()
  124.  
  125. {
  126.  
  127.     Kulka *nowa=NULL;
  128.     Pojemnik<Kulka> *P=new Pojemnik<Kulka> (nowa);
  129.  
  130.     for (int i=0; i<10; i++)
  131.     {
  132.         nowa=new Kulka();
  133.         P->dodaj(nowa);
  134.     }
  135.  
  136.     cout<<"Stworzylem "<<konstruktor<<" kulek."<<endl;
  137.  
  138.     P->root->show();
  139.  
  140.     cout<<"Pokazalem "<<pokaz<<" kulek."<<endl;
  141.  
  142.     Kwadrat *nowy=NULL;
  143.     Pojemnik<Kwadrat> *P2=new Pojemnik<Kwadrat> (nowy);
  144.  
  145.     for (int i=0; i<10; i++)
  146.     {
  147.         nowy=new Kwadrat();
  148.         P2->dodaj(nowy);
  149.     }
  150.  
  151.     cout<<"Stworzylem "<<konstruktor2<<" kwadratow."<<endl;
  152.  
  153.     P2->root->show();
  154.  
  155.     cout<<"Pokazalem "<<pokaz<<" kulek i kwadratow."<<endl;
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement