Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. // main.cpp
  2. #include <iostream>
  3. #include "katalog.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     Samochod* autka[6];
  10.     Katalog kat;
  11.     string rej;
  12.     unsigned moc;
  13.     double waga;
  14.     char rodzaj;
  15.     for(unsigned i = 0; i < 6; ++i)
  16.     {
  17.         cin >> rej >> moc >> waga >> rodzaj;
  18.         autka[i] = new Samochod(rej, moc, waga, rodzaj);
  19.         kat.dodaj(autka[i]);
  20.     }
  21.  
  22.     kat.pokaz();
  23.     for(unsigned i = 0; i < 6; ++i)
  24.         delete autka[i];
  25.  
  26.     return 0;
  27. }
  28.  
  29. // katalog.cpp
  30. #include "katalog.h"
  31.  
  32. Katalog::Katalog() : ile(0)
  33. {
  34. }
  35.  
  36. Samochod* Katalog::korzen()
  37. {
  38.     return kopiec[1];
  39. }
  40.  
  41. void Katalog::dodaj(Samochod* car)
  42. {
  43.     unsigned value = car->get_moc();
  44.     int child = ++ile;
  45.     int parent = child/2;
  46.     while (parent && kopiec[parent]->get_moc() > value)
  47.     {
  48.         kopiec[child] = kopiec[parent];
  49.         child = parent;
  50.         parent /= 2;
  51.     }
  52.     kopiec[child] = car;
  53. }
  54.  
  55. void Katalog::usun_korzen()
  56. {
  57.     Samochod* last = kopiec[ile--];
  58.     int x = 1;
  59.     int c = minChild(1);
  60.     while (c && kopiec[c]->get_moc() < last->get_moc())
  61.     {
  62.         kopiec[x] = kopiec[c];
  63.         x = c;
  64.         c = minChild(c);  // returns 0 if no children
  65.     }
  66.     kopiec[x] = last;
  67. }
  68.  
  69. int Katalog::minChild(int parent)
  70. {
  71.     int left = 2*parent;
  72.     int right = left + 1;
  73.     if (left > ile)
  74.         return 0;
  75.     if (right > ile || kopiec[left]->get_moc() < kopiec[right]->get_moc())
  76.         return left;
  77.     return right;
  78. }
  79.  
  80. void Katalog::pokaz()
  81. {
  82.     while(ile > 0)
  83.     {
  84.         korzen()->drukuj();
  85.         usun_korzen();
  86.     }
  87. }
  88.  
  89. // katalog.h
  90. #include "samochod.h"
  91.  
  92. class Katalog
  93. {
  94.     Samochod* kopiec[10];
  95.     int ile;
  96. public:
  97.     Katalog();
  98.     void dodaj(Samochod*);
  99.     Samochod* korzen();
  100.     void usun_korzen();
  101.     int minChild(int);
  102.     void pokaz();
  103. };
  104.  
  105. // samochod.h
  106. #include <iostream>
  107.  
  108. using namespace std;
  109.  
  110. class Samochod
  111. {
  112.     string rejestr;
  113.     unsigned moc;
  114.     double waga;
  115.     char rodzaj;
  116. public:
  117.     Samochod(string, unsigned, double, char);
  118.     void set_rejestr(string);
  119.     void set_moc(unsigned);
  120.     void set_waga(double);
  121.     void set_rodzaj(char);
  122.     void drukuj();
  123.     const string get_rejestr();
  124.     const unsigned get_moc();
  125.     const double get_waga();
  126.     const char get_rodzaj();
  127. };
  128.  
  129. // samochod.cpp
  130. #include "samochod.h"
  131.  
  132. Samochod::Samochod(string r, unsigned m, double w, char rodz) : rejestr(r), moc(m), waga(w), rodzaj(rodz)
  133. {
  134. }
  135.  
  136. void Samochod::set_rejestr(string rej)
  137. {
  138.     rejestr = rej;
  139. }
  140.  
  141. void Samochod::set_moc(unsigned m)
  142. {
  143.     moc = m;
  144. }
  145.  
  146. void Samochod::set_waga(double w)
  147. {
  148.     waga = w;
  149. }
  150.  
  151. void Samochod::set_rodzaj(char r)
  152. {
  153.     rodzaj = r;
  154. }
  155.  
  156. void Samochod::drukuj()
  157. {
  158.     cout << rejestr << " " << moc << " ";
  159. }
  160.  
  161. const string Samochod::get_rejestr()
  162. {
  163.     return rejestr;
  164. }
  165.  
  166. const unsigned Samochod::get_moc()
  167. {
  168.     return moc;
  169. }
  170.  
  171. const double Samochod::get_waga()
  172. {
  173.     return waga;
  174. }
  175.  
  176. const char Samochod::get_rodzaj()
  177. {
  178.     return rodzaj;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement