Advertisement
Guest User

Untitled

a guest
Feb 16th, 2025
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. struct Athlete {
  8. string surname;
  9. int min;
  10. int sec;
  11. int tent;
  12. int hund;
  13. };
  14.  
  15. // Функция для расчёта общего времени в условных единицах
  16. double totalTime(const Athlete& athlete) {
  17. return athlete.min * 6000 + athlete.sec * 100 + athlete.tent * 10 + athlete.hund;
  18. }
  19.  
  20. int main() {
  21. system("chcp 1251 > nul");
  22.  
  23. const int numAthletes = 10;
  24. Athlete* athletes = new Athlete[numAthletes];
  25.  
  26. // Записываем данные спортсменов в файл
  27. ofstream outfile("athletes.txt");
  28. if (!outfile) {
  29. cout << "Ошибка открытия файла для записи!" << endl;
  30. delete[] athletes;
  31. return 1;
  32. }
  33.  
  34. cout << "Ввод данных спортсменов:" << endl;
  35. for (int i = 0; i < numAthletes; ++i) {
  36. cout << "Введите фамилию спортсмена #" << (i + 1) << ": ";
  37. cin >> athletes[i].surname;
  38. cout << "Введите время (минуты, секунды, десятые, сотые) для " << athletes[i].surname << ": ";
  39. cin >> athletes[i].min >> athletes[i].sec >> athletes[i].tent >> athletes[i].hund;
  40. outfile << athletes[i].surname << " "
  41. << athletes[i].min << " "
  42. << athletes[i].sec << " "
  43. << athletes[i].tent << " "
  44. << athletes[i].hund << "\n";
  45. }
  46. outfile.close();
  47.  
  48. // Читаем данные из файла с помощью ifstream и заполняем вектор
  49. ifstream infile("athletes.txt");
  50. if (!infile) {
  51. cout << "Ошибка открытия файла для чтения!" << endl;
  52. delete[] athletes;
  53. return 1;
  54. }
  55. vector<Athlete> athletesVector;
  56. Athlete temp;
  57. while (infile >> temp.surname >> temp.min >> temp.sec >> temp.tent >> temp.hund) {
  58. athletesVector.push_back(temp);
  59. }
  60. infile.close();
  61.  
  62. // Сортируем вектор по возрастанию общего времени
  63. sort(athletesVector.begin(), athletesVector.end(), [](const Athlete& a, const Athlete& b) {
  64. return totalTime(a) < totalTime(b);
  65. });
  66.  
  67. // Записываем 6 лучших спортсменов в отдельный файл
  68. ofstream bestOutfile("best_athletes.txt");
  69. if (!bestOutfile) {
  70. cerr << "Ошибка открытия файла для записи лучших спортсменов!" << endl;
  71. delete[] athletes;
  72. return 1;
  73. }
  74. for (size_t i = 0; i < 6 && i < athletesVector.size(); ++i) {
  75. bestOutfile << athletesVector[i].surname << " "
  76. << athletesVector[i].min << " "
  77. << athletesVector[i].sec << " "
  78. << athletesVector[i].tent << " "
  79. << athletesVector[i].hund << "\n";
  80. }
  81. bestOutfile.close();
  82.  
  83. delete[] athletes;
  84. return 0;
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement