Advertisement
codegod313

=)(P.S. Если что пишите)

Jun 9th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.10 KB | None | 0 0
  1. // Задание 6.2(16) Стельмашук В.А.(гр. №953506)
  2.  
  3. /*Рейтинг студентов.
  4. Программа рассчитывает рейтинг студентов
  5. специальности «Информатика».Рейтинг учитывает все оценки,
  6. полученные студентами во время сессий – как положительные, так
  7. и отрицательные.Можно просмотреть рейтинг по курсам, по
  8. группам.Отображается текущий средний балл, рост
  9. (положительный или отрицательный) относительно последней
  10. сессии, изменение места в рейтинге.*/
  11.  
  12.  
  13. #define _CRT_SECURE_NO_WARNINGS
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18.  
  19. typedef struct student
  20. {
  21.     char* surname;
  22.     char* name;
  23.     char* patronym;
  24.     int course;
  25.     int group;
  26.     int* marks1, *marks2; /*оценки за две сессии*/
  27.     double average1, average2; /*средний балл за две сессии*/
  28.  
  29.     int ratingPlace1, ratingPlace2; /*место в рейтинге в первой и во второй сессии*/
  30.     double averageDelta; /*изменение среднего балла относительно последней сессии*/
  31.     int ratingDelta; /*изменение места в рейтинге*/
  32. } Student;
  33.  
  34. typedef struct node
  35. {
  36.     int number;
  37.     Student* student;
  38.     struct node* next;// указатель на следующий эл. списка
  39.     struct node* prev;// указатель на предыдущий эл. списка
  40. } Node;
  41.  
  42. typedef struct list
  43. {
  44.     Node* first, *last;
  45.     int counter;
  46. } List;
  47.  
  48. List* list;
  49. int updated = 0;
  50.  
  51. void FreeMemoryNode(node* &Node);
  52. void FreeMemoryList(List* &LIST);
  53. void Menu(void);
  54. int chooseOption(void);
  55.  
  56. void showStudent(Student* student, int sem);
  57. void showList(int sem);
  58. void showByGroup(int group);
  59. void showByCourse(int course);
  60. void QuickSortList(Node* left, Node* right);
  61.  
  62. double getAverage(int** marks);
  63. double getAverageDelta(Student* student);
  64. int getRatingDelta(Node* node);
  65. void update(void);
  66.  
  67. char* readString(FILE* file);
  68. int* readDigit(FILE* file);
  69. Student* readStudent(FILE* file);
  70. void pushBack(Student* student);
  71. void writeStudent(Student* student, FILE* file);
  72.  
  73. void save();
  74.  
  75. int main()
  76. {
  77.     list = (List*)malloc(sizeof(List));
  78.     list->first = NULL;
  79.     list->last = NULL;
  80.     list->counter = 0;
  81.  
  82.     FILE* students = fopen("C:\\Лабораторные С\\LR7\\students.txt", "r");
  83.     if (students == NULL)
  84.     {
  85.         printf("\nSomething is wrong. File wasn't open :<");
  86.         return 0;
  87.     }
  88.  
  89.     while (!feof(students))
  90.         pushBack(readStudent(students));
  91.  
  92.     while (1)
  93.         Menu();
  94.  
  95.     fclose(students);
  96.     return 0;
  97. }
  98.  
  99. void FreeMemoryNode(node* &Node)
  100. {
  101.     free(Node->student->surname);
  102.     free(Node->student->name);
  103.     free(Node->student->patronym);
  104.     free(Node->student->marks1);
  105.     free(Node->student->marks2);
  106.     free(Node->student);
  107.     free(Node);
  108. }
  109.  
  110. void FreeMemoryList(List* &LIST)
  111. {
  112.     if (LIST->first != NULL)
  113.     {
  114.         node* current = LIST->first;
  115.         while (current != LIST->last)
  116.         {
  117.             current = current->next;
  118.             if (current != LIST->last)
  119.                 FreeMemoryNode(current->prev);
  120.         }
  121.         free(current);
  122.         LIST->first = NULL;
  123.         LIST->last = NULL;
  124.         LIST->counter = 0;
  125.     }
  126. }
  127.  
  128. void Menu(void)
  129. {
  130.     printf("\t\t\t\t\t\tMenu");
  131.     printf("\n\t\t\t\t\t1. Show first semester rating");
  132.     printf("\n\t\t\t\t\t2. Update on the results of the second semester");
  133.     printf("\n\t\t\t\t\t3. Show second semester rating");
  134.     printf("\n\t\t\t\t\t4. Show all information");
  135.     printf("\n\t\t\t\t\t5. Show specific course..");
  136.     printf("\n\t\t\t\t\t6. Show specific group..");
  137.     printf("\n\t\t\t\t\t7. Exit from the program");
  138.     chooseOption();
  139. }
  140.  
  141. int chooseOption(void)
  142. {
  143.     int ans = 0;
  144.  
  145.     printf("\nYour choice: ");
  146.     while (!scanf("%d", &ans) || ans <= 0 || ans > 7)
  147.     {
  148.         printf("Wrong input! Try again. \n");
  149.         while (getchar() != '\n');
  150.         printf("\nYour choice: ");
  151.     }
  152.     while (getchar() != '\n');
  153.  
  154.     switch (ans)
  155.     {
  156.     case 1:
  157.         showList(1);
  158.         break;
  159.     case 2:
  160.         QuickSortList(list->first, list->last);
  161.         update();
  162.         system("cls");
  163.         printf("\n\nRating was updated!\n\n");
  164.         break;
  165.     case 3:
  166.         showList(2);
  167.         break;
  168.     case 4:
  169.         showList(0);
  170.         break;
  171.     case 5:
  172.     {
  173.         int course = 0;
  174.         printf("\nEnter number of a course: ");
  175.         while (!scanf("%d", &course) || course < 0)
  176.         {
  177.             printf("Wrong input! Try again. \n");
  178.             while (getchar() != '\n');
  179.             printf("\nEnter number of a course: ");
  180.         }
  181.         while (getchar() != '\n');
  182.  
  183.         showByCourse(course);
  184.         break;
  185.     }
  186.     case 6:
  187.     {
  188.         int group = 0;
  189.         printf("\nEnter number of a group: ");
  190.         while (!scanf("%d", &group) || group < 0)
  191.         {
  192.             printf("Wrong input! Try again. \n");
  193.             while (getchar() != '\n');
  194.             printf("\nEnter number of a group: ");
  195.         }
  196.         while (getchar() != '\n');
  197.  
  198.         showByGroup(group);
  199.         break;
  200.     }
  201.     case 7:
  202.         save();
  203.         printf("\nGood bye!\n\n");
  204.         exit(1);
  205.     default:
  206.         printf("\nSomething is wrong!");
  207.     }
  208.  
  209.     return ans;
  210. }
  211.  
  212. void showStudent(Student* student, int sem)
  213. {
  214.     printf("\n%s ", student->surname);
  215.     printf("%s ", student->name);
  216.     printf("%s", student->patronym);
  217.  
  218.     printf("\nCourse: %d", student->course);
  219.     printf("\nGroup: %d", student->group);
  220.  
  221.     int k = 0;
  222.     if (sem == 1 || sem == 0)
  223.     {
  224.         printf("\nFirst semester marks: ");
  225.         for (k = 0; k < 4; k++)
  226.             printf("%d ", student->marks1[k]);
  227.  
  228.         printf("\nRating place in first semester: %d", student->ratingPlace1);
  229.  
  230.         printf("\nAverage point in first semester: %.2f", student->average1);
  231.     }
  232.     if (sem == 2 || sem == 0)
  233.     {
  234.         if (updated == 1)
  235.         {
  236.             printf("\nSecond semester marks: ");
  237.             for (k = 0; k < 4; k++)
  238.                 printf("%d ", student->marks2[k]);
  239.  
  240.             printf("\nRating place in second semester: %d", student->ratingPlace2);
  241.  
  242.             printf("\nRating increase: ");
  243.             if (student->ratingDelta > 0)
  244.                 printf("+");
  245.             printf("%d", student->ratingDelta);
  246.  
  247.             printf("\nAverage in second semester: %.2f", student->average2);
  248.  
  249.             printf("\nAverage point increase: ");
  250.             if (student->averageDelta > 0)
  251.                 printf("+");
  252.             printf("%.2f", student->averageDelta);
  253.         }
  254.         else
  255.             printf("\nTo see more information firstly update rating");
  256.     }
  257.  
  258.     printf("\n\n");
  259. }
  260.  
  261. void showList(int sem)
  262. {
  263.     system("cls");
  264.  
  265.     Node* current = list->first;
  266.  
  267.     if (list->first == NULL)
  268.         printf("The list is empty :(");
  269.     else
  270.     {
  271.         while (current != NULL)
  272.         {
  273.             showStudent(current->student, sem);
  274.             current = current->next;
  275.         }
  276.     }
  277. }
  278.  
  279. void showByCourse(int course)
  280. {
  281.     system("cls");
  282.  
  283.     Node* current = list->first;
  284.  
  285.     int done = 0;
  286.  
  287.     if (list->first == NULL)
  288.         printf("The list is empty :(");
  289.     else
  290.     {
  291.         while (current != NULL)
  292.         {
  293.             if (current->student->course == course)
  294.             {
  295.                 showStudent(current->student, 0);
  296.                 done = 1;
  297.             }
  298.             current = current->next;
  299.         }
  300.     }
  301.  
  302.     if (done == 0)
  303.         printf("\n\nNo matches found :(\n\n");
  304. }
  305.  
  306. void showByGroup(int group)
  307. {
  308.     system("cls");
  309.  
  310.     Node* current = list->first;
  311.  
  312.     int done = 0;
  313.  
  314.     if (list->first == NULL)
  315.         printf("The list is empty :(");
  316.     else
  317.     {
  318.         while (current != NULL)
  319.         {
  320.             if (current->student->group == group)
  321.             {
  322.                 showStudent(current->student, 0);
  323.                 done = 1;
  324.             }
  325.             current = current->next;
  326.         }
  327.     }
  328.  
  329.     if (done == 0)
  330.         printf("\n\nNo matches found :(\n\n");
  331. }
  332.  
  333. void QuickSortList(Node* left, Node* right)
  334. {
  335.     if (list->first == NULL)
  336.         return;
  337.  
  338.     Node* start;
  339.     Node* current;
  340.     Student* placeholder;
  341.  
  342.     if (left == right)
  343.         return;
  344.  
  345.     start = left;
  346.     current = start->next;
  347.  
  348.     while (1)
  349.     {
  350.         if (start->student->average2 > current->student->average2)
  351.         {
  352.             placeholder = current->student;
  353.             current->student = start->student;
  354.             start->student = placeholder;
  355.         }
  356.  
  357.         if (current == right)
  358.             break;
  359.  
  360.         current = current->next;
  361.     }
  362.  
  363.     placeholder = left->student;
  364.     left->student = current->student;
  365.     current->student = placeholder;
  366.  
  367.     Node* oldCurrent = current;
  368.  
  369.     current = current->prev;
  370.  
  371.     if (current != NULL)
  372.         if ((left->prev != current) && (current->next != left))
  373.             QuickSortList(left, current);
  374.  
  375.     current = oldCurrent;
  376.     current = current->next;
  377.  
  378.     if (current != NULL)
  379.         if ((current->prev != right) && (right->next != current))
  380.             QuickSortList(current, right);
  381. }
  382.  
  383. double getAverage(int** marks)
  384. {
  385.     double sum = 0;
  386.     int amount = 0;
  387.     int i = 0;
  388.  
  389.     for (i = 0; (*marks)[i] != NULL; i++)
  390.         amount += 1;
  391.     for (i = 0; i < amount; i++)
  392.         sum += (*marks)[i];
  393.  
  394.     return sum / amount;
  395. }
  396.  
  397. double getAverageDelta(Student* student)
  398. {
  399.     return student->average2 - student->average1;
  400. }
  401.  
  402. int getRatingDelta(Node* node)
  403. {
  404.     return node->student->ratingPlace1 - node->student->ratingPlace2;
  405. }
  406.  
  407. void update(void)
  408. {
  409.     updated = 1;
  410.     Node* current = list->first;
  411.  
  412.     while (current != NULL)
  413.     {
  414.         current->student->ratingPlace2 = current->number + 1;
  415.         current->student->ratingDelta = getRatingDelta(current);
  416.         current->student->averageDelta = getAverageDelta(current->student);
  417.         current = current->next;
  418.     }
  419. }
  420.  
  421. Student* readStudent(FILE* file)
  422. {
  423.     Student* newStudent = (Student*)malloc(sizeof(Student));
  424.  
  425.     newStudent->surname = readString(file);
  426.     newStudent->name = readString(file);
  427.     newStudent->patronym = readString(file);
  428.     newStudent->course = *(readDigit(file));
  429.     newStudent->group = *(readDigit(file));
  430.     newStudent->marks1 = readDigit(file);
  431.     newStudent->marks2 = readDigit(file);
  432.  
  433.     newStudent->average1 = getAverage(&(newStudent->marks1));
  434.     newStudent->average2 = getAverage(&(newStudent->marks2));
  435.     newStudent->averageDelta = 0;
  436.     newStudent->ratingPlace1 = 0;
  437.     newStudent->ratingPlace2 = 0;
  438.     newStudent->ratingDelta = 0;
  439.  
  440.     return newStudent;
  441. }
  442.  
  443. void pushBack(Student* student)
  444. {
  445.     Node* newNode = (Node*)malloc(sizeof(Node));
  446.     newNode->next = NULL;
  447.     newNode->student = student;
  448.     newNode->number = list->counter;
  449.     newNode->student->ratingPlace1 = newNode->number + 1;
  450.  
  451.     if (list->first == NULL)
  452.     {
  453.         list->first = newNode;
  454.         list->last = newNode;
  455.     }
  456.     else
  457.     {
  458.         list->last->next = newNode;
  459.         newNode->prev = list->last;
  460.         list->last = newNode;
  461.     }
  462.  
  463.     list->counter += 1;
  464. }
  465.  
  466. char* readString(FILE* file)
  467. {
  468.     char s[100];
  469.     fscanf(file, "%s", s);
  470.     int len = strlen(s);
  471.  
  472.     char* output = (char*)calloc(len + 1, sizeof(char));
  473.     if (output == NULL)
  474.     {
  475.         printf("\nNot enough memory :(");
  476.         exit(1);
  477.     }
  478.  
  479.     strcpy(output, s);
  480.  
  481.     return output;
  482. }
  483.  
  484. int* readDigit(FILE* file) /*считывание строки с оценками*/
  485. {
  486.     int* vals = (int*)calloc(100, sizeof(int)); /*массив значений*/
  487.  
  488.     char s[30] = "";
  489.     fscanf(file, "%s", s);
  490.  
  491.     int col = 0;
  492.     int i = 0;
  493.     for (i = 0; s[i] != '\0'; i++)
  494.     {
  495.         if (s[i] == ' ' || s[i] == '\t' || s[i] == ',')
  496.             continue;
  497.  
  498.         if (s[i] == '-' || (s[i] >= '0' && s[i] <= '9'))
  499.         {
  500.             char* box = (char*)calloc(5, sizeof(char));
  501.             int len = 0;
  502.             while (s[i] == '-' || (s[i] >= '0' && s[i] <= '9'))
  503.             {
  504.                 box[len++] = s[i];
  505.                 i++;
  506.             }
  507.             vals[col++] = atoi(box);
  508.         }
  509.     }
  510.     return vals;
  511. }
  512.  
  513. void save()
  514. {
  515.     FILE* file;
  516.     file = fopen("C:\\Лабораторные С\\LR7\\studentsOutput.txt", "w");
  517.  
  518.     Node* current = list->first;
  519.  
  520.     while (current != NULL)
  521.     {
  522.         writeStudent(current->student, file);
  523.         current = current->next;
  524.     }
  525.     fclose(file);
  526. }
  527.  
  528. void writeStudent(Student* student, FILE* file)
  529. {
  530.     fprintf(file, "\n%s ", student->surname);
  531.     fprintf(file, "%s ", student->name);
  532.     fprintf(file, "%s", student->patronym);
  533.  
  534.     fprintf(file, "\nCourse: %d", student->course);
  535.     fprintf(file, "\nGroup: %d", student->group);
  536.  
  537.     fprintf(file, "\nFirst semester marks: ");
  538.     int k = 0;
  539.     for (k = 0; k < 4; k++)
  540.         fprintf(file, "%d ", student->marks1[k]);
  541.  
  542.     fprintf(file, "\nRating place in first semester: %d", student->ratingPlace1);
  543.  
  544.     fprintf(file, "\nAverage point in first semester: %.2f", student->average1);
  545.  
  546.     if (updated == 1)
  547.     {
  548.         fprintf(file, "\nSecond semester marks: ");
  549.         for (k = 0; k < 4; k++)
  550.             fprintf(file, "%d ", student->marks2[k]);
  551.  
  552.         fprintf(file, "\nRating place in second semester: %d", student->ratingPlace2);
  553.  
  554.         fprintf(file, "\nRating increase: ");
  555.         if (student->ratingDelta > 0)
  556.             fprintf(file, "+");
  557.         fprintf(file, "%d", student->ratingDelta);
  558.  
  559.         fprintf(file, "\nAverage in second semester: %.2f", student->average2);
  560.  
  561.         fprintf(file, "\nAverage point increase: ");
  562.         if (student->averageDelta > 0)
  563.             fprintf(file, "+");
  564.         fprintf(file, "%.2f", student->averageDelta);
  565.     }
  566.  
  567.     fprintf(file, "\n\n");
  568. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement