MaksNew

Untitled

Nov 22nd, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <filesystem>
  4. #include <sstream>
  5. #include <string>
  6. #include <set>
  7. #include <iomanip>
  8. #include <Windows.h>
  9. #ifdef max
  10. #undef max
  11. #endif
  12. using namespace std;
  13.  
  14. struct slist
  15. {
  16.     int score;
  17.     string name;
  18. };
  19.  
  20. int getsize(int size)
  21. {
  22.     bool isNotCorrect;
  23.     string inputLine;
  24.     cout << "Введите количество участников: " << endl;
  25.     do
  26.     {
  27.         isNotCorrect = false;
  28.         try
  29.         {
  30.             cin >> inputLine;
  31.             size = stoi(inputLine);
  32.         }
  33.         catch (...)
  34.         {
  35.             isNotCorrect = true;
  36.             cout << "Введите целое число!" << endl;
  37.         }
  38.         if ((size < 3) && !isNotCorrect)
  39.         {
  40.             cout << "Минимальное количество участников: 3" << endl;
  41.             isNotCorrect = true;
  42.         }
  43.     } while (isNotCorrect);
  44.     return size;
  45. }
  46.  
  47. struct slist* getListFromConsole(int size, struct slist* aol)
  48. {
  49.     bool isNotCorrect;
  50.     string inputLine;
  51.     string buf;
  52.     for (int i = 0; i < size; i++)
  53.     {
  54.         do
  55.         {
  56.             isNotCorrect = false;
  57.             cout << "Введите фамилию участника: " << endl;
  58.             cin >> aol[i].name;
  59.             buf = aol[i].name;
  60.             for (int j = 0; j < buf.length(); j++)
  61.             {
  62.                 if (!(('А' <= buf[j]) && (buf[j] <= 'Я') || ('а' <= buf[j]) && (buf[j] <= 'я')))
  63.                 {
  64.                     isNotCorrect = true;
  65.                 }
  66.             }
  67.             if (isNotCorrect == true)
  68.             {
  69.                 cout << "Фамилия должна состоять из русских букв" << endl;
  70.             }
  71.         } while (isNotCorrect);
  72.         do
  73.         {
  74.             isNotCorrect = false;
  75.             cout << "Введите счёт участника: " << endl;
  76.             try
  77.             {
  78.                 cin >> inputLine;
  79.                 aol[i].score = stoi(inputLine);
  80.             }
  81.             catch (...)
  82.             {
  83.                 isNotCorrect = true;
  84.                 cout << "Введите целое число!" << endl;
  85.             }
  86.             if ((aol[i].score < 0) && !isNotCorrect)
  87.             {
  88.                 cout << "Введите неотрицательное число!" << endl;
  89.                 isNotCorrect = true;
  90.             }
  91.         } while (isNotCorrect);
  92.     }
  93.     return aol;
  94. }
  95.  
  96. void printListBeforeSorting(struct slist* aol, int size)
  97. {
  98.     cout << endl;
  99.     cout << "Список участников до сортировки: " << endl;
  100.     for (int i = 0; i < size; i++)
  101.     {
  102.         cout << aol[i].name << " " << aol[i].score << endl;
  103.     }
  104.     cout << endl;
  105. }
  106.  
  107. void sorting(struct slist*& aol, int size)
  108. {
  109.     int bufi;
  110.     string bufs;
  111.     for (int i = 0; i < size-1; i++)
  112.     {
  113.         for (int j = i + 1; j < size; j++)
  114.         {
  115.             if (aol[i].score < aol[j].score)
  116.             {
  117.                 bufi= aol[j].score;
  118.                 aol[j].score = aol[i].score;
  119.                 aol[i].score = bufi;
  120.                 bufs = aol[i].name;
  121.                 aol[i].name = aol[j].name;
  122.                 aol[j].name = bufs;
  123.             }
  124.         }
  125.     }
  126. }
  127.  
  128. void top3Output(struct slist*& aol, int size)
  129. {
  130.     int ind, bufOne, bufTwo;
  131.     ind = 0;
  132.     cout << "Участники, показавшие 3 лучших результата: " << endl;
  133.     cout << aol[0].name << ' ' << aol[0].score << endl;
  134.     for (int i = 0; i < size; i++)
  135.     {
  136.         if ((aol[i].score == aol[0].score) and (aol[i].name != aol[0].name))
  137.         {
  138.             cout << aol[i].name << ' ' << aol[i].score << endl;
  139.             ind = i;
  140.         }
  141.     }
  142.     bufOne = ind;
  143.     cout << aol[ind+1].name << ' ' << aol[ind+1].score << endl;
  144.     for (int i = ind + 1; i < size; i++)
  145.     {
  146.         if((aol[i].score == aol[ind + 1].score) and (aol[i].name != aol[ind + 1].name))
  147.         {
  148.             cout << aol[i].name << ' ' << aol[i].score << endl;
  149.             ind = i;
  150.         }
  151.     }
  152.     bufTwo = ind;
  153.     if (bufOne != bufTwo)
  154.     {
  155.         cout << aol[ind + 1].name << ' ' << aol[ind + 1].score << endl;
  156.         for (int i = ind + 1; i < size; i++)
  157.         {
  158.             if ((aol[i].score == aol[ind + 1].score) && (aol[i].name != aol[ind + 1].name))
  159.             {
  160.                 cout << aol[i].name << ' ' << aol[i].score << endl;
  161.             }
  162.         }
  163.     }
  164.     else
  165.     {
  166.         cout << aol[ind + 2].name << ' ' << aol[ind + 2].score << endl;
  167.         for (int i = ind + 2; i < size; i++)
  168.         {
  169.             if((aol[i].score == aol[ind + 2].score) && (aol[i].name != aol[ind + 2].name))
  170.             {
  171.                 cout << aol[i].name << ' ' << aol[i].score << endl;
  172.             }
  173.         }
  174.     }
  175. }
  176.  
  177. bool isFileCorrect(int size, string path)
  178. {
  179.     int i;
  180.     bool isCorrect;
  181.     string bufstring, bts, nums;
  182.     fstream listFile(path, fstream::in);
  183.     isCorrect = true;
  184.     i = 1;
  185.     while (!listFile.eof() && isCorrect)
  186.     {
  187.         if (i % 2 == 1)
  188.         {
  189.             getline(listFile, bufstring);
  190.             stringstream strstream;
  191.             strstream << bufstring;
  192.             while (!strstream.eof() && isCorrect)
  193.             {
  194.                 strstream >> bts;
  195.                 for (int j = 0; j < bts.length(); j++)
  196.                 {
  197.                     if (!(('А' <= bts[j]) && (bts[j] <= 'Я') || ('а' <= bts[j]) && (bts[j] <= 'я')) && isCorrect)
  198.                     {
  199.                         isCorrect = false;
  200.                     }
  201.                 }
  202.             }
  203.         }
  204.         else
  205.         {
  206.             getline(listFile, bufstring);
  207.             stringstream strstream;
  208.             strstream << bufstring;
  209.             while (!strstream.eof() && isCorrect)
  210.             {
  211.                 strstream >> nums;
  212.                 for (int j = 0; j < nums.length(); j++)
  213.                 {
  214.                     if (!(('0' <= nums[j]) && (nums[j] <= '9')) && isCorrect)
  215.                     {
  216.                         isCorrect = false;
  217.                     }
  218.                 }
  219.             }
  220.         }
  221.         i++;
  222.     }
  223.     if (i != size*2+1)
  224.     {
  225.         isCorrect = false;
  226.     }
  227.     listFile.close();
  228.     return isCorrect;
  229.  
  230. }
  231.  
  232. string getFilePath(int size)
  233. {
  234.     string path;
  235.     bool isIncorrect;
  236.     isIncorrect = true;
  237.     do
  238.     {
  239.         cout << "Введите абсолютный путь к файлу: " << endl;
  240.         cin >> path;
  241.         if (!std::filesystem::exists(path))
  242.         {
  243.             cout << "Файл не найден. Проверьте введённый путь." << endl;
  244.         }
  245.         else
  246.         {
  247.             if (isFileCorrect(size, path))
  248.             {
  249.                 isIncorrect = false;
  250.             }
  251.             else
  252.             {
  253.                 cout << "Данные в файле некорректны\n";
  254.             }
  255.         }
  256.     } while (isIncorrect);
  257.     return path;
  258. }
  259.  
  260. struct slist* getListfromFile(struct slist* aol, int size)
  261. {
  262.     string path;
  263.     path = getFilePath(size);
  264.     fstream listFile(path, fstream::in);
  265.     for (int i = 0; i < size; i++)
  266.     {
  267.         getline(listFile, aol[i].name);
  268.         listFile >> aol[i].score;
  269.         listFile.clear();
  270.         listFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  271.     }
  272.     listFile.close();
  273.     return aol;
  274. }
  275.  
  276. int getInputType(int& value)
  277. {
  278.     bool isNotCorrect;
  279.     string inputLine;
  280.     cout << "Выберите способ ввода предложения: 1 - файл, 2 - консоль. " << endl;
  281.     do
  282.     {
  283.         isNotCorrect = false;
  284.         try
  285.         {
  286.             cin >> inputLine;
  287.             value = stoi(inputLine);
  288.         }
  289.         catch (...)
  290.         {
  291.             isNotCorrect = true;
  292.             cout << "Введите одно из предложенных чисел" << endl;
  293.         }
  294.         if ((((value < 1) || (value > 2))) && !isNotCorrect)
  295.         {
  296.             cout << "Выберете одно из двух значений! 1 для ввода из файла, или 2 для ввода консоли!" << endl;
  297.             isNotCorrect = true;
  298.         }
  299.     } while (isNotCorrect);
  300.     return value;
  301. }
  302.  
  303. struct slist* getChoiceType(int value, struct slist* aol, string path, int size)
  304. {
  305.     value = getInputType(value);
  306.     if (value == 1)
  307.     {
  308.         aol = getListfromFile(aol, size);
  309.     }
  310.     if (value == 2)
  311.     {
  312.         aol = getListFromConsole(size, aol);
  313.     }
  314.     return aol;
  315. }
  316.  
  317. void printListAfterSorting(struct slist* aol, int size)
  318. {
  319.     cout << "Список участников после сортировки: " << endl;
  320.     for (int i = 0; i < size; i++)
  321.     {
  322.         cout << aol[i].name << " " << aol[i].score << endl;
  323.     }
  324.     cout << endl;
  325. }
  326.  
  327. void printSentenceAfterEditing(string sentence)
  328. {
  329.     if (sentence.length() < 1)
  330.     {
  331.         sentence = "Ни один символ не прошёл проверку! От вашей строки ничего не осталось!";
  332.     }
  333.     else
  334.     {
  335.         cout << "Исправленное предложение:" << endl;
  336.     }
  337.     cout << sentence << endl;
  338. }
  339.  
  340. string output()
  341. {
  342.     string patho;
  343.     bool isIncorrect;
  344.     isIncorrect = true;
  345.     do
  346.     {
  347.         cout << "Введите директорию, в которую хотите сохранить ответ:" << endl;
  348.         cin >> patho;
  349.         if (filesystem::exists(patho))
  350.         {
  351.             isIncorrect = false;
  352.         }
  353.         else
  354.         {
  355.             cout << "Такой директории не существует. Попробуйте ещё раз." << endl;
  356.         }
  357.  
  358.     } while (isIncorrect);
  359.     return patho;
  360. }
  361.  
  362. void saveListToFile(struct slist*& aol, int size)
  363. {
  364.     string path;
  365.     int ind, bufOne, bufTwo;
  366.     ind = 0;
  367.     path = output();
  368.     ofstream fout;
  369.     fout.open(path + "\output.txt");
  370.     fout << "Участники, показавшие 3 лучших результата: " << endl;
  371.     fout << aol[0].name << ' ' << aol[0].score << endl;
  372.     for (int i = 0; i < size; i++)
  373.     {
  374.         if ((aol[i].score == aol[0].score) and (aol[i].name != aol[0].name))
  375.         {
  376.             fout << aol[i].name << ' ' << aol[i].score << endl;
  377.             ind = i;
  378.         }
  379.     }
  380.     bufOne = ind;
  381.     fout << aol[ind + 1].name << ' ' << aol[ind + 1].score << endl;
  382.     for (int i = ind + 1; i < size; i++)
  383.     {
  384.         if ((aol[i].score == aol[ind + 1].score) and (aol[i].name != aol[ind + 1].name))
  385.         {
  386.             fout << aol[i].name << ' ' << aol[i].score << endl;
  387.             ind = i;
  388.         }
  389.     }
  390.     bufTwo = ind;
  391.     if (bufOne != bufTwo)
  392.     {
  393.         fout << aol[ind + 1].name << ' ' << aol[ind + 1].score << endl;
  394.         for (int i = ind + 1; i < size; i++)
  395.         {
  396.             if ((aol[i].score == aol[ind + 1].score) && (aol[i].name != aol[ind + 1].name))
  397.             {
  398.                 fout << aol[i].name << ' ' << aol[i].score << endl;
  399.             }
  400.         }
  401.     }
  402.     else
  403.     {
  404.         fout << aol[ind + 2].name << ' ' << aol[ind + 2].score << endl;
  405.         for (int i = ind + 2; i < size; i++)
  406.         {
  407.             if ((aol[i].score = aol[ind + 2].score) && (aol[i].name != aol[ind + 2].name))
  408.             {
  409.                 fout << aol[i].name << ' ' << aol[i].score << endl;
  410.             }
  411.         }
  412.     }
  413.     fout.close();
  414.     cout << "Результат сохранён по указанному пути.";
  415.  
  416. }
  417.  
  418.  
  419. int main()
  420. {
  421.     SetConsoleCP(1251);
  422.     SetConsoleOutputCP(1251);
  423.     setlocale(LC_ALL, "ru");
  424.     int size;
  425.     int value;
  426.     string path;
  427.     size = 0;
  428.     value = 0;
  429.     size = getsize(size);
  430.     struct slist* aol = new struct slist[size];
  431.     aol = getChoiceType(value, aol, path, size);
  432.     printListBeforeSorting(aol, size);
  433.     sorting(aol, size);
  434.     printListAfterSorting(aol, size);
  435.     top3Output(aol, size);
  436.     saveListToFile(aol, size);
  437. }
  438.  
Advertisement
Add Comment
Please, Sign In to add comment