Advertisement
Alyks

Untitled

Feb 23rd, 2020
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. class Participant {
  8.     public:
  9.         string surname;
  10.         int points;
  11.         Participant(string surname, int points) {
  12.             this->surname = surname;
  13.             this->points = points;
  14.         }
  15. };
  16.  
  17. void sort(vector<Participant> &participants) {
  18.     for(int i = 0; i < participants.size(); i++) {
  19.         Participant participant = participants[i];
  20.         int maxIdx = i;
  21.         for(int j = i + 1; j < participants.size(); j++) {
  22.             int points = participants[j].points;
  23.             int maxPoints = participants[maxIdx].points;
  24.             if(points == maxPoints) {
  25.                 char surname[participants[j].surname.size() + 1];
  26.                 char maxSurname[participants[maxIdx].surname.size() + 1];
  27.                 strcpy(surname, participants[j].surname.c_str());
  28.                 strcpy(maxSurname, participants[maxIdx].surname.c_str());
  29.                 int result = strcmp(surname, maxSurname);
  30.                 if(result < 0)
  31.                     maxIdx = j;
  32.             } else if(points > maxPoints)
  33.                 maxIdx = j;
  34.         }
  35.         participants[i] = participants[maxIdx];
  36.         participants[maxIdx] = participant;
  37.     }
  38. }
  39.  
  40. void showResult(const vector<Participant> &participants) {
  41.     for(Participant participant : participants)
  42.         cout << participant.surname <<  ' ' << participant.points << endl;
  43. }
  44.  
  45. vector<Participant> inputParticipants() {
  46.     vector<Participant> participants;
  47.     cout << "Введите фамилию участника и набранные им баллы" << endl;
  48.     bool stop = false;
  49.     while(!stop) {
  50.         cin.clear();
  51.         cout << "Введите фамилию участника, либо stop, чтобы прекратить ввод" << endl;
  52.         string surname;
  53.         cin >> surname;
  54.         cin.clear();
  55.         if(surname != "stop") {
  56.             cout << "Введите количество баллов" << endl;
  57.             int points;
  58.             cin >> points;
  59.             cin.clear();
  60.             participants.push_back(*new Participant(surname, points));
  61.         } else
  62.             stop = true;
  63.     }
  64.     return participants;
  65. }
  66.  
  67.  
  68. int main() {
  69.     cout << "Данная программа сортирует участников соревнования по убыванию количества баллов\n" << endl;
  70.     vector<Participant> participants = inputParticipants();
  71.     sort(participants);
  72.     showResult(participants);
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement