Advertisement
MeehoweCK

Untitled

Nov 24th, 2020
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. class Kandydat
  7. {
  8.     friend class Okreg;
  9.     string imie;
  10.     string nazwisko;
  11.     unsigned id;
  12.     unsigned liczba_glosow;
  13. public:
  14.     Kandydat(string, string, unsigned);
  15.     void dodaj_glosy(unsigned);
  16.     void wypisz_dane();
  17. };
  18.  
  19. Kandydat::Kandydat(string name, string scndname, unsigned ident) : imie(name), nazwisko(scndname), id(ident), liczba_glosow(0) {}
  20.  
  21. void Kandydat::dodaj_glosy(unsigned ile)
  22. {
  23.     liczba_glosow += ile;
  24. }
  25.  
  26. void Kandydat::wypisz_dane()
  27. {
  28.     cout << id << ' ' << imie << ' ' << nazwisko << "\t\tliczba glosow: " << liczba_glosow << endl;
  29. }
  30.  
  31. class Okreg
  32. {
  33.     unsigned nr_okregu;
  34.     vector<Kandydat*> kandydaci;
  35.     unsigned ilu_kandydatow;
  36. public:
  37.     Okreg(unsigned);
  38.     ~Okreg();
  39.     void dodaj_kandydata(string, string);
  40.     bool dodaj_glosy(unsigned, unsigned);
  41.     void wypisz_liste();
  42.     void sortuj();
  43.     Kandydat* zwyciezca();
  44. };
  45.  
  46. Okreg::Okreg(unsigned numer) : nr_okregu(numer), ilu_kandydatow(0)
  47. {
  48.     kandydaci.resize(0);
  49. }
  50.  
  51. Okreg::~Okreg()
  52. {
  53.     for(unsigned i = 0; i < ilu_kandydatow; ++i)
  54.         delete kandydaci[i];
  55.     cout << "Destruktor zakonczyl prace" << endl;
  56. }
  57.  
  58. void Okreg::dodaj_kandydata(string imie, string nazwisko)
  59. {
  60.     kandydaci.push_back(nullptr);
  61.     kandydaci[kandydaci.size() - 1] = new Kandydat(imie, nazwisko, ilu_kandydatow);
  62.     ++ilu_kandydatow;
  63. }
  64.  
  65. bool Okreg::dodaj_glosy(unsigned numer, unsigned liczba_glosow)
  66. {
  67.     // szukanie kandydata
  68.     for(unsigned i = 0; i < ilu_kandydatow; ++i)
  69.         if(kandydaci[i]->id == numer)
  70.         {
  71.             kandydaci[i]->dodaj_glosy(liczba_glosow);
  72.             return true;
  73.         }
  74.     // brak kandydata:
  75.     return false;
  76. }
  77.  
  78. void Okreg::wypisz_liste()
  79. {
  80.     for(unsigned i = 0; i < ilu_kandydatow; ++i)
  81.         kandydaci[i]->wypisz_dane();
  82. }
  83.  
  84. void Okreg::sortuj()
  85. {
  86.     // sortowanie bąbelkowe:
  87.     for(unsigned i = 0; i < ilu_kandydatow; ++i)
  88.         for(unsigned j = 0; j < ilu_kandydatow - i - 1; ++j)
  89.             if(kandydaci[j]->liczba_glosow < kandydaci[j + 1]->liczba_glosow)
  90.                 swap(kandydaci[j], kandydaci[j + 1]);
  91. }
  92.  
  93. Kandydat* Okreg::zwyciezca()
  94. {
  95.     unsigned max = kandydaci[0]->liczba_glosow;
  96.     unsigned numer = 0;
  97.  
  98.     for(unsigned i = 1; i < ilu_kandydatow; ++i)
  99.         if(kandydaci[i]->liczba_glosow > max)
  100.         {
  101.             max = kandydaci[i]->liczba_glosow;
  102.             numer = i;
  103.         }
  104.  
  105.     return kandydaci[numer];
  106. }
  107.  
  108. int main()
  109. {
  110.     Okreg nowy_okreg(1);
  111.     nowy_okreg.dodaj_kandydata("Adam", "Adamski");
  112.     nowy_okreg.dodaj_kandydata("Bozydar", "Bielan");
  113.     nowy_okreg.dodaj_kandydata("Cecylia", "Calka");
  114.     nowy_okreg.wypisz_liste();
  115.     nowy_okreg.dodaj_glosy(0, 100);
  116.     nowy_okreg.dodaj_glosy(1, 300);
  117.     nowy_okreg.dodaj_glosy(0, 500);
  118.     nowy_okreg.dodaj_glosy(2, 500);
  119.     nowy_okreg.dodaj_glosy(3, 1000);
  120.     nowy_okreg.sortuj();
  121.     nowy_okreg.wypisz_liste();
  122.     Kandydat* winner = nowy_okreg.zwyciezca();
  123.     winner->wypisz_dane();
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement