Wow_Rasl

Untitled

Jun 2nd, 2022
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.47 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS //подключаем необходимые библиотеки
  2. #include <stdio.h> //ввоод/вывод
  3. #include <locale.h> //русский язык
  4. #include <string.h> //функции для работы со строками
  5. #include <stdlib.h> //выделение памяти
  6. #include <math.h>
  7.  
  8. typedef struct Node {
  9.     int degree;
  10.     int val;
  11.     struct Node* next;
  12. } Node;
  13.  
  14.  
  15. void PrintPolinomyal(Node* head) {
  16.     int cnt = 0;
  17.     Node* p = head;
  18.     if (!p) {
  19.         printf("Пустой многочлен\n");
  20.         return;
  21.     }
  22.     if (p->val > 0) {
  23.         printf("%dx^%d ", p->val, p->degree);
  24.         ++cnt;
  25.     }
  26.     p = p->next;
  27.     for (; p != NULL; p = p->next) {
  28.         if (p->val > 0) {
  29.             if (cnt > 0) {
  30.                 printf("+%dx^%d ", p->val, p->degree);
  31.                 ++cnt;
  32.             }
  33.             else {
  34.                 printf("%dx^%d ", p->val, p->degree);
  35.                 ++cnt;
  36.             }
  37.         }
  38.         else if (p->val < 0) {
  39.             printf("%dx^%d ", p->val, p->degree);
  40.             ++cnt;
  41.         }
  42.     }
  43.     printf("\n");
  44. }
  45.  
  46. Node* addElement(Node* head, int value, int degree, unsigned int position)
  47. {
  48.     Node* new_node, * tmp;
  49.     int i = 0;
  50.     new_node = (Node*)malloc(sizeof(Node));
  51.     new_node->degree = degree;
  52.     new_node->val = value;
  53.     new_node->next = NULL;
  54.     if (!head)
  55.         return new_node;
  56.  
  57.     if (!position)
  58.     {
  59.         new_node->next = head;
  60.         return new_node;
  61.     }
  62.  
  63.     tmp = head;
  64.     while (tmp->next && i < position - 1)
  65.     {
  66.         tmp = tmp->next;
  67.         i++;
  68.     }
  69.  
  70.     if (!tmp->next)
  71.     {
  72.         tmp->next = new_node;
  73.         return head;
  74.     }
  75.     new_node->next = tmp->next;
  76.     tmp->next = new_node;
  77.     return head;
  78. }
  79.  
  80. Node* ClearList(Node* L) {
  81.     Node* next, * tmp = L;
  82.  
  83.     while (tmp)
  84.     {
  85.         next = tmp->next;
  86.         free(tmp);
  87.         tmp = next;
  88.     }
  89.  
  90.     L = NULL;
  91.  
  92.     printf("Лист очищен\n");
  93. }
  94.  
  95. Node* deleteElement(Node* head, int position) {
  96.     Node* elm = head, * tmp;
  97.     int i = 0;
  98.  
  99.     if (!elm) {
  100.         printf("List is empty\n\n");
  101.         return head;
  102.     }
  103.     //удаляем голову
  104.     if (!position) {
  105.         elm = head->next;
  106.         head->next = NULL;
  107.         free(head);
  108.         return elm;
  109.     }
  110.  
  111.     while (elm != NULL && i < position - 1) {
  112.         elm = elm->next;
  113.         i++;
  114.     }
  115.  
  116.     //если индекс больше длины списка, ничего не делаем
  117.     if (!elm) {
  118.         return head;
  119.     }
  120.     if (!elm->next) {
  121.         return head;
  122.     }
  123.  
  124.     tmp = elm->next;
  125.  
  126.     if ((elm->next)->next) {
  127.         elm->next = (elm->next)->next;
  128.         tmp->next = NULL;
  129.     }
  130.     else {
  131.         elm->next = NULL;
  132.     }
  133.     free(tmp);
  134.     return head;
  135. }
  136.  
  137. Node* ChangePolinomyal(Node* L) {
  138.     int ind = 0;
  139.     Node* p = L;
  140.     for (; p != NULL;) {
  141.         if (p->degree % 2 == 0) {
  142.             L = deleteElement(L, ind);
  143.             p = L;
  144.             ind = 0;
  145.         }
  146.         else {
  147.             ind++;
  148.             p = p->next;
  149.         }
  150.     }
  151.     return L;
  152. }
  153.  
  154.  
  155. Node* CreatePolinomyal() {
  156.     printf("Введите количество членов в многочлене L:\n");
  157.     int n;
  158.     scanf("%d", &n);
  159.     printf("Почленно введите данные многочлена:\n");
  160.     Node* L = NULL;
  161.     for (int i = 0; i < n; ++i) {
  162.         int tmp_degree, tmp_val;
  163.         scanf("%d%d", &tmp_val, &tmp_degree);
  164.         Node* p = L;
  165.         int flag = 0;
  166.         for (int j = 0; j < i, p != NULL; j++) {
  167.             if (tmp_degree == p->degree) {
  168.                 p->val += tmp_val;
  169.                 flag = 1;
  170.             }
  171.             p = p->next;
  172.         }
  173.         if (flag == 0) {
  174.             L = addElement(L, tmp_val, tmp_degree, i);
  175.         }
  176.     }
  177.     return L;
  178. }
  179.  
  180.  
  181.  
  182. int main() {
  183.     setlocale(LC_ALL, "rus");
  184.     Node* L;
  185.     L = CreatePolinomyal();
  186.     PrintPolinomyal(L);
  187.     L = ChangePolinomyal(L);
  188.     printf("Измененный многочлен:\n");
  189.     PrintPolinomyal(L);
  190.     ClearList(L);
  191. }
  192.  
  193.  
Advertisement
Add Comment
Please, Sign In to add comment