Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.cpp
- #include <iostream>
- #include "katalog.h"
- using namespace std;
- int main()
- {
- Samochod* autka[6];
- Katalog kat;
- string rej;
- unsigned moc;
- double waga;
- char rodzaj;
- for(unsigned i = 0; i < 6; ++i)
- {
- cin >> rej >> moc >> waga >> rodzaj;
- autka[i] = new Samochod(rej, moc, waga, rodzaj);
- kat.dodaj(autka[i]);
- }
- kat.pokaz();
- for(unsigned i = 0; i < 6; ++i)
- delete autka[i];
- return 0;
- }
- // katalog.cpp
- #include "katalog.h"
- Katalog::Katalog() : ile(0)
- {
- }
- Samochod* Katalog::korzen()
- {
- return kopiec[1];
- }
- void Katalog::dodaj(Samochod* car)
- {
- unsigned value = car->get_moc();
- int child = ++ile;
- int parent = child/2;
- while (parent && kopiec[parent]->get_moc() > value)
- {
- kopiec[child] = kopiec[parent];
- child = parent;
- parent /= 2;
- }
- kopiec[child] = car;
- }
- void Katalog::usun_korzen()
- {
- Samochod* last = kopiec[ile--];
- int x = 1;
- int c = minChild(1);
- while (c && kopiec[c]->get_moc() < last->get_moc())
- {
- kopiec[x] = kopiec[c];
- x = c;
- c = minChild(c); // returns 0 if no children
- }
- kopiec[x] = last;
- }
- int Katalog::minChild(int parent)
- {
- int left = 2*parent;
- int right = left + 1;
- if (left > ile)
- return 0;
- if (right > ile || kopiec[left]->get_moc() < kopiec[right]->get_moc())
- return left;
- return right;
- }
- void Katalog::pokaz()
- {
- while(ile > 0)
- {
- korzen()->drukuj();
- usun_korzen();
- }
- }
- // katalog.h
- #include "samochod.h"
- class Katalog
- {
- Samochod* kopiec[10];
- int ile;
- public:
- Katalog();
- void dodaj(Samochod*);
- Samochod* korzen();
- void usun_korzen();
- int minChild(int);
- void pokaz();
- };
- // samochod.h
- #include <iostream>
- using namespace std;
- class Samochod
- {
- string rejestr;
- unsigned moc;
- double waga;
- char rodzaj;
- public:
- Samochod(string, unsigned, double, char);
- void set_rejestr(string);
- void set_moc(unsigned);
- void set_waga(double);
- void set_rodzaj(char);
- void drukuj();
- const string get_rejestr();
- const unsigned get_moc();
- const double get_waga();
- const char get_rodzaj();
- };
- // samochod.cpp
- #include "samochod.h"
- Samochod::Samochod(string r, unsigned m, double w, char rodz) : rejestr(r), moc(m), waga(w), rodzaj(rodz)
- {
- }
- void Samochod::set_rejestr(string rej)
- {
- rejestr = rej;
- }
- void Samochod::set_moc(unsigned m)
- {
- moc = m;
- }
- void Samochod::set_waga(double w)
- {
- waga = w;
- }
- void Samochod::set_rodzaj(char r)
- {
- rodzaj = r;
- }
- void Samochod::drukuj()
- {
- cout << rejestr << " " << moc << " ";
- }
- const string Samochod::get_rejestr()
- {
- return rejestr;
- }
- const unsigned Samochod::get_moc()
- {
- return moc;
- }
- const double Samochod::get_waga()
- {
- return waga;
- }
- const char Samochod::get_rodzaj()
- {
- return rodzaj;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement