Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <filesystem>
- #include <sstream>
- #include <string>
- #include <set>
- #include <iomanip>
- #include <Windows.h>
- #ifdef max
- #undef max
- #endif
- using namespace std;
- struct slist
- {
- int score;
- string name;
- };
- int getsize(int size)
- {
- bool isNotCorrect;
- string inputLine;
- cout << "Введите количество участников: " << endl;
- do
- {
- isNotCorrect = false;
- try
- {
- cin >> inputLine;
- size = stoi(inputLine);
- }
- catch (...)
- {
- isNotCorrect = true;
- cout << "Введите целое число!" << endl;
- }
- if ((size < 3) && !isNotCorrect)
- {
- cout << "Минимальное количество участников: 3" << endl;
- isNotCorrect = true;
- }
- } while (isNotCorrect);
- return size;
- }
- struct slist* getListFromConsole(int size, struct slist* aol)
- {
- bool isNotCorrect;
- string inputLine;
- string buf;
- for (int i = 0; i < size; i++)
- {
- do
- {
- isNotCorrect = false;
- cout << "Введите фамилию участника: " << endl;
- cin >> aol[i].name;
- buf = aol[i].name;
- for (int j = 0; j < buf.length(); j++)
- {
- if (!(('А' <= buf[j]) && (buf[j] <= 'Я') || ('а' <= buf[j]) && (buf[j] <= 'я')))
- {
- isNotCorrect = true;
- }
- }
- if (isNotCorrect == true)
- {
- cout << "Фамилия должна состоять из русских букв" << endl;
- }
- } while (isNotCorrect);
- do
- {
- isNotCorrect = false;
- cout << "Введите счёт участника: " << endl;
- try
- {
- cin >> inputLine;
- aol[i].score = stoi(inputLine);
- }
- catch (...)
- {
- isNotCorrect = true;
- cout << "Введите целое число!" << endl;
- }
- if ((aol[i].score < 0) && !isNotCorrect)
- {
- cout << "Введите неотрицательное число!" << endl;
- isNotCorrect = true;
- }
- } while (isNotCorrect);
- }
- return aol;
- }
- void printListBeforeSorting(struct slist* aol, int size)
- {
- cout << endl;
- cout << "Список участников до сортировки: " << endl;
- for (int i = 0; i < size; i++)
- {
- cout << aol[i].name << " " << aol[i].score << endl;
- }
- cout << endl;
- }
- void sorting(struct slist*& aol, int size)
- {
- int bufi;
- string bufs;
- for (int i = 0; i < size-1; i++)
- {
- for (int j = i + 1; j < size; j++)
- {
- if (aol[i].score < aol[j].score)
- {
- bufi= aol[j].score;
- aol[j].score = aol[i].score;
- aol[i].score = bufi;
- bufs = aol[i].name;
- aol[i].name = aol[j].name;
- aol[j].name = bufs;
- }
- }
- }
- }
- void top3Output(struct slist*& aol, int size)
- {
- int ind, bufOne, bufTwo;
- ind = 0;
- cout << "Участники, показавшие 3 лучших результата: " << endl;
- cout << aol[0].name << ' ' << aol[0].score << endl;
- for (int i = 0; i < size; i++)
- {
- if ((aol[i].score == aol[0].score) and (aol[i].name != aol[0].name))
- {
- cout << aol[i].name << ' ' << aol[i].score << endl;
- ind = i;
- }
- }
- bufOne = ind;
- cout << aol[ind+1].name << ' ' << aol[ind+1].score << endl;
- for (int i = ind + 1; i < size; i++)
- {
- if((aol[i].score == aol[ind + 1].score) and (aol[i].name != aol[ind + 1].name))
- {
- cout << aol[i].name << ' ' << aol[i].score << endl;
- ind = i;
- }
- }
- bufTwo = ind;
- if (bufOne != bufTwo)
- {
- cout << aol[ind + 1].name << ' ' << aol[ind + 1].score << endl;
- for (int i = ind + 1; i < size; i++)
- {
- if ((aol[i].score == aol[ind + 1].score) && (aol[i].name != aol[ind + 1].name))
- {
- cout << aol[i].name << ' ' << aol[i].score << endl;
- }
- }
- }
- else
- {
- cout << aol[ind + 2].name << ' ' << aol[ind + 2].score << endl;
- for (int i = ind + 2; i < size; i++)
- {
- if((aol[i].score == aol[ind + 2].score) && (aol[i].name != aol[ind + 2].name))
- {
- cout << aol[i].name << ' ' << aol[i].score << endl;
- }
- }
- }
- }
- bool isFileCorrect(int size, string path)
- {
- int i;
- bool isCorrect;
- string bufstring, bts, nums;
- fstream listFile(path, fstream::in);
- isCorrect = true;
- i = 1;
- while (!listFile.eof() && isCorrect)
- {
- if (i % 2 == 1)
- {
- getline(listFile, bufstring);
- stringstream strstream;
- strstream << bufstring;
- while (!strstream.eof() && isCorrect)
- {
- strstream >> bts;
- for (int j = 0; j < bts.length(); j++)
- {
- if (!(('А' <= bts[j]) && (bts[j] <= 'Я') || ('а' <= bts[j]) && (bts[j] <= 'я')) && isCorrect)
- {
- isCorrect = false;
- }
- }
- }
- }
- else
- {
- getline(listFile, bufstring);
- stringstream strstream;
- strstream << bufstring;
- while (!strstream.eof() && isCorrect)
- {
- strstream >> nums;
- for (int j = 0; j < nums.length(); j++)
- {
- if (!(('0' <= nums[j]) && (nums[j] <= '9')) && isCorrect)
- {
- isCorrect = false;
- }
- }
- }
- }
- i++;
- }
- if (i != size*2+1)
- {
- isCorrect = false;
- }
- listFile.close();
- return isCorrect;
- }
- string getFilePath(int size)
- {
- string path;
- bool isIncorrect;
- isIncorrect = true;
- do
- {
- cout << "Введите абсолютный путь к файлу: " << endl;
- cin >> path;
- if (!std::filesystem::exists(path))
- {
- cout << "Файл не найден. Проверьте введённый путь." << endl;
- }
- else
- {
- if (isFileCorrect(size, path))
- {
- isIncorrect = false;
- }
- else
- {
- cout << "Данные в файле некорректны\n";
- }
- }
- } while (isIncorrect);
- return path;
- }
- struct slist* getListfromFile(struct slist* aol, int size)
- {
- string path;
- path = getFilePath(size);
- fstream listFile(path, fstream::in);
- for (int i = 0; i < size; i++)
- {
- getline(listFile, aol[i].name);
- listFile >> aol[i].score;
- listFile.clear();
- listFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- }
- listFile.close();
- return aol;
- }
- int getInputType(int& value)
- {
- bool isNotCorrect;
- string inputLine;
- cout << "Выберите способ ввода предложения: 1 - файл, 2 - консоль. " << endl;
- do
- {
- isNotCorrect = false;
- try
- {
- cin >> inputLine;
- value = stoi(inputLine);
- }
- catch (...)
- {
- isNotCorrect = true;
- cout << "Введите одно из предложенных чисел" << endl;
- }
- if ((((value < 1) || (value > 2))) && !isNotCorrect)
- {
- cout << "Выберете одно из двух значений! 1 для ввода из файла, или 2 для ввода консоли!" << endl;
- isNotCorrect = true;
- }
- } while (isNotCorrect);
- return value;
- }
- struct slist* getChoiceType(int value, struct slist* aol, string path, int size)
- {
- value = getInputType(value);
- if (value == 1)
- {
- aol = getListfromFile(aol, size);
- }
- if (value == 2)
- {
- aol = getListFromConsole(size, aol);
- }
- return aol;
- }
- void printListAfterSorting(struct slist* aol, int size)
- {
- cout << "Список участников после сортировки: " << endl;
- for (int i = 0; i < size; i++)
- {
- cout << aol[i].name << " " << aol[i].score << endl;
- }
- cout << endl;
- }
- void printSentenceAfterEditing(string sentence)
- {
- if (sentence.length() < 1)
- {
- sentence = "Ни один символ не прошёл проверку! От вашей строки ничего не осталось!";
- }
- else
- {
- cout << "Исправленное предложение:" << endl;
- }
- cout << sentence << endl;
- }
- string output()
- {
- string patho;
- bool isIncorrect;
- isIncorrect = true;
- do
- {
- cout << "Введите директорию, в которую хотите сохранить ответ:" << endl;
- cin >> patho;
- if (filesystem::exists(patho))
- {
- isIncorrect = false;
- }
- else
- {
- cout << "Такой директории не существует. Попробуйте ещё раз." << endl;
- }
- } while (isIncorrect);
- return patho;
- }
- void saveListToFile(struct slist*& aol, int size)
- {
- string path;
- int ind, bufOne, bufTwo;
- ind = 0;
- path = output();
- ofstream fout;
- fout.open(path + "\output.txt");
- fout << "Участники, показавшие 3 лучших результата: " << endl;
- fout << aol[0].name << ' ' << aol[0].score << endl;
- for (int i = 0; i < size; i++)
- {
- if ((aol[i].score == aol[0].score) and (aol[i].name != aol[0].name))
- {
- fout << aol[i].name << ' ' << aol[i].score << endl;
- ind = i;
- }
- }
- bufOne = ind;
- fout << aol[ind + 1].name << ' ' << aol[ind + 1].score << endl;
- for (int i = ind + 1; i < size; i++)
- {
- if ((aol[i].score == aol[ind + 1].score) and (aol[i].name != aol[ind + 1].name))
- {
- fout << aol[i].name << ' ' << aol[i].score << endl;
- ind = i;
- }
- }
- bufTwo = ind;
- if (bufOne != bufTwo)
- {
- fout << aol[ind + 1].name << ' ' << aol[ind + 1].score << endl;
- for (int i = ind + 1; i < size; i++)
- {
- if ((aol[i].score == aol[ind + 1].score) && (aol[i].name != aol[ind + 1].name))
- {
- fout << aol[i].name << ' ' << aol[i].score << endl;
- }
- }
- }
- else
- {
- fout << aol[ind + 2].name << ' ' << aol[ind + 2].score << endl;
- for (int i = ind + 2; i < size; i++)
- {
- if ((aol[i].score = aol[ind + 2].score) && (aol[i].name != aol[ind + 2].name))
- {
- fout << aol[i].name << ' ' << aol[i].score << endl;
- }
- }
- }
- fout.close();
- cout << "Результат сохранён по указанному пути.";
- }
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- setlocale(LC_ALL, "ru");
- int size;
- int value;
- string path;
- size = 0;
- value = 0;
- size = getsize(size);
- struct slist* aol = new struct slist[size];
- aol = getChoiceType(value, aol, path, size);
- printListBeforeSorting(aol, size);
- sorting(aol, size);
- printListAfterSorting(aol, size);
- top3Output(aol, size);
- saveListToFile(aol, size);
- }
Advertisement
Add Comment
Please, Sign In to add comment