Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include "Header.h"
- char* get_line()
- {
- int lenght = 0;
- char c;
- char* line = (char*)malloc(sizeof(char));
- do
- {
- c = _getch();
- fprintf(stdout, "%c", c);
- if (c == '\b' && lenght >= 1)
- {
- line = (char*)realloc(line, sizeof(char) * (lenght - 1));
- line = (char*)realloc(line, sizeof(char) * lenght);
- lenght--;
- }
- if (c != '\n' && c != '\r' && c != '\b')
- {
- line = (char*)realloc(line, sizeof(char) * (++lenght + 1));
- line[lenght - 1] = c;
- }
- } while (c != '\n' && c != '\r');
- line[lenght] = '\0';
- fprintf(stdout, "\n");
- return line;
- }
- void push(char s, struct list** stack)
- {
- struct list* temp = (struct list*)malloc(sizeof(struct list));
- temp->symb = s;
- temp->next = *stack;
- *stack = temp;
- }
- char pop(struct list** stack)
- {
- if (*stack == NULL)
- {
- return 0;
- }
- char c = (*stack)->symb;
- struct list* temp = *stack;
- *stack = (*stack)->next;
- free(temp);
- return c;
- }
- int evaluate_quantity(char* expr)
- {
- int i = 0;
- int open = 0, close = 0;
- while (expr[i] != '\0')
- {
- switch (expr[i])
- {
- case '(':
- open++;
- break;
- case '{':
- open++;
- break;
- case '[':
- open++;
- break;
- case ')':
- close++;
- break;
- case '}':
- close++;
- break;
- case ']':
- close++;
- break;
- default:
- break;
- }
- i++;
- }
- if (open != close) return 0;
- return 1;
- }
- int check_first_bracket(char* expr)
- {
- int i = 0;
- while (expr[i] != '\0' && expr[i] != '(' && expr[i] != '{' && expr[i] != '[')
- {
- if (expr[i] == ')' || expr[i] == ']' || expr[i] == '}')
- {
- return 0;
- }
- i++;
- }
- return 1;
- }
- int evaluate_expression(char* expr, struct list* stack)
- {
- int i = 0;
- int res;
- char c;
- res = evaluate_quantity(expr);
- if (!res)
- {
- fprintf(stdout, "Количетсво закрывающихся скобок не соответствует количеству открывающихся\n");
- return 0;
- }
- res = check_first_bracket(expr);
- if (!res)
- {
- fprintf(stdout, "Первая скобка является закрывающейся\n");
- return 0;
- }
- while (expr[i] != '\0')
- {
- switch (expr[i])
- {
- case '(':
- push(expr[i], &stack);
- break;
- case '{':
- push(expr[i], &stack);
- break;
- case '[':
- push(expr[i], &stack);
- break;
- case ')':
- c = pop(&stack);
- if (c != '(')
- {
- fprintf(stdout, "Открывающаяся скобка '%c' не соответсвует закрывающейся ')'\n", c);
- return 0;
- }
- break;
- case '}':
- c = pop(&stack);
- if (c != '{')
- {
- fprintf(stdout, "Открывающаяся скобка '%c' не соответсвует закрывающейся '}'\n", c);
- return 0;
- }
- break;
- case ']':
- c = pop(&stack);
- if (c != '[')
- {
- fprintf(stdout, "Открывающаяся скобка '%c' не соответсвует закрывающейся ']'\n", c);
- return 0;
- }
- break;
- default:
- break;
- }
- i++;
- }
- return 1;
- }
- void free_stack(struct list** stack)
- {
- int res = 1;
- do
- {
- res = pop(stack);
- } while (res);
- }
- int finish()
- {
- int i, n;
- do
- {
- printf("Продолжить/завершить | 1/0\n");
- i = scanf_s("%d", &n);
- if (!i)
- {
- fprintf(stdout, "Введите целое число\n");
- rewind(stdin);
- continue;
- }
- } while (!i);
- return n;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement