Advertisement
3axap_010

source.cpp

Jun 18th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.94 KB | None | 0 0
  1. #include "pch.h"
  2. #include "Header.h"
  3.  
  4. int get_int()
  5. {
  6.     int i, n;
  7.     do
  8.     {
  9.         i = scanf_s("%d", &n);
  10.         if (!i)
  11.         {
  12.             rewind(stdin);
  13.             continue;
  14.         }
  15.     } while (!i);
  16.  
  17.     return n;
  18. }
  19.  
  20. char* get_string()
  21. {
  22.     char c;
  23.     int lenght = 0;
  24.  
  25.     char* string = (char*)malloc(sizeof(char));
  26.  
  27.     while (1)
  28.     {
  29.         rewind(stdin);
  30.         c = _getch();
  31.         fputc(c, stdout);
  32.         if (c == '\b')
  33.         {
  34.             fprintf(stdout, " ");
  35.             fputc(c, stdout);
  36.             if (lenght >= 1)
  37.             {
  38.                 string = (char*)realloc(string, sizeof(char) * lenght - 1);
  39.                 string = (char*)realloc(string, sizeof(char) * lenght);
  40.                 lenght--;
  41.             }
  42.         }
  43.  
  44.         if (c != '\n' && c != '\r')
  45.         {
  46.             if (c != '\b')
  47.             {
  48.                 string[lenght++] = c;
  49.                 string = (char*)realloc(string, sizeof(char) * (lenght + 1));
  50.             }
  51.         }
  52.         else
  53.         {
  54.             string[lenght] = '\0';
  55.             break;
  56.         }
  57.     }
  58.     fprintf(stdout, "\n");
  59.  
  60.     return string;
  61. }
  62.  
  63. int get_year()
  64. {
  65.     int year;
  66.  
  67.     do
  68.     {
  69.         year = get_int();
  70.     } while (year < 1 || year > 11);
  71.  
  72.     return year;
  73. }
  74.  
  75. int get_positive()
  76. {
  77.     int n;
  78.  
  79.     do
  80.     {
  81.         n = get_int();
  82.     } while (n <= 0);
  83.  
  84.     return n;
  85. }
  86.  
  87. int* get_ints(int size)
  88. {
  89.     int* array = (int*)malloc(sizeof(int) * size);
  90.     int i;
  91.  
  92.     for (i = 0; i < size; i++)
  93.     {
  94.         array[i] = get_positive();
  95.     }
  96.  
  97.     return array;
  98. }
  99.  
  100. struct student* get_student()
  101. {
  102.     struct student* stud = (struct student*)malloc(sizeof(struct student));
  103.  
  104.     printf("Введите имя ученика:");
  105.     stud->name = get_string();
  106.     printf("Введите фамилию ученикa:");
  107.     stud->surname = get_string();
  108.     printf("Введите отчесто ученика:");
  109.     stud->patronimyc = get_string();
  110.     printf("Введите год обучения(номер класса):");
  111.     stud->year = get_year();
  112.     printf("Введите букву класса:");
  113.     rewind(stdin);
  114.     stud->letter = _getch();
  115.     fputc(stud->letter, stdout);
  116.     printf("\n");
  117.     printf("Введите количество предметов:");
  118.     stud->marks_numb = get_positive();
  119.     printf("Введите оценки:\n");
  120.     stud->marks = get_ints(stud->marks_numb);
  121.  
  122.     return stud;
  123. }
  124.  
  125. struct student* student_delete(struct student* stud)
  126. {
  127.     if (stud != NULL)
  128.     {
  129.         free(stud->name);
  130.         free(stud->surname);
  131.         free(stud->patronimyc);
  132.         free(stud->marks);
  133.         free(stud);
  134.     }
  135.  
  136.     stud = NULL;
  137.     return stud;
  138. }
  139.  
  140. void student_print(struct student* stud)
  141. {
  142.     if (stud == NULL)
  143.     {
  144.         return;
  145.     }
  146.  
  147.     printf("%d\t%c %s\t%s\t%s", stud->year, stud->letter, stud->name, stud->surname, stud->patronimyc);
  148.  
  149.     printf("\tОценки: ");
  150.  
  151.     for (int i = 0; i < stud->marks_numb; i++)
  152.     {
  153.         printf("%2d ", stud->marks[i]);
  154.     }
  155.     printf("\n");
  156. }
  157.  
  158. int students_cmp(struct student* stud1, struct student* stud2)
  159. {
  160.     if (stud1 == NULL || stud2 == NULL) return 0;
  161.  
  162.     if (strcmp(stud1->name, stud2->name) != 0) return 0;
  163.     if (strcmp(stud1->surname, stud2->surname) != 0) return 0;
  164.     if (strcmp(stud1->patronimyc, stud2->patronimyc) != 0) return 0;
  165.     if (stud1->year != stud2->year || stud1->letter != stud2->letter) return 0;
  166.     if (stud1->marks_numb != stud2->marks_numb) return 0;
  167.  
  168.     for (int i = 0; i < stud1->marks_numb; i++)
  169.     {
  170.         if (stud1->marks[i] != stud2->marks[i]) return 0;
  171.     }
  172.  
  173.     return 1;
  174. }
  175.  
  176. void student_copy(struct student* source, struct student* destinition)
  177. {
  178.     if (!source) return;
  179.  
  180.     destinition->letter = source->letter;
  181.     destinition->marks_numb = source->marks_numb;
  182.     destinition->year = source->year;
  183.  
  184.     destinition->name = (char*)malloc(sizeof(char) * (strlen(source->name) + 1));
  185.     strcpy(destinition->name, source->name);
  186.  
  187.     destinition->surname = (char*)malloc(sizeof(char) * (strlen(source->surname) + 1));
  188.     strcpy(destinition->surname, source->surname);
  189.  
  190.     destinition->patronimyc = (char*)malloc(sizeof(char) * (strlen(source->patronimyc) + 1));
  191.     strcpy(destinition->patronimyc, source->patronimyc);
  192.  
  193.     destinition->marks = (int*)malloc(sizeof(int) * destinition->marks_numb);
  194.  
  195.     int i;
  196.     for (i = 0; i < destinition->marks_numb; i++)
  197.     {
  198.         destinition->marks[i] = source->marks[i];
  199.     }
  200. }
  201.  
  202. struct list* add_element(struct list* ring, struct student* stud)
  203. {
  204.     if (!ring)
  205.     {
  206.         ring = (struct list*)malloc(sizeof(struct list));
  207.         ring->stud = (struct student*)malloc(sizeof(struct student));
  208.         student_copy(stud, ring->stud);
  209.         ring->left = ring;
  210.         ring->right = ring;
  211.         return ring;
  212.     }
  213.  
  214.     struct list* temp = ring->left;
  215.     ring->left = (struct list*)malloc(sizeof(struct list));
  216.     ring->left->stud = (struct student*)malloc(sizeof(struct student));
  217.     student_copy(stud, ring->left->stud);
  218.     ring->left->left = temp;
  219.     ring->left->right = ring;
  220.     temp->right = ring->left;
  221.  
  222.     return ring;
  223. }
  224.  
  225. struct list* move(struct list* el, char direction)
  226. {
  227.     if (direction == 'l')
  228.     {
  229.         el = el->left;
  230.     }
  231.     else
  232.     {
  233.         el = el->right;
  234.     }
  235.  
  236.     return el;
  237. }
  238.  
  239. void show(struct list* ring, char direction)
  240. {
  241.     if (!ring)
  242.     {
  243.         return;
  244.     }
  245.  
  246.     struct list* temp = ring;
  247.  
  248.     printf("Класс Буква Имя Фамилия Отчество Оценки\n");
  249.  
  250.     do
  251.     {
  252.         student_print(temp->stud);
  253.         temp = move(temp, direction);
  254.     } while (temp != ring);
  255. }
  256.  
  257. struct list* search(struct list* head, struct student* stud)
  258. {
  259.     if (!head || students_cmp(head->stud, stud))
  260.     {
  261.         return head;
  262.     }
  263.  
  264.     struct list* temp = head;
  265.  
  266.     do
  267.     {
  268.         if (students_cmp(temp->stud, stud))
  269.         {
  270.             return temp;
  271.         }
  272.  
  273.         temp = move(temp, 'r');
  274.     } while (temp != head);
  275.  
  276.     return NULL;
  277. }
  278.  
  279. struct list* delete_node(struct list* ring, struct student* stud)
  280. {
  281.     struct list* temp = search(ring, stud);
  282.     if (!temp)
  283.     {
  284.         return ring;
  285.     }
  286.  
  287.     if (temp == ring)
  288.     {
  289.         if (ring->left == ring)
  290.         {
  291.             student_delete(ring->stud);
  292.             free(ring);
  293.             return NULL;
  294.         }
  295.  
  296.         ring->left->right = ring->right;
  297.         ring->right->left = ring->left;
  298.         ring = ring->right;
  299.         student_delete(temp->stud);
  300.         free(temp);
  301.         return ring;
  302.     }
  303.  
  304.     temp->left->right = temp->right;
  305.     temp->right->left = temp->left;
  306.     student_delete(temp->stud);
  307.     free(temp);
  308.  
  309.     return ring;
  310. }
  311.  
  312. void ring_free(struct list** ring)
  313. {
  314.     while (*ring)
  315.     {
  316.         *ring = delete_node(*ring, (*ring)->stud);
  317.     }
  318. }
  319.  
  320. void sort_by_value(struct list* head)
  321. {
  322.     struct list* i = head, *j;
  323.     struct list* min_p;
  324.     struct student* temp;
  325.     int min;
  326.  
  327.     do
  328.     {
  329.         min = i->stud->year;
  330.         min_p = i;
  331.  
  332.         for (j = i->right; j != head; j = j->right)
  333.         {
  334.             if (j->stud->year < min)
  335.             {
  336.                 min = j->stud->year;
  337.                 min_p = j;
  338.             }
  339.         }
  340.  
  341.         if (min_p != i)
  342.         {
  343.             temp = i->stud;
  344.             i->stud = min_p->stud;
  345.             min_p->stud = temp;
  346.         }
  347.  
  348.  
  349.         i = i->right;
  350.     } while (i->right != head);
  351. }
  352.  
  353. struct list* sort_by_pointer(struct list* head)
  354. {
  355.     struct list* i = head;
  356.     struct list* min_p, *j;
  357.     int min;
  358.  
  359.     do
  360.     {
  361.         min_p = i;
  362.         min = i->stud->year;
  363.  
  364.         for (j = i->right; j != head; j = j->right)
  365.         {
  366.             if (min > j->stud->year)
  367.             {
  368.                 min_p = j;
  369.                 min = j->stud->year;
  370.             }
  371.         }
  372.  
  373.         if (min_p != i)
  374.         {
  375.             if (i == head)
  376.             {
  377.                 head = min_p;
  378.                 i = head->right;
  379.             }
  380.             else
  381.             {
  382.                 min_p->left->right = min_p->right;
  383.                 min_p->right->left = min_p->left;
  384.                 i->left->right = min_p;
  385.                 min_p->right = i;
  386.                 min_p->left = i->left;
  387.                 i->left = min_p;
  388.             }
  389.         }
  390.         else
  391.         {
  392.             i = i->right;
  393.         }
  394.     } while (i->right != head);
  395.  
  396.     return head;
  397. }
  398.  
  399. int choose()
  400. {
  401.     int n;
  402.  
  403.     do
  404.     {
  405.         printf("Выберите действие:\n");
  406.         n = get_int();
  407.     } while (n < 1 || n > 9);
  408.  
  409.     return n;
  410. }
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417. struct list* search_by_int(struct list* head, int parametr, int n)
  418. {
  419.     if (head == NULL)
  420.         return NULL;
  421.  
  422.     struct list* t = head;
  423.  
  424.     do
  425.     {
  426.         switch (parametr)
  427.         {
  428.         case 1:
  429.             if (t->stud->year == n)
  430.             {
  431.                 return t;
  432.             }
  433.             break;
  434.         case 2:
  435.             if (t->stud->marks_numb == n)
  436.             {
  437.                 return t;
  438.             }
  439.         default:
  440.             break;
  441.         }
  442.  
  443.         t = t->right;
  444.     } while (t != head);
  445.  
  446.     return NULL;
  447. }
  448.  
  449. struct list* search_by_string(struct list* head, int parametr, char* n)
  450. {
  451.     if (head == NULL)
  452.         return NULL;
  453.  
  454.     struct list* t = head;
  455.  
  456.     do
  457.     {
  458.         switch (parametr)
  459.         {
  460.         case 1:
  461.             if (strcmp(t->stud->name, n) == 0)
  462.             {
  463.                 return t;
  464.             }
  465.             break;
  466.         case 2:
  467.             if (strcmp(t->stud->surname, n) == 0)
  468.             {
  469.                 return t;
  470.             }
  471.             break;
  472.         case 3:
  473.             if (strcmp(t->stud->patronimyc, n) == 0)
  474.             {
  475.                 return t;
  476.             }
  477.             break;
  478.         default:
  479.             break;
  480.         }
  481.  
  482.         t = t->right;
  483.     } while (t != head);
  484.  
  485.     return NULL;
  486. }
  487.  
  488. struct list* search_by_char(struct list* head, char n)
  489. {
  490.     if (head == NULL) return NULL;
  491.  
  492.     struct list* t = head;
  493.  
  494.     do
  495.     {
  496.         if (t->stud->letter == n)
  497.         {
  498.             return t;
  499.         }
  500.  
  501.         t = t->right;
  502.     } while (t != head);
  503.  
  504.     return NULL;
  505. }
  506.  
  507. struct list* search_by_any(struct list* head)
  508. {
  509.     printf("1)Поиск по году обучению\n");
  510.     printf("2)Поиск по количеству оценок\n");
  511.     printf("3)Поиск по имени\n");
  512.     printf("4)Поиск по фамилии\n");
  513.     printf("5)Поиск по отчеству\n");
  514.     printf("6)Поиск по букве класса\n");
  515.  
  516.     int ch = get_int();
  517.     int n;
  518.     char c;
  519.     char* str = NULL;
  520.     struct list* temp = NULL;
  521.  
  522.     switch (ch)
  523.     {
  524.     case 1: //поиск по году
  525.         printf("Введите год для поиска:\n");
  526.         n = get_int();
  527.         temp = search_by_int(head, 1, n);
  528.         break;
  529.     case 2: //поиск по количеству оценок
  530.         printf("Введите кол-во оценок для поиска:\n");
  531.         n = get_int();
  532.         temp = search_by_int(head, 2, n);
  533.         break;
  534.     case 3: //поиск по имени
  535.         printf("Введите имя для поиска:\n");
  536.         str = get_string();
  537.         temp = search_by_string(head, 1, str);
  538.         break;
  539.     case 4: //поиск по фамилии
  540.         printf("Введите фамилию для поиска:\n");
  541.         str = get_string();
  542.         temp = search_by_string(head, 2, str);
  543.         break;
  544.     case 5: //поиск по фамилии
  545.         printf("Введите отчество для поиска:\n");
  546.         str = get_string();
  547.         temp = search_by_string(head, 3, str);
  548.         break;
  549.     case 6: //поиск по букве классаЧ
  550.         printf("Введите буквy класса для поиска:\n");
  551.         c = _getch();
  552.         fputc(c, stdout);
  553.         printf("\n");
  554.         temp = search_by_char(head, c);
  555.         break;
  556.     default:
  557.         break;
  558.     }
  559.  
  560.     return temp;
  561. }
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570. void info()
  571. {
  572.     printf("1)Добавление элемента в кольцо\n");
  573.     printf("2)Удаление элемента из кольца\n");
  574.     printf("3)Вывод справа налево\n");
  575.     printf("4)Вывод слева направо\n");
  576.     printf("5)Сортировка по указателю\n");
  577.     printf("6)Сортировка по значению\n");
  578.     printf("7)Поиск элемента\n");
  579.     printf("8)Закончить\n");
  580.     printf("9)Поиск по выбранному параметру\n");
  581. }
  582.  
  583. void choose(struct list** ring, struct student* stud)
  584. {
  585.     struct list* temp = NULL;
  586.     int ch;
  587.     info();
  588.  
  589.     do
  590.     {
  591.         ch = choose();
  592.  
  593.         switch (ch)
  594.         {
  595.         case 1:
  596.             stud = get_student();
  597.             *ring = add_element(*ring, stud);
  598.             student_delete(stud);
  599.             break;
  600.         case 2:
  601.             stud = get_student();
  602.             *ring = delete_node(*ring, stud);
  603.             stud = student_delete(stud);
  604.             break;
  605.         case 3:
  606.             show(*ring, 'r');
  607.             break;
  608.         case 4:
  609.             show(*ring, 'l');
  610.             break;
  611.         case 5:
  612.             *ring = sort_by_pointer(*ring);
  613.             break;
  614.         case 6:
  615.             sort_by_value(*ring);
  616.             break;
  617.         case 7:
  618.             stud = get_student();
  619.             temp = search(*ring, stud);
  620.             stud = student_delete(stud);
  621.             if (!temp)
  622.             {
  623.                 printf("Элемент не сществует\n");
  624.             }
  625.             else
  626.             {
  627.                 printf("Элемент существует\n");
  628.             }
  629.             break;
  630.         case 8:
  631.             ring_free(ring);
  632.             break;
  633.         case 9:
  634.             temp = search_by_any(*ring);
  635.             if (temp == NULL)
  636.             {
  637.                 printf("Элемент не найден\n");
  638.             }
  639.             else
  640.             {
  641.                 printf("Элемент найден\n");
  642.                 printf("Класс Буква Имя Фамилия Отчество Оценки\n");
  643.                 student_print(temp->stud);
  644.             }
  645.             break;
  646.         default:
  647.             break;
  648.         }
  649.  
  650.     } while (ch != 8);
  651. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement