Advertisement
3axap_010

source.cpp

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