Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int konstruktor=0;
- int konstruktor2=0;
- int pokaz=0;
- class Kwadrat
- {
- public:
- char v;
- Kwadrat()
- {
- v=rand()%126;
- cout<<v<<" ";
- konstruktor2++;
- }
- };
- class Kulka
- {
- public:
- int v;
- Kulka()
- {
- v=rand()%100;
- cout<<v<<" ";
- konstruktor++;
- }
- ~Kulka()
- {
- cout<<"Usuwam kulke"<<endl;
- }
- };
- template <class K>
- class Wagonik
- {
- public:
- K *reka;
- Wagonik *lewy;
- Wagonik *prawy;
- Wagonik()
- {
- }
- Wagonik(K *nowa)
- {
- reka=nowa;
- lewy=NULL;
- prawy=NULL;
- }
- ~Wagonik()
- {
- cout<<"Usuwam wagonik."<<endl;
- }
- void dodaj_wagonik(Wagonik *nowy)
- {
- if ((nowy->reka->v)<(reka->v))
- {
- if (lewy)
- lewy->dodaj_wagonik(nowy);
- else
- lewy=nowy;
- }
- else
- {
- if(prawy)
- prawy->dodaj_wagonik(nowy);
- else
- prawy=nowy;
- }
- }
- void show()
- {
- if (lewy!=NULL)
- lewy->show();
- cout<<reka->v<<" ";;
- pokaz++;
- if (prawy!=NULL)
- prawy->show();
- }
- };
- template <class K>
- class Pojemnik
- {
- public:
- Wagonik<K> *root;
- Wagonik<K> *nowy;
- Pojemnik(K *nowy)
- {
- root=NULL;
- nowy=NULL;
- }
- void dodaj(K *nowa)
- {
- if (root==NULL)
- root=new Wagonik<K>(nowa);
- else
- {
- nowy=new Wagonik<K>(nowa);
- root->dodaj_wagonik(nowy);
- }
- }
- };
- int main()
- {
- Kulka *nowa=NULL;
- Pojemnik<Kulka> *P=new Pojemnik<Kulka> (nowa);
- for (int i=0; i<10; i++)
- {
- nowa=new Kulka();
- P->dodaj(nowa);
- }
- cout<<"Stworzylem "<<konstruktor<<" kulek."<<endl;
- P->root->show();
- cout<<"Pokazalem "<<pokaz<<" kulek."<<endl;
- Kwadrat *nowy=NULL;
- Pojemnik<Kwadrat> *P2=new Pojemnik<Kwadrat> (nowy);
- for (int i=0; i<10; i++)
- {
- nowy=new Kwadrat();
- P2->dodaj(nowy);
- }
- cout<<"Stworzylem "<<konstruktor2<<" kwadratow."<<endl;
- P2->root->show();
- cout<<"Pokazalem "<<pokaz<<" kulek i kwadratow."<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement