diff --git a/README.md b/README.md index 814f83e..e24df3b 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ -# lab_5_elections \ No newline at end of file +# lab_5_elections + +Програма дозволяє ввести 5 кандидатів на виборах, виводить їхній відсоток голосів від загальної суми та визначає переможця. \ No newline at end of file diff --git a/class.h b/class.h new file mode 100644 index 0000000..19a566a --- /dev/null +++ b/class.h @@ -0,0 +1,82 @@ +#include +#include +#include +#include + +using namespace std; + +class Candidate +{ +public: + string name; + string surname; + int num_of_votes; + int percent_of_votes; + + void SetCandidate() + { + cout << "Name of the candidate: "; + cin >> name; + cout << "Surname of the candidate: "; + cin >> surname; + cout << "Number of votes of the candidate: "; + cin >> num_of_votes; + cout << endl; + } + + void Print() + { + cout << name << " " << surname << " has " << percent_of_votes << "% of votes" << endl; + } + +}; + +bool Comp(Candidate &a, Candidate &b) +{ + return (a.num_of_votes < b.num_of_votes); +} + +class Elections +{ +private: + vector candidates; + int summ; + +public: + + void AddCandidate (Candidate &c) + { + candidates.push_back(c); + } + + void VotesSumm () + { + summ = 0; + for (auto i : candidates) { + summ += i.num_of_votes; + } + } + + void VotesPercent () + { + for (auto &i : candidates) { + i.percent_of_votes = (i.num_of_votes*100)/summ; + } + } + + + void Print() + { + for (auto i : candidates) { + i.Print(); + } + } + + + void GetWinner() + { + cout << endl; + cout << "The winner is " << max_element(candidates.begin(), candidates.end(), Comp)->name << " " << max_element(candidates.begin(), candidates.end(), Comp)->surname; + } + +}; \ No newline at end of file diff --git a/main5lab.cpp b/main5lab.cpp new file mode 100644 index 0000000..74039c5 --- /dev/null +++ b/main5lab.cpp @@ -0,0 +1,38 @@ +#include +#include +#include "class.h" + +using namespace std; + +int main() +{ + + Candidate First; + First.SetCandidate(); + + Candidate Second; + Second.SetCandidate(); + + Candidate Third; + Third.SetCandidate(); + + Candidate Fourth; + Fourth.SetCandidate(); + + Candidate Fifth; + Fifth.SetCandidate(); + + Elections Rayon; + Rayon.AddCandidate(First); + Rayon.AddCandidate(Second); + Rayon.AddCandidate(Third); + Rayon.AddCandidate(Fourth); + Rayon.AddCandidate(Fifth); + Rayon.VotesSumm(); + Rayon.VotesPercent(); + Rayon.Print(); + + Rayon.GetWinner(); + + return 0; +} \ No newline at end of file