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>
- 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 <= 0) && !IsNotCorrect)
- {
- cout << "Введите положительное число!" << 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.ignore();
- //getline(cin, aol[i].name);
- 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 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;
- }
- }
- }
- }
- bool isFileCorrect(int size, string path)
- {
- int i;
- bool IsNotCorrect;
- string bufstring;
- fstream listFile(path, fstream::in);
- IsNotCorrect = true;
- i = 1;
- while (!listFile.eof() && IsNotCorrect)
- {
- if (i % 2 == 1)
- {
- getline(listFile, bufstring);
- stringstream strstream;
- strstream << bufstring;
- while (!strstream.eof() && IsNotCorrect)
- {
- for (int j = 0; j < bufstring.length(); j++)
- {
- if (!(('А' <= bufstring[j]) && (bufstring[j] <= 'Я') || ('а' <= bufstring[j]) && (bufstring[j] <= 'я')) && IsNotCorrect)
- {
- IsNotCorrect = false;
- }
- }
- }
- }
- else
- {
- getline(listFile, bufstring);
- stringstream strstream;
- strstream << bufstring;
- while (!strstream.eof() && IsNotCorrect)
- {
- for (int j = 0; j < bufstring.length(); j++)
- {
- if (!(('0' <= bufstring[j]) && (bufstring[j] <= '9')) && IsNotCorrect)
- {
- IsNotCorrect = false;
- }
- }
- }
- }
- i++;
- }
- if (i != size*2+1)
- {
- IsNotCorrect = true;
- }
- listFile.close();
- return IsNotCorrect;
- }
- string getFilePath(int size)
- {
- string path;
- bool isIncorrect;
- do
- {
- cout << "Введите абсолютный путь к файлу: " << endl;
- cin >> path;
- isIncorrect = true;
- if (!std::filesystem::exists(path))
- {
- cout << "Файл не найден. Проверьте введённый путь." << endl;
- }
- else
- {
- if (isFileCorrect(size, path))
- {
- isIncorrect = false;
- }
- else
- {
- cout << "Данные в файле некорректны\n";
- }
- }
- } while (isIncorrect);
- cout << "didifidg";
- 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 printSentenceBeforeEditing(string sentence)
- {
- cout << "Исходное предложение: " << endl;
- cout << sentence << endl;
- cout << endl;
- }
- string editing(string sentence)
- {
- int i;
- i = 0;
- while (i < sentence.length())
- {
- if (!(('0' <= sentence[i]) && (sentence[i] <= '9') || ('A' <= sentence[i]) && (sentence[i] <='Z') || ('a' <= sentence[i]) && (sentence[i] <= 'z') || (sentence[i] == ' ')))
- {
- sentence.erase(i, 1);
- i--;
- }
- i++;
- }
- return sentence;
- }
- 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 saveResultToFile(string sentence)
- {
- string path;
- path = Output();
- ofstream fout;
- fout.open(path + "\output.txt");
- fout << sentence << endl;
- fout.close();
- cout << "Результат сохранён по указанному пути.";
- }
- int main()
- {
- //SetConsoleCP(1251);
- //SetConsoleOutputCP(1251);
- setlocale(LC_ALL, "ru");
- int size = 0;
- string path;
- int value;
- value = 0;
- size = getsize(size);
- struct slist* aol = new struct slist[size];
- aol = getChoiceType(value, aol, path, size);
- sorting(aol, size);
- for (int i = 0; i < size; i++)
- {
- cout << aol[i].name << " " << aol[i].score << endl;
- }
- //printSentenceBeforeEditing(sentence);
- //sentence = editing(sentence);
- //printSentenceAfterEditing(sentence);
- //saveResultToFile(sentence);
- }
Advertisement
Add Comment
Please, Sign In to add comment