Advertisement
3axap_010

source.cpp

Jun 6th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.36 KB | None | 0 0
  1. #include "pch.h"
  2. #include "Header.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <locale.h>
  7.  
  8. FILE* file_create(const char *filename, const char *mode)
  9. {
  10.     FILE *file = fopen(filename, mode);
  11.     if (file == NULL)
  12.     {
  13.         fprintf(stderr, "Can't open file: %s\n", filename);
  14.         return NULL;
  15.     }
  16.  
  17.     return file;
  18. }
  19.  
  20. struct connection* make_connection(const char *filename, const char *mode)
  21. {
  22.     struct connection *conn = (struct connection*)malloc(sizeof(struct connection));
  23.     if (!conn)
  24.     {
  25.         fprintf(stderr, "Out of memory\n");
  26.         return NULL;
  27.     }
  28.  
  29.     conn->file = file_create(filename, mode);
  30.     conn->classes = (struct clas*)malloc(sizeof(struct clas));
  31.     if (!conn->classes)
  32.     {
  33.         fprintf(stderr, "Out of memory\n");
  34.         free(conn);
  35.         return NULL;
  36.     }
  37.  
  38.     return conn;
  39. }
  40.  
  41. int int_input()
  42. {
  43.     int i, n;
  44.  
  45.     do
  46.     {
  47.         i = scanf_s("%d", &n);
  48.         if (!i)
  49.         {
  50.             rewind(stdin);
  51.             printf("Invalid input\n");
  52.             continue;
  53.         }
  54.     } while (!i);
  55.  
  56.     return n;
  57. }
  58.  
  59. int stud_number()
  60. {
  61.     int numb;
  62.     printf("Введите количество студентов:\n");
  63.  
  64.     do
  65.     {
  66.         numb = int_input();
  67.     } while (numb <= 0);
  68.  
  69.     return numb;
  70. }
  71.  
  72. struct student* set_student(struct student *st)
  73. {
  74.     int i;
  75.  
  76.     st->set = 1;
  77.  
  78.     printf("Имя:");
  79.     rewind(stdin);
  80.     gets_s(st->first_name, MAX_DATA);
  81.     printf("Фамилия:");
  82.     rewind(stdin);
  83.     gets_s(st->second_name, MAX_DATA);
  84.     printf("Отчество:");
  85.     rewind(stdin);
  86.     gets_s(st->patronimyc, MAX_DATA);
  87.  
  88.     printf("Номер класса:");
  89.     st->year = int_input();
  90.     printf("Буква класса:");
  91.     rewind(stdin);
  92.     st->letter = getchar();
  93.  
  94.     st->sum = 0;
  95.  
  96.     for (i = 0; i < MARKS; i++)
  97.     {
  98.         printf("Оценка № %d: ", i + 1);
  99.         st->marks[i] = int_input();
  100.         st->sum += st->marks[i];
  101.     }
  102.  
  103.     return st;
  104. }
  105.  
  106. int set_all(struct connection *conn)
  107. {
  108.     int i;
  109.     int students_number = stud_number();
  110.  
  111.     for (i = 0; i < MAX_ROWS; i++)
  112.     {
  113.         conn->classes->rows[i].set = 0;
  114.     }
  115.  
  116.     struct student *stud;
  117.  
  118.     for (i = 0; i < students_number; i++)
  119.     {
  120.         stud = &conn->classes->rows[i];
  121.         stud = set_student(stud);
  122.     }
  123.  
  124.     return students_number;
  125. }
  126.  
  127. struct connection* delete_connection(struct connection *conn)
  128. {
  129.     int i;
  130.  
  131.     if (conn)
  132.     {
  133.         for (i = 0; i < MAX_ROWS; i++)
  134.         {
  135.             conn->classes->rows[i].set = 0;
  136.         }
  137.  
  138.         if (conn->file)
  139.         {
  140.             fclose(conn->file);
  141.             conn->file = NULL;
  142.         }
  143.  
  144.         if (conn->classes)
  145.         {
  146.             free(conn->classes);
  147.             conn->classes = NULL;
  148.         }
  149.  
  150.         free(conn);
  151.     }
  152.  
  153.     return NULL;
  154. }
  155.  
  156. void print_student(struct student *stud)
  157. {
  158.     int i = 0;
  159.  
  160.     printf("%d%c ", stud->year, stud->letter);
  161.     printf("%s %s %s\n", stud->first_name, stud->second_name, stud->patronimyc);
  162.  
  163.     printf("Оценки:\n");
  164.     for (i = 0; i < MARKS; i++)
  165.     {
  166.         printf("%3d", stud->marks[i]);
  167.     }
  168.     printf("\n\n");
  169. }
  170.  
  171. void print_all(struct clas *cl, int students_number)
  172. {
  173.     int i;
  174.  
  175.     for (i = 0; i < students_number; i++)
  176.     {
  177.         if (cl->rows[i].set == 1)
  178.             print_student(&cl->rows[i]);
  179.     }
  180. }
  181.  
  182. void write(struct connection *conn)
  183. {
  184.     rewind(conn->file);
  185.  
  186.     int rc = fwrite(conn->classes, sizeof(struct clas), 1, conn->file);
  187.     if (rc != 1)
  188.     {
  189.         fprintf(stderr, "Can't write data in file\n");
  190.     }
  191. }
  192.  
  193. struct connection* read(struct connection *conn)
  194. {
  195.     if (conn != NULL)
  196.     {
  197.         delete_connection(conn);
  198.     }
  199.  
  200.     conn = make_connection("file", "rb");
  201.  
  202.     int rc = fread(conn->classes, sizeof(struct clas), 1, conn->file);
  203.     if (rc > 1 || ferror(conn->file))
  204.     {
  205.         fprintf(stderr, "Can't load data from file\n");
  206.     }
  207.  
  208.     return conn;
  209. }
  210.  
  211. int define_student(struct student *stud)
  212. {
  213.     if (!stud->set)
  214.     {
  215.         return 0;
  216.     }
  217.  
  218.     for (int i = 0; i < MARKS; i++)
  219.     {
  220.         if (stud->marks[i] <= 4)
  221.         {
  222.             return 0;
  223.         }
  224.     }
  225.  
  226.     return 1;
  227. }
  228.  
  229. int print_good(struct clas *classes)
  230. {
  231.     int i = 0, j = 0, good = 0;
  232.  
  233.     printf("Ученики без четверок:\n");
  234.  
  235.     for (i = 0; i < MAX_ROWS; i++)
  236.     {
  237.         if (define_student(&classes->rows[i]))
  238.         {
  239.             print_student(&classes->rows[i]);
  240.             good++;
  241.         }
  242.     }
  243.  
  244.     if (!good)
  245.     {
  246.         printf("Нет учеников без четверок\n");
  247.     }
  248.     else
  249.     {
  250.         printf("Всего учеников без четверок: %d\n", good);
  251.     }
  252.  
  253.     return good;
  254. }
  255.  
  256. int get_max_sum(struct student *rows, int students_number, int class_year)
  257. {
  258.     int i, max_sum = 0;
  259.  
  260.     for (i = 0; i < students_number; i++)
  261.     {
  262.         if (rows[i].year == class_year && rows[i].sum > max_sum && define_student(&rows[i]))
  263.         {
  264.             max_sum = rows[i].sum;
  265.         }
  266.     }
  267.  
  268.     return max_sum;
  269. }
  270.  
  271. void student_copy(struct clas *destinition, struct clas *source, int des_indx, int sour_indx)
  272. {
  273.     strcpy(destinition->rows[des_indx].first_name, source->rows[sour_indx].first_name);
  274.     strcpy(destinition->rows[des_indx].second_name, source->rows[sour_indx].second_name);
  275.     strcpy(destinition->rows[des_indx].patronimyc, source->rows[sour_indx].patronimyc);
  276.  
  277.     destinition->rows[des_indx].set = 1;
  278.     destinition->rows[des_indx].letter = source->rows[sour_indx].letter;
  279.     destinition->rows[des_indx].year = source->rows[sour_indx].year;
  280.     destinition->rows[des_indx].sum = source->rows[sour_indx].sum;
  281.  
  282.     for (int i = 0; i < MARKS; i++)
  283.     {
  284.         destinition->rows[des_indx].marks[i] = source->rows[sour_indx].marks[i];
  285.     }
  286. }
  287.  
  288. int students_cmp(struct student *stud1, struct student *stud2)
  289. {
  290.     if (stud1->set != 1 || stud2->set != 1)
  291.     {
  292.         return 0;
  293.     }
  294.  
  295.     if (strcmp(stud1->first_name, stud2->first_name) != 0) return 0;
  296.     if (strcmp(stud1->second_name, stud2->second_name) != 0) return 0;
  297.     if (strcmp(stud1->patronimyc, stud2->patronimyc) != 0) return 0;
  298.     if (stud1->sum != stud2->sum) return 0;
  299.  
  300.     return 1;
  301. }
  302.  
  303. void remove_similar(struct student *rows)
  304. {
  305.     for (int i = 0; i < MAX_ROWS; i++)
  306.     {
  307.         for (int j = i + 1; j < MAX_ROWS; j++)
  308.         {
  309.             if (students_cmp(&rows[i], &rows[j]))
  310.             {
  311.                 rows[j].set = 0;
  312.             }
  313.         }
  314.     }
  315. }
  316.  
  317. struct clas* find_best(struct clas *classes, int students_number)
  318. {
  319.     int class_year, i, j, max_sum = 0;
  320.     int indx = 0;
  321.  
  322.     struct clas *best = (struct clas*)malloc(sizeof(struct clas));
  323.  
  324.     for (i = 0; i < students_number; i++)
  325.     {
  326.         class_year = classes->rows[i].year;
  327.         max_sum = get_max_sum(classes->rows, students_number, class_year);
  328.  
  329.         for (j = i; j < students_number; j++)
  330.         {
  331.             if (classes->rows[j].year == class_year && classes->rows[j].sum == max_sum)
  332.             {
  333.                 student_copy(best, classes, indx++, j);
  334.             }
  335.         }
  336.     }
  337.  
  338.     remove_similar(best->rows);
  339.  
  340.     return best;
  341. }
  342.  
  343. void print_best(struct clas *classes)
  344. {
  345.     int i = 0;
  346.     int count = 0;
  347.  
  348.     printf("Лучшие ученики по сумме баллов в своем и параллельных классах:\n");
  349.  
  350.     for (i = 0; i < MAX_ROWS; i++)
  351.     {
  352.         if (classes->rows[i].set == 1)
  353.         {
  354.             print_student(&classes->rows[i]);
  355.             count++;
  356.         }
  357.     }
  358.  
  359.     if (!count) printf("Нет учеников без четверок\n");
  360. }
  361.  
  362. int obtain_max_row(char *str1, char *str2)
  363. {
  364.     int i = 0;
  365.  
  366.     while (*(str1 + i) != '\0' && *(str2 + i) != '\0')
  367.     {
  368.         if (str1[i] > str2[i]) return 1;
  369.         else if (str2[i] > str1[i++]) return 0;
  370.     }
  371.  
  372.     if (str1[i] == str2[i] == '\0') return 0;
  373.     if (str1[i] == '\0') return 0;
  374.  
  375.     return 1;
  376. }
  377.  
  378. void sort(struct clas *clas)
  379. {
  380.     int i, j, max_indx;
  381.  
  382.     for (i = 0; i < MAX_ROWS - 1; i++)
  383.     {
  384.         struct student key = clas->rows[i];
  385.         max_indx = i;
  386.  
  387.         for (j = i + 1; j < MAX_ROWS; j++)
  388.         {
  389.             if (key.set && clas->rows[j].set)
  390.             {
  391.                 if (obtain_max_row(key.first_name, clas->rows[j].first_name))
  392.                 {
  393.                     max_indx = j;
  394.                     key = clas->rows[j];
  395.                 }
  396.             }
  397.         }
  398.  
  399.         clas->rows[max_indx] = clas->rows[i];
  400.         clas->rows[i] = key;
  401.     }
  402. }
  403.  
  404. float get_average(struct clas *clas)
  405. {
  406.     int i = 0;
  407.     float gen_sum = 0.0, average = 0.0;
  408.     int numb = 0;
  409.  
  410.     for (i = 0; i < MAX_ROWS; i++)
  411.     {
  412.         if (clas->rows[i].set)
  413.         {
  414.             gen_sum += clas->rows[i].sum;
  415.             numb++;
  416.         }
  417.     }
  418.  
  419.     average = gen_sum / MARKS / numb;
  420.  
  421.     return average;
  422. }
  423.  
  424. struct student* read_n(struct connection *conn)
  425. {
  426.     rewind(conn->file);
  427.     fpos_t pos;
  428.     fgetpos(conn->file, &pos);
  429.  
  430.     printf("Введите номер записи для считывания:\n");
  431.     int n = int_input();
  432.  
  433.     for (int i = 0; i < MAX_ROWS && i < n - 1; i++)
  434.     {
  435.         fseek(conn->file, sizeof(struct student), SEEK_CUR);
  436.     }
  437.  
  438.     struct student *stud = (struct student*)malloc(sizeof(struct student));
  439.     int rc = fread(stud, sizeof(struct student), 1, conn->file);
  440.     if (rc < 1)
  441.     {
  442.         fprintf(stderr, "Can't read data\n");
  443.         return NULL;
  444.     }
  445.  
  446.     return stud;
  447. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement