hpnq

имифи практика

Jul 17th, 2024 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstring>
  5.  
  6. // Константы для размеров массивов
  7. const int MAX_ATHLETES = 100;
  8. const int MAX_COACHES = 20;
  9. const int MAX_JUDGES = 20;
  10. const int MAX_COMPETITIONS = 50;
  11. const int MAX_ATHLETES_PER_COACH = 10;
  12. const int MAX_JUDGES_PER_COMPETITION = 5;
  13. const int MAX_COMPETITIONS_PER_JUDGE = 10;
  14. const int MAX_RESULTS_PER_COMPETITION = 100;
  15. using namespace std;
  16. // Структуры данных
  17. struct Athlete {
  18.     string fullName;
  19.     string rank;
  20.     int birthYear;
  21.     string team;
  22.     string achievements;
  23. };
  24.  
  25. struct Coach {
  26.     string fullName;
  27.     string title;
  28.     Athlete* athletes[MAX_ATHLETES_PER_COACH];
  29.     int athletecnt;
  30. };
  31.  
  32. struct Judge {
  33.     string fullName;
  34.     string category;
  35.     string competitions[MAX_COMPETITIONS_PER_JUDGE];
  36.     int competitioncnt;
  37. };
  38.  
  39. struct Competition {
  40.     string date;
  41.     string status;
  42.     string venue;
  43.     Judge* judges[MAX_JUDGES_PER_COMPETITION];
  44.     int judgecnt;
  45.     struct Result {
  46.         Athlete* athlete;
  47.         string result;
  48.     } results[MAX_RESULTS_PER_COMPETITION];
  49.     int resultcnt;
  50. };
  51.  
  52. class SportsFederation {
  53. private:
  54.     Athlete athletes[MAX_ATHLETES];
  55.     int athletecnt;
  56.  
  57.     Coach coaches[MAX_COACHES];
  58.     int coachcnt;
  59.  
  60.     Judge judges[MAX_JUDGES];
  61.     int judgecnt;
  62.  
  63.     Competition competitions[MAX_COMPETITIONS];
  64.     int competitioncnt;
  65.  
  66.     void loadAthletes() {
  67.         ifstream file("Спортсмены.txt");
  68.         athletecnt = 0;
  69.         if (file.is_open()) {
  70.             while (file >> athletes[athletecnt].fullName >> athletes[athletecnt].rank >> athletes[athletecnt].birthYear
  71.                         >> athletes[athletecnt].team >> athletes[athletecnt].achievements) {
  72.                 athletecnt++;
  73.             }
  74.             file.close();
  75.         }
  76.     }
  77.  
  78.     void loadCoaches() {
  79.         ifstream file("Тренеры.txt");
  80.         coachcnt = 0;
  81.         if (file.is_open()) {
  82.             while (file >> coaches[coachcnt].fullName >> coaches[coachcnt].title) {
  83.                 coaches[coachcnt].athletecnt = 0;
  84.                 string athleteName;
  85.                 while (file >> athleteName) {
  86.                     for (int i = 0; i < athletecnt; ++i) {
  87.                         if (athletes[i].fullName == athleteName) {
  88.                             coaches[coachcnt].athletes[coaches[coachcnt].athletecnt++] = &athletes[i];
  89.                             break;
  90.                         }
  91.                     }
  92.                 }
  93.                 coachcnt++;
  94.             }
  95.             file.close();
  96.         }
  97.     }
  98.  
  99.     void loadJudges() {
  100.         ifstream file("Судьи.txt");
  101.         judgecnt = 0;
  102.         if (file.is_open()) {
  103.             while (file >> judges[judgecnt].fullName >> judges[judgecnt].category) {
  104.                 judges[judgecnt].competitioncnt = 0;
  105.                 while (file >> judges[judgecnt].competitions[judges[judgecnt].competitioncnt]) {
  106.                     judges[judgecnt].competitioncnt++;
  107.                 }
  108.                 judgecnt++;
  109.             }
  110.             file.close();
  111.         }
  112.     }
  113.  
  114.     void saveAthletes() {
  115.         ofstream file("Спортсмены.txt");
  116.         if (file.is_open()) {
  117.             for (int i = 0; i < athletecnt; ++i) {
  118.                 file << athletes[i].fullName << " " << athletes[i].rank << " " << athletes[i].birthYear << " "
  119.                      << athletes[i].team << " " << athletes[i].achievements << endl;
  120.             }
  121.             file.close();
  122.         }
  123.     }
  124.  
  125.     void saveCoaches() {
  126.         ofstream file("Тренеры.txt");
  127.         if (file.is_open()) {
  128.             for (int i = 0; i < coachcnt; ++i) {
  129.                 file << coaches[i].fullName << " " << coaches[i].title;
  130.                 for (int j = 0; j < coaches[i].athletecnt; ++j) {
  131.                     file << " " << coaches[i].athletes[j]->fullName;
  132.                 }
  133.                 file << endl;
  134.             }
  135.             file.close();
  136.         }
  137.     }
  138.  
  139.     void saveJudges() {
  140.         ofstream file("Судьи.txt");
  141.         if (file.is_open()) {
  142.             for (int i = 0; i < judgecnt; ++i) {
  143.                 file << judges[i].fullName << " " << judges[i].category;
  144.                 for (int j = 0; j < judges[i].competitioncnt; ++j) {
  145.                     file << " " << judges[i].competitions[j];
  146.                 }
  147.                 file << endl;
  148.             }
  149.             file.close();
  150.         }
  151.     }
  152.  
  153. public:
  154.     SportsFederation() : athletecnt(0), coachcnt(0), judgecnt(0), competitioncnt(0) {
  155.         loadAthletes();
  156.         loadCoaches();
  157.         loadJudges();
  158.     }
  159.  
  160.     void addAthlete(const Athlete& athlete) {
  161.         if (athletecnt < MAX_ATHLETES) {
  162.             athletes[athletecnt++] = athlete;
  163.             saveAthletes();
  164.         }
  165.     }
  166.  
  167.  
  168.     void addAthlete() {
  169.         Athlete athlete;
  170.         cout << "Введите ФИО спортсмена: ";
  171.         cin.ignore();
  172.         getline(cin, athlete.fullName);
  173.         cout << "Введите разряд спортсмена: ";
  174.         getline(cin, athlete.rank);
  175.         cout << "Введите год рождения спортсмена: ";
  176.         cin >> athlete.birthYear;
  177.         cout << "Введите команду спортсмена: ";
  178.         cin.ignore();
  179.         getline(cin, athlete.team);
  180.         cout << "Введите достижения спортсмена: ";
  181.         getline(cin, athlete.achievements);
  182.         addAthlete(athlete);
  183.     }
  184.  
  185.  
  186.     void removeAthlete(const string& fullName) {
  187.         for (int i = 0; i < athletecnt; ++i) {
  188.             if (athletes[i].fullName == fullName) {
  189.                 for (int j = i; j < athletecnt - 1; ++j) {
  190.                     athletes[j] = athletes[j + 1];
  191.                 }
  192.                 athletecnt--;
  193.                 saveAthletes();
  194.                 break;
  195.             }
  196.         }
  197.     }
  198.     void removeAthlete() {
  199.         string fullName;
  200.         cout << "Введите ФИО спортсмена для удаления: ";
  201.         cin.ignore();
  202.         getline(cin, fullName);
  203.         removeAthlete(fullName);
  204.     }
  205.  
  206.     void addCoach(const Coach& coach) {
  207.         if (coachcnt < MAX_COACHES) {
  208.             coaches[coachcnt++] = coach;
  209.             saveCoaches();
  210.         }
  211.     }
  212.     void addCoach() {
  213.         Coach coach;
  214.         cout << "Введите ФИО тренера: ";
  215.         cin.ignore();
  216.         getline(cin, coach.fullName);
  217.         cout << "Введите звание тренера: ";
  218.         getline(cin, coach.title);
  219.         coach.athletecnt = 0;
  220.         cout << "Введите количество спортсменов тренера: ";
  221.         int c;
  222. //        cout << "123";
  223.         int cnt;
  224.         cin >> cnt;
  225.         cin.ignore();
  226.  
  227.         for (int i = 0; i < cnt; ++i) {
  228.             string athleteName;
  229.             cout << "Введите ФИО спортсмена: ";
  230.  
  231. //            cout << "wqe";
  232.             getline(cin, athleteName);
  233.             for (int j = 0; j < athletecnt; ++j) {
  234.                 if (athletes[j].fullName == athleteName) {
  235.                     coach.athletes[coach.athletecnt++] = &athletes[j];
  236.                     break;
  237.                 }
  238.             }
  239.         }
  240.         addCoach(coach);
  241.     }
  242.  
  243.     void removeCoach(const string& fullName) {
  244.         for (int i = 0; i < coachcnt; ++i) {
  245.             if (coaches[i].fullName == fullName) {
  246.                 for (int j = i; j < coachcnt - 1; ++j) {
  247.                     coaches[j] = coaches[j + 1];
  248.                 }
  249.                 coachcnt--;
  250.                 saveCoaches();
  251.                 break;
  252.             }
  253.         }
  254.     }
  255.     void removeCoach() {
  256.         string fullName;
  257.         cout << "Введите ФИО тренера для удаления: ";
  258.         cin.ignore();
  259.         getline(cin, fullName);
  260.         removeCoach(fullName);
  261.     }
  262.  
  263.     void addJudge(const Judge& judge) {
  264.         if (judgecnt < MAX_JUDGES) {
  265.             judges[judgecnt++] = judge;
  266.             saveJudges();
  267.         }
  268.     }
  269.  
  270.     void addJudge() {
  271.         Judge judge;
  272.         cout << "Введите ФИО судьи: ";
  273.         cin.ignore();
  274.         getline(cin, judge.fullName);
  275.         cout << "Введите категорию судьи: ";
  276.         getline(cin, judge.category);
  277.         judge.competitioncnt = 0;
  278.         cout << "Введите количество проведённых соревнований: ";
  279.         int cnt;
  280.         cin >> cnt;
  281.         cin.ignore();
  282.         for (int i = 0; i < cnt; ++i) {
  283.             cout << "Введите название соревнования: ";
  284.             getline(cin, judge.competitions[judge.competitioncnt++]);
  285.         }
  286.         addJudge(judge);
  287.     }
  288.  
  289.     void removeJudge(const string& fullName) {
  290.         for (int i = 0; i < judgecnt; ++i) {
  291.             if (judges[i].fullName == fullName) {
  292.                 for (int j = i; j < judgecnt - 1; ++j) {
  293.                     judges[j] = judges[j + 1];
  294.                 }
  295.                 judgecnt--;
  296.                 saveJudges();
  297.                 break;
  298.             }
  299.         }
  300.     }
  301.  
  302.     void removeJudge() {
  303.         string fullName;
  304.         cout << "Введите ФИО судьи для удаления: ";
  305.         cin.ignore();
  306.         getline(cin, fullName);
  307.         removeJudge(fullName);
  308.     }
  309.  
  310.     void addCompetition(const Competition& competition) {
  311.         if (competitioncnt < MAX_COMPETITIONS) {
  312.             competitions[competitioncnt++] = competition;
  313.         }
  314.     }
  315.  
  316.     void addCompetition() {
  317.         Competition competition;
  318.         cout << "Введите дату соревнования: ";
  319.         cin.ignore();
  320.         getline(cin, competition.date);
  321.         cout << "Введите статус соревнования: ";
  322.         getline(cin, competition.status);
  323.         cout << "Введите место проведения соревнования: ";
  324.         getline(cin, competition.venue);
  325.         competition.judgecnt = 0;
  326.         cout << "Введите количество судей: ";
  327.         int cnt;
  328.         cin >> cnt;
  329.         cin.ignore();
  330.         for (int i = 0; i < cnt; ++i) {
  331.             string judgeName;
  332.             cout << "Введите ФИО судьи: ";
  333.             getline(cin, judgeName);
  334.             for (int j = 0; j < judgecnt; ++j) {
  335.                 if (judges[j].fullName == judgeName) {
  336.                     competition.judges[competition.judgecnt++] = &judges[j];
  337.                     break;
  338.                 }
  339.             }
  340.         }
  341.         addCompetition(competition);
  342.     }
  343.  
  344.     void createCompetitionResults(const string& competitionDate) {
  345.         for (int i = 0; i < competitioncnt; ++i) {
  346.             if (competitions[i].date == competitionDate) {
  347.                 competitions[i].resultcnt = 0;
  348.                 for (int j = 0; j < athletecnt; ++j) {
  349.                     if (competitions[i].resultcnt < MAX_RESULTS_PER_COMPETITION) {
  350.                         competitions[i].results[competitions[i].resultcnt].athlete = &athletes[j];
  351.                         competitions[i].results[competitions[i].resultcnt].result = "Результат";
  352.                         competitions[i].resultcnt++;
  353.                     }
  354.                 }
  355.                 break;
  356.             }
  357.         }
  358.     }
  359.     void createCompetitionResults() {
  360.         string date;
  361.         cout << "Введите дату соревнования для создания результатов: ";
  362.         cin.ignore();
  363.         getline(cin, date);
  364.         createCompetitionResults(date);
  365.     }
  366.  
  367.  
  368.  
  369.     void updateAthleteAchievements(const string& competitionDate) {
  370.         for (int i = 0; i < competitioncnt; ++i) {
  371.             if (competitions[i].date == competitionDate) {
  372.                 for (int j = 0; j < competitions[i].resultcnt; ++j) {
  373.                     Athlete* athlete = competitions[i].results[j].athlete;
  374.                     if (!athlete->achievements.empty()) {
  375.                         athlete->achievements += ", ";
  376.                     }
  377.                     athlete->achievements += competitions[i].results[j].result;
  378.                 }
  379.                 saveAthletes();
  380.                 break;
  381.             }
  382.         }
  383.     }
  384.     void updateAthleteAchievements() {
  385.         string date;
  386.         cout << "Введите дату соревнования для обновления достижений спортсменов: ";
  387.         cin.ignore();
  388.         getline(cin, date);
  389.         updateAthleteAchievements(date);
  390.     }
  391.  
  392.     void printCoachAchievements() {
  393.         for (int i = 0; i < coachcnt; ++i) {
  394.             cout << coaches[i].fullName << " (" << coaches[i].title << "):" << endl;
  395.             for (int j = 0; j < coaches[i].athletecnt; ++j) {
  396.                 cout << "  " << coaches[i].athletes[j]->fullName << " - " << coaches[i].athletes[j]->achievements << endl;
  397.             }
  398.         }
  399.     }
  400.  
  401.  
  402. };
  403.  
  404. void printMenu() {
  405.     cout << "Меню:\n";
  406.     cout << "1. Добавить спортсмена\n";
  407.     cout << "2. Удалить спортсмена\n";
  408.     cout << "3. Добавить тренера\n";
  409.     cout << "4. Удалить тренера\n";
  410.     cout << "5. Добавить судью\n";
  411.     cout << "6. Удалить судью\n";
  412.     cout << "7. Добавить соревнование\n";
  413.     cout << "8. Создать результаты соревнования\n";
  414.     cout << "9. Обновить достижения спортсменов\n";
  415.     cout << "10. Показать достижения тренеров\n";
  416.     cout << "0. Выход\n";
  417. }
  418.  
  419. int main() {
  420.  
  421.     system("chcp 1251");
  422.     SportsFederation federation;
  423.     int choice;
  424. //    cout << 123 << endl;
  425. //    return 1;
  426.     do {
  427.         printMenu();
  428.         cin >> choice;
  429.  
  430.         switch (choice) {
  431.             case 1:
  432.                 federation.addAthlete();
  433.                 break;
  434.             case 2:
  435.                 federation.removeAthlete();
  436.                 break;
  437.             case 3:
  438.                 federation.addCoach();
  439.                 break;
  440.             case 4:
  441.                 federation.removeCoach();
  442.                 break;
  443.             case 5:
  444.                 federation.addJudge();
  445.                 break;
  446.             case 6:
  447.                 federation.removeJudge();
  448.                 break;
  449.             case 7:
  450.                 federation.addCompetition();
  451.                 break;
  452.             case 8:
  453.                 federation.createCompetitionResults();
  454.                 break;
  455.             case 9:
  456.                 federation.updateAthleteAchievements();
  457.                 break;
  458.             case 10:
  459.                 federation.printCoachAchievements();
  460.                 break;
  461.             case 0:
  462.                 cout << "Выход...\n";
  463.                 break;
  464.             default:
  465.                 cout << "Неверный выбор. Попробуйте снова.\n";
  466.                 break;
  467.         }
  468.     } while (choice != 0);
  469.  
  470.     return 0;
  471. }
  472.  
Advertisement
Add Comment
Please, Sign In to add comment