Advertisement
Alyks

Untitled

Feb 23rd, 2020
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 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.  
  36.         if(maxIdx != i) {
  37.             participants[i] = participants[maxIdx];
  38.             participants[maxIdx] = participant;
  39.         }
  40.     }
  41. }
  42.  
  43. void showResult(const vector<Participant> &participants) {
  44.     for(Participant participant : participants)
  45.         cout << participant.surname <<  ' ' << participant.points << endl;
  46. }
  47.  
  48. vector<Participant> inputParticipants() {
  49.     vector<Participant> participants;
  50.     cout << "Введите фамилию участника и набранные им баллы" << endl;
  51.     bool stop = false;
  52.     while(!stop) {
  53.         cin.clear();
  54.         cout << "Введите фамилию участника, либо stop, чтобы прекратить ввод" << endl;
  55.         string surname;
  56.         cin >> surname;
  57.         cin.clear();
  58.         if(surname != "stop") {
  59.             cout << "Введите количество баллов" << endl;
  60.             int points;
  61.             cin >> points;
  62.             cin.clear();
  63.             participants.push_back(*new Participant(surname, points));
  64.         } else
  65.             stop = true;
  66.     }
  67.     return participants;
  68. }
  69.  
  70.  
  71. int main() {
  72.     cout << "Данная программа сортирует участников соревнования по убыванию количества баллов\n" << endl;
  73.     vector<Participant> participants = inputParticipants();
  74.     sort(participants);
  75.     showResult(participants);
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement