Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstring>
- #include <fstream>
- using namespace std;
- class Participant {
- public:
- string surname;
- int points;
- Participant(string surname, int points) {
- this->surname = surname;
- this->points = points;
- }
- };
- vector<string> split(string str, string delimiter) {
- vector<string> result;
- int pos = 0;
- while(pos != -1) {
- pos = str.find(delimiter);
- result.push_back(str.substr(0, pos));
- str.erase(0, pos + delimiter.length());
- }
- return result;
- }
- 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) {
- cout << "Результат:" << endl;
- for(Participant participant : participants)
- cout << participant.surname << ' ' << participant.points << endl;
- }
- vector<Participant> readFile(string filePath) {
- vector<Participant> participants;
- vector<string> data;
- ifstream inputFile;
- inputFile.open(filePath);
- if(inputFile) {
- string currStr;
- while(getline(inputFile, currStr)) {
- data = split(currStr, " ");
- string surname = data[0];
- int points = stoi(data[1]);
- participants.push_back(*new Participant(surname, points));
- }
- inputFile.close();
- } else
- cout << "Произошла ошибка при чтении файла. Убедитесь, что такой файл существует, либо проверьте имя файла" << endl;
- return participants;
- }
- void saveResult(vector<Participant> participants) {
- cout << "Хотите сохранить результат в файл? [Д/Н]" << endl;
- string choice;
- cin >> choice;
- bool notCorrect = true;
- while(notCorrect) {
- if(choice == "Д" || choice == "Н")
- notCorrect = false;
- else {
- cout << "Вы должны ввести либо [Д], либо [Н]" << endl;
- cin >> choice;
- }
- }
- bool saveToFile = choice == "Д";
- if(saveToFile) {
- ofstream outputFile;
- outputFile.open("output.txt");
- for (Participant participant : participants) {
- string str = participant.surname + ' ' + to_string(participant.points) + '\n';
- outputFile << str;
- }
- outputFile.close();
- cout << "Результат был сохранен в файл output.txt" << endl;
- }
- }
- vector<Participant> inputParticipants(bool fromFile) {
- vector<Participant> participants;
- if(fromFile) {
- cout << "Введите путь к файлу" << endl;
- string filePath;
- cin >> filePath;
- participants = readFile(filePath);
- } else {
- cout << "Введите фамилию участника и набранные им баллы" << endl;
- bool continueCycle = true;
- while(continueCycle) {
- cout << "Введите фамилию участника, либо stop, чтобы прекратить ввод" << endl;
- string surname;
- cin >> surname;
- if(surname == "stop")
- continueCycle = false;
- else {
- cout << "Введите количество баллов" << endl;
- int points;
- cin >> points;
- participants.push_back(*new Participant(surname, points));
- }
- }
- }
- return participants;
- }
- bool greeting() {
- cout << "Данная программа сортирует участников соревнования по убыванию количества баллов\n" << endl;
- cout << "Введите [C], если хотите ввести файл с консоли, или [F], если хотите сделать ввод из файла" << endl;
- bool notCorrect = true;
- string inputFrom;
- cin >> inputFrom;
- while(notCorrect) {
- if(inputFrom == "F" || inputFrom == "C")
- notCorrect = false;
- else {
- cout << "Вы должны ввести либо [C], либо [F]" << endl;
- cin >> inputFrom;
- }
- }
- bool inputFromFile = inputFrom == "F";
- return inputFromFile;
- }
- int main() {
- bool inputFromFile = greeting();
- vector<Participant> participants = inputParticipants(inputFromFile);
- sort(participants);
- showResult(participants);
- saveResult(participants);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement