Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- class Kandydat
- {
- friend class Okreg;
- string imie;
- string nazwisko;
- unsigned id;
- unsigned liczba_glosow;
- public:
- Kandydat(string, string, unsigned);
- void dodaj_glosy(unsigned);
- void wypisz_dane();
- };
- Kandydat::Kandydat(string name, string scndname, unsigned ident) : imie(name), nazwisko(scndname), id(ident), liczba_glosow(0) {}
- void Kandydat::dodaj_glosy(unsigned ile)
- {
- liczba_glosow += ile;
- }
- void Kandydat::wypisz_dane()
- {
- cout << id << ' ' << imie << ' ' << nazwisko << "\t\tliczba glosow: " << liczba_glosow << endl;
- }
- class Okreg
- {
- unsigned nr_okregu;
- vector<Kandydat*> kandydaci;
- unsigned ilu_kandydatow;
- public:
- Okreg(unsigned);
- ~Okreg();
- void dodaj_kandydata(string, string);
- bool dodaj_glosy(unsigned, unsigned);
- void wypisz_liste();
- void sortuj();
- Kandydat* zwyciezca();
- };
- Okreg::Okreg(unsigned numer) : nr_okregu(numer), ilu_kandydatow(0)
- {
- kandydaci.resize(0);
- }
- Okreg::~Okreg()
- {
- for(unsigned i = 0; i < ilu_kandydatow; ++i)
- delete kandydaci[i];
- cout << "Destruktor zakonczyl prace" << endl;
- }
- void Okreg::dodaj_kandydata(string imie, string nazwisko)
- {
- kandydaci.push_back(nullptr);
- kandydaci[kandydaci.size() - 1] = new Kandydat(imie, nazwisko, ilu_kandydatow);
- ++ilu_kandydatow;
- }
- bool Okreg::dodaj_glosy(unsigned numer, unsigned liczba_glosow)
- {
- // szukanie kandydata
- for(unsigned i = 0; i < ilu_kandydatow; ++i)
- if(kandydaci[i]->id == numer)
- {
- kandydaci[i]->dodaj_glosy(liczba_glosow);
- return true;
- }
- // brak kandydata:
- return false;
- }
- void Okreg::wypisz_liste()
- {
- for(unsigned i = 0; i < ilu_kandydatow; ++i)
- kandydaci[i]->wypisz_dane();
- }
- void Okreg::sortuj()
- {
- // sortowanie bąbelkowe:
- for(unsigned i = 0; i < ilu_kandydatow; ++i)
- for(unsigned j = 0; j < ilu_kandydatow - i - 1; ++j)
- if(kandydaci[j]->liczba_glosow < kandydaci[j + 1]->liczba_glosow)
- swap(kandydaci[j], kandydaci[j + 1]);
- }
- Kandydat* Okreg::zwyciezca()
- {
- unsigned max = kandydaci[0]->liczba_glosow;
- unsigned numer = 0;
- for(unsigned i = 1; i < ilu_kandydatow; ++i)
- if(kandydaci[i]->liczba_glosow > max)
- {
- max = kandydaci[i]->liczba_glosow;
- numer = i;
- }
- return kandydaci[numer];
- }
- int main()
- {
- Okreg nowy_okreg(1);
- nowy_okreg.dodaj_kandydata("Adam", "Adamski");
- nowy_okreg.dodaj_kandydata("Bozydar", "Bielan");
- nowy_okreg.dodaj_kandydata("Cecylia", "Calka");
- nowy_okreg.wypisz_liste();
- nowy_okreg.dodaj_glosy(0, 100);
- nowy_okreg.dodaj_glosy(1, 300);
- nowy_okreg.dodaj_glosy(0, 500);
- nowy_okreg.dodaj_glosy(2, 500);
- nowy_okreg.dodaj_glosy(3, 1000);
- nowy_okreg.sortuj();
- nowy_okreg.wypisz_liste();
- Kandydat* winner = nowy_okreg.zwyciezca();
- winner->wypisz_dane();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement