Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# lab_5_elections
# lab_5_elections

Програма дозволяє ввести 5 кандидатів на виборах, виводить їхній відсоток голосів від загальної суми та визначає переможця.
82 changes: 82 additions & 0 deletions class.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class Candidate
{
public:
string name;
string surname;
int num_of_votes;
int percent_of_votes;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks better now)


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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for changing the function names

{
return (a.num_of_votes < b.num_of_votes);
}

class Elections
{
private:
vector<Candidate> 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;
}

};
38 changes: 38 additions & 0 deletions main5lab.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
#include <string>
#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;
}