Advertisement
Alyks

Untitled

Mar 1st, 2020
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. class Participant {
  9.     public:
  10.         string surname;
  11.         int points;
  12.         Participant(string surname, int points) {
  13.             this->surname = surname;
  14.             this->points = points;
  15.         }
  16. };
  17.  
  18. vector<string> split(string str, string delimiter) {
  19.     vector<string> result;
  20.     int pos = 0;
  21.     while(pos != -1) {
  22.         pos = str.find(delimiter);
  23.         result.push_back(str.substr(0, pos));
  24.         str.erase(0, pos + delimiter.length());
  25.     }
  26.     return result;
  27. }
  28.  
  29. void sort(vector<Participant> &participants) {
  30.     for(int i = 0; i < participants.size(); i++) {
  31.         Participant participant = participants[i];
  32.         int maxIdx = i;
  33.         for(int j = i + 1; j < participants.size(); j++) {
  34.             int points = participants[j].points;
  35.             int maxPoints = participants[maxIdx].points;
  36.             if(points == maxPoints) {
  37.                 char surname[participants[j].surname.size() + 1];
  38.                 char maxSurname[participants[maxIdx].surname.size() + 1];
  39.                 strcpy(surname, participants[j].surname.c_str());
  40.                 strcpy(maxSurname, participants[maxIdx].surname.c_str());
  41.                 int result = strcmp(surname, maxSurname);
  42.                 if(result < 0)
  43.                     maxIdx = j;
  44.             } else if(points > maxPoints)
  45.                 maxIdx = j;
  46.         }
  47.  
  48.         if(maxIdx != i) {
  49.             participants[i] = participants[maxIdx];
  50.             participants[maxIdx] = participant;
  51.         }
  52.     }
  53. }
  54.  
  55. void showResult(const vector<Participant> &participants) {
  56.     cout << "Результат:" << endl;
  57.     for(Participant participant : participants)
  58.         cout << participant.surname <<  ' ' << participant.points << endl;
  59. }
  60.  
  61. vector<Participant> readFile(string filePath) {
  62.     vector<Participant> participants;
  63.     vector<string> data;
  64.     ifstream inputFile;
  65.     inputFile.open(filePath);
  66.     if(inputFile) {
  67.         string currStr;
  68.         while(getline(inputFile, currStr)) {
  69.             data = split(currStr, " ");
  70.             string surname = data[0];
  71.             int points = stoi(data[1]);
  72.             participants.push_back(*new Participant(surname, points));
  73.         }
  74.         inputFile.close();
  75.     } else
  76.         cout << "Произошла ошибка при чтении файла. Убедитесь, что такой файл существует, либо проверьте имя файла" << endl;
  77.     return participants;
  78. }
  79.  
  80. void saveResult(vector<Participant> participants) {
  81.     cout << "Хотите сохранить результат в файл? [Д/Н]" << endl;
  82.     string choice;
  83.     cin >> choice;
  84.     bool notCorrect = true;
  85.     while(notCorrect) {
  86.         if(choice == "Д" || choice == "Н")
  87.             notCorrect = false;
  88.         else {
  89.             cout << "Вы должны ввести либо [Д], либо [Н]" << endl;
  90.             cin >> choice;
  91.         }
  92.     }
  93.     bool saveToFile = choice == "Д";
  94.     if(saveToFile) {
  95.         ofstream outputFile;
  96.         outputFile.open("output.txt");
  97.         for (Participant participant : participants) {
  98.             string str = participant.surname + ' ' + to_string(participant.points) + '\n';
  99.             outputFile << str;
  100.         }
  101.         outputFile.close();
  102.         cout << "Результат был сохранен в файл output.txt" << endl;
  103.     }
  104. }
  105.  
  106. vector<Participant> inputParticipants(bool fromFile) {
  107.     vector<Participant> participants;
  108.     if(fromFile) {
  109.         cout << "Введите путь к файлу" << endl;
  110.         string filePath;
  111.         cin >> filePath;
  112.         participants = readFile(filePath);
  113.     } else {
  114.         cout << "Введите фамилию участника и набранные им баллы" << endl;
  115.         bool continueCycle = true;
  116.         while(continueCycle) {
  117.             cout << "Введите фамилию участника, либо stop, чтобы прекратить ввод" << endl;
  118.             string surname;
  119.             cin >> surname;
  120.             if(surname == "stop")
  121.                 continueCycle = false;
  122.             else {
  123.                 cout << "Введите количество баллов" << endl;
  124.                 int points;
  125.                 cin >> points;
  126.                 participants.push_back(*new Participant(surname, points));
  127.             }
  128.         }
  129.     }
  130.     return participants;
  131. }
  132.  
  133. bool greeting() {
  134.     cout << "Данная программа сортирует участников соревнования по убыванию количества баллов\n" << endl;
  135.     cout << "Введите [C], если хотите ввести файл с консоли, или [F], если хотите сделать ввод из файла" << endl;
  136.     bool notCorrect = true;
  137.     string inputFrom;
  138.     cin >> inputFrom;
  139.     while(notCorrect) {
  140.         if(inputFrom == "F" || inputFrom == "C")
  141.             notCorrect = false;
  142.         else {
  143.             cout << "Вы должны ввести либо [C], либо [F]" << endl;
  144.             cin >> inputFrom;
  145.         }
  146.     }
  147.     bool inputFromFile = inputFrom == "F";
  148.     return inputFromFile;
  149. }
  150.  
  151. int main() {
  152.     bool inputFromFile = greeting();
  153.     vector<Participant> participants = inputParticipants(inputFromFile);
  154.     sort(participants);
  155.     showResult(participants);
  156.     saveResult(participants);
  157.     return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement