Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstring>
- using namespace std;
- class Participant {
- public:
- string surname;
- int points;
- Participant(string surname, int points) {
- this->surname = surname;
- this->points = points;
- }
- };
- void sort(vector<Participant> &participants) {
- for(int i = 0; i < participants.size(); i++) {
- Participant participant = participants[i];
- int maxIdx = i;
- for(int j = i + 1; j < participants.size(); j++) {
- int points = participants[j].points;
- int maxPoints = participants[maxIdx].points;
- if(points == maxPoints) {
- char surname[participants[j].surname.size() + 1];
- char maxSurname[participants[maxIdx].surname.size() + 1];
- strcpy(surname, participants[j].surname.c_str());
- strcpy(maxSurname, participants[maxIdx].surname.c_str());
- int result = strcmp(surname, maxSurname);
- if(result < 0)
- maxIdx = j;
- } else if(points > maxPoints)
- maxIdx = j;
- }
- if(maxIdx != i) {
- participants[i] = participants[maxIdx];
- participants[maxIdx] = participant;
- }
- }
- }
- void showResult(const vector<Participant> &participants) {
- for(Participant participant : participants)
- cout << participant.surname << ' ' << participant.points << endl;
- }
- vector<Participant> inputParticipants() {
- vector<Participant> participants;
- cout << "Введите фамилию участника и набранные им баллы" << endl;
- bool stop = false;
- while(!stop) {
- cin.clear();
- cout << "Введите фамилию участника, либо stop, чтобы прекратить ввод" << endl;
- string surname;
- cin >> surname;
- cin.clear();
- if(surname != "stop") {
- cout << "Введите количество баллов" << endl;
- int points;
- cin >> points;
- cin.clear();
- participants.push_back(*new Participant(surname, points));
- } else
- stop = true;
- }
- return participants;
- }
- int main() {
- cout << "Данная программа сортирует участников соревнования по убыванию количества баллов\n" << endl;
- vector<Participant> participants = inputParticipants();
- sort(participants);
- showResult(participants);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement