Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <Windows.h>
  3.  
  4. struct SPORTMAN
  5. {
  6.     int oc;
  7.     int razbr;
  8.     char Name[255];
  9. };
  10.  
  11. struct COMP
  12. {
  13.     int judsCount;
  14.     int sportmansCount;
  15.     SPORTMAN sportmans[100];
  16. };
  17.  
  18. void AddSor()
  19. {
  20.     system("cls");
  21.  
  22.     COMP comp = { 0 };
  23.  
  24.     FILE* file = fopen("sorevs.bd", "r+b");
  25.     if (file == NULL)
  26.     {
  27.         file = fopen("sorevs.bd", "w+b");
  28.         if (file == NULL)
  29.         {
  30.             printf("Ошибка открытии файла\n");
  31.             system("pause");
  32.             return;
  33.         }
  34.     }
  35.  
  36.     printf("Введите количество спортсменов: ");
  37.     scanf("%d", &comp.sportmansCount);
  38.  
  39.     printf("Введите количество судей: ");
  40.     scanf("%d", &comp.judsCount);
  41.  
  42.     printf("Введите оценки спортсменов (от 0 до 5)\n\n");
  43.     char buff[255] = { 0 };
  44.  
  45.     for (int i = 0; i < comp.sportmansCount; i++)
  46.     {
  47.         int min = 7, max = -1;
  48.         bool bMin = false, bMax = false;
  49.         bool skipMinFlag = false, skipMaxFlag = false;
  50.         int oc = 0;
  51.  
  52.         fgets(buff, 255, stdin);
  53.         printf("Спортсмен №%d\nВведите имя спортсмена: ", i + 1);
  54.         fgets(comp.sportmans[i].Name, 255, stdin);
  55.         if (comp.sportmans[i].Name[strlen(comp.sportmans[i].Name) - 1] == '\n')
  56.             comp.sportmans[i].Name[strlen(comp.sportmans[i].Name) - 1] = '\0';
  57.  
  58.         for (int j = 0; j < comp.judsCount; j++)
  59.         {
  60.             printf("\tСудья №%d: ", j + 1);
  61.  
  62.             do scanf("%d", &oc);
  63.             while (oc < 0 || oc > 5);
  64.  
  65.             if (oc > max)
  66.                 max = oc;
  67.  
  68.             if (oc < min)
  69.                 min = oc;
  70.  
  71.             comp.sportmans[i].oc += oc;
  72.         }
  73.         comp.sportmans[i].oc = comp.sportmans[i].oc - max - min;
  74.         comp.sportmans[i].razbr = max - min;
  75.     }
  76.  
  77.     fseek(file, 0, SEEK_END);
  78.     fwrite(&comp, sizeof(COMP), 1, file);
  79.     fclose(file);
  80. }
  81.  
  82. void PrintAllSor()
  83. {
  84.     system("cls");
  85.  
  86.     FILE* file = fopen("sorevs.bd", "r+b");
  87.     if (file == NULL)
  88.     {
  89.         printf("Ошибка открытия файла\n");
  90.         system("pause");
  91.         return;
  92.     }
  93.  
  94.     COMP* comp = new COMP;
  95.  
  96.     fseek(file, 0, SEEK_END);
  97.     int filesize = ftell(file);
  98.  
  99.     for (unsigned int i = 0; i < filesize; i += sizeof(COMP))
  100.     {
  101.         fseek(file, i, SEEK_SET);
  102.  
  103.         fread(comp, sizeof(COMP), 1, file);
  104.  
  105.         printf("\nСоревнование №%d\nКол-во спортсменов: %d\nКол-во судей: %d\n\n", i / sizeof(COMP) + 1, comp->sportmansCount, comp->judsCount);
  106.  
  107.         SPORTMAN* sportmanMin = new SPORTMAN{ 0 };
  108.         SPORTMAN* sportmanMax = new SPORTMAN{ 0 };
  109.  
  110.         sportmanMin->razbr = 7;
  111.         sportmanMax->razbr = -1;
  112.  
  113.         for (int j = 0; j < comp->sportmansCount; j++)
  114.         {
  115.             SPORTMAN* sportman = new SPORTMAN{ 0 };
  116.             int ind = -1;
  117.  
  118.             for (int k = 0; k < comp->sportmansCount; k++)
  119.             {
  120.                 if (comp->sportmans[k].oc > sportman->oc)
  121.                 {
  122.                     memcpy(sportman, &comp->sportmans[k], sizeof(SPORTMAN));
  123.                     ind = k;
  124.                 }
  125.             }
  126.  
  127.             if (sportman->razbr > sportmanMax->razbr)
  128.                 memcpy(sportmanMax, sportman, sizeof(SPORTMAN));
  129.             if (sportman->razbr < sportmanMin->razbr)
  130.                 memcpy(sportmanMin, sportman, sizeof(SPORTMAN));
  131.  
  132.             comp->sportmans[ind].oc = 0;
  133.             printf("\t%s : %d\n", sportman->Name, sportman->oc);
  134.             delete sportman;
  135.             sportman = nullptr;
  136.         }
  137.  
  138.         printf("\nНаибольший разброс у %s\nНаименьший разброс у %s\n",
  139.             sportmanMax->Name, sportmanMin->Name);
  140.  
  141.         delete sportmanMin; delete sportmanMax;
  142.         sportmanMin = nullptr; sportmanMax = nullptr;
  143.     }
  144.  
  145.     delete comp;
  146.     comp = nullptr;
  147.  
  148.     fclose(file);
  149.  
  150.     system("pause");
  151. }
  152.  
  153. int main()
  154. {
  155.     SetConsoleCP(1251);
  156.     SetConsoleOutputCP(1251);
  157.     int sel = 5;
  158.  
  159.     while (sel != 0)
  160.     {
  161.         do
  162.         {
  163.             system("cls");
  164.             printf("1 - Добавить информацию о соревновании\n2 - Вывести все соревнования\n0 - Выход\n");
  165.             scanf("%d", &sel);
  166.         } while (sel < 0 || sel > 2);
  167.  
  168.         if (sel == 1)
  169.             AddSor();
  170.         else if (sel == 2)
  171.             PrintAllSor();
  172.     }
  173.  
  174.     system("pause");
  175.     return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement