Advertisement
3axap_010

source.cpp

May 21st, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.42 KB | None | 0 0
  1. #include "pch.h"
  2. #include "Header.h"
  3.  
  4. char* get_line()
  5. {
  6.     int lenght = 0;
  7.     char c;
  8.  
  9.     char* line = (char*)malloc(sizeof(char));
  10.  
  11.     do
  12.     {
  13.         c = _getch();
  14.         fprintf(stdout, "%c", c);
  15.         if (c == '\b' && lenght >= 1)
  16.         {
  17.             line = (char*)realloc(line, sizeof(char) * (lenght - 1));
  18.             line = (char*)realloc(line, sizeof(char) * lenght);
  19.             lenght--;
  20.         }
  21.  
  22.         if (c != '\n' && c != '\r' && c != '\b')
  23.         {
  24.             line = (char*)realloc(line, sizeof(char) * (++lenght + 1));
  25.             line[lenght - 1] = c;
  26.         }
  27.     } while (c != '\n' && c != '\r');
  28.  
  29.     line[lenght] = '\0';
  30.     fprintf(stdout, "\n");
  31.  
  32.     return line;
  33. }
  34.  
  35. void push(char s, struct list** stack)
  36. {
  37.     struct list* temp = (struct list*)malloc(sizeof(struct list));
  38.     temp->symb = s;
  39.     temp->next = *stack;
  40.     *stack = temp;
  41. }
  42.  
  43. char pop(struct list** stack)
  44. {
  45.     if (*stack == NULL)
  46.     {
  47.         return 0;
  48.     }
  49.  
  50.     char c = (*stack)->symb;
  51.     struct list* temp = *stack;
  52.     *stack = (*stack)->next;
  53.     free(temp);
  54.     return c;
  55. }
  56.  
  57. int evaluate_quantity(char* expr)
  58. {
  59.     int i = 0;
  60.     int open = 0, close = 0;
  61.  
  62.     while (expr[i] != '\0')
  63.     {
  64.         switch (expr[i])
  65.         {
  66.         case '(':
  67.             open++;
  68.             break;
  69.         case '{':
  70.             open++;
  71.             break;
  72.         case '[':
  73.             open++;
  74.             break;
  75.         case ')':
  76.             close++;
  77.             break;
  78.         case '}':
  79.             close++;
  80.             break;
  81.         case ']':
  82.             close++;
  83.             break;
  84.         default:
  85.             break;
  86.         }
  87.         i++;
  88.     }
  89.  
  90.     if (open != close) return 0;
  91.     return 1;
  92. }
  93.  
  94. int check_first_bracket(char* expr)
  95. {
  96.     int i = 0;
  97.     while (expr[i] != '\0' && expr[i] != '(' && expr[i] != '{' && expr[i] != '[')
  98.     {
  99.         if (expr[i] == ')' || expr[i] == ']' || expr[i] == '}')
  100.         {
  101.             return 0;
  102.         }
  103.         i++;
  104.     }
  105.     return 1;
  106. }
  107.  
  108. int evaluate_expression(char* expr, struct list* stack)
  109. {
  110.     int i = 0;
  111.     int res;
  112.     char c;
  113.  
  114.     res = evaluate_quantity(expr);
  115.     if (!res)
  116.     {
  117.         fprintf(stdout, "Количетсво закрывающихся скобок не соответствует количеству открывающихся\n");
  118.         return 0;
  119.     }
  120.  
  121.     res = check_first_bracket(expr);
  122.     if (!res)
  123.     {
  124.         fprintf(stdout, "Первая скобка является закрывающейся\n");
  125.         return 0;
  126.     }
  127.  
  128.     while (expr[i] != '\0')
  129.     {
  130.         switch (expr[i])
  131.         {
  132.         case '(':
  133.             push(expr[i], &stack);
  134.             break;
  135.         case '{':
  136.             push(expr[i], &stack);
  137.             break;
  138.         case '[':
  139.             push(expr[i], &stack);
  140.             break;
  141.         case ')':
  142.             c = pop(&stack);
  143.             if (c != '(')
  144.             {
  145.                 fprintf(stdout, "Открывающаяся скобка '%c' не соответсвует закрывающейся ')'\n", c);
  146.                 return 0;
  147.             }
  148.             break;
  149.         case '}':
  150.             c = pop(&stack);
  151.             if (c != '{')
  152.             {
  153.                 fprintf(stdout, "Открывающаяся скобка '%c' не соответсвует закрывающейся '}'\n", c);
  154.                 return 0;
  155.             }
  156.             break;
  157.         case ']':
  158.             c = pop(&stack);
  159.             if (c != '[')
  160.             {
  161.                 fprintf(stdout, "Открывающаяся скобка '%c' не соответсвует закрывающейся ']'\n", c);
  162.                 return 0;
  163.             }
  164.             break;
  165.         default:
  166.             break;
  167.         }
  168.         i++;
  169.     }
  170.  
  171.     return 1;
  172. }
  173.  
  174. void free_stack(struct list** stack)
  175. {
  176.     int res = 1;
  177.     do
  178.     {
  179.         res = pop(stack);
  180.     } while (res);
  181. }
  182.  
  183. int finish()
  184. {
  185.     int i, n;
  186.     do
  187.     {
  188.         printf("Продолжить/завершить | 1/0\n");
  189.         i = scanf_s("%d", &n);
  190.         if (!i)
  191.         {
  192.             fprintf(stdout, "Введите целое число\n");
  193.             rewind(stdin);
  194.             continue;
  195.         }
  196.     } while (!i);
  197.  
  198.     return n;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement