Advertisement
Guest User

1

a guest
Feb 24th, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. char postfix_str[1001] = { 0 }; //the reason why i lost 2 weeks
  8. //stack_begin
  9.  
  10. struct list
  11. {
  12.     char data;
  13.     struct list * next;
  14. };
  15. typedef struct stack
  16. {
  17.     struct list *top;
  18. } stack;
  19.  
  20. stack *construct()
  21. {
  22.     stack *_stack;
  23.     _stack = (stack*)malloc(sizeof(stack));
  24.     _stack->top = NULL;
  25.     return _stack;
  26. }
  27.  
  28. void push(stack *_stack, int a)
  29. {
  30.     struct list *p;
  31.     p = (struct list *) malloc(sizeof(struct list));
  32.     p->data = a;
  33.     p->next = _stack->top;
  34.     _stack->top = p;
  35. }
  36.  
  37. char top(stack *_stack)
  38. {
  39.     return (_stack->top->data);
  40. }
  41.  
  42. int is_empty(stack *_stack)
  43. {
  44.     if (_stack->top == NULL)
  45.         return 1;
  46.     return 0;
  47. }
  48.  
  49. char pop(stack *_stack)
  50. {
  51.     char a;
  52.     struct list *p;
  53.     p = _stack->top;
  54.     a = p->data;
  55.     _stack->top = p->next;
  56.     free(p);
  57.     return a;
  58. }
  59.  
  60. //stack_end
  61.  
  62. int get_priority(char symbol)
  63. {
  64.     char operations[] = "()+-*/";
  65.     for (size_t priority = 0; operations[priority] != '\0'; priority++)
  66.     {
  67.         if (symbol == operations[priority])
  68.             return priority / 2;
  69.     }
  70.     return -1;
  71. }
  72.  
  73.  
  74. char *to_postfix(char infix_str[])
  75. {
  76.     int priority = 0;
  77.     stack *postfix_stack = construct();
  78.     int j = 0;
  79.     for (int i = 0; infix_str[i] != '\0'; i++)
  80.     {
  81.         priority = get_priority(infix_str[i]);
  82.         if (priority == -1)
  83.         {
  84.             postfix_str[j++] = infix_str[i];
  85.             continue;
  86.         }
  87.         if (infix_str[i] == '(')
  88.         {
  89.             push(postfix_stack, infix_str[i]);
  90.             continue;
  91.         }
  92.         if (infix_str[i] == ')')
  93.         {
  94.             while (top(postfix_stack) != '(')
  95.             {
  96.                 postfix_str[j++] = pop(postfix_stack);
  97.             }
  98.             pop(postfix_stack);
  99.             continue;
  100.         }
  101.         while (is_empty(postfix_stack) == 0 && priority <= get_priority(top(postfix_stack)))
  102.         {
  103.             postfix_str[j++] = pop(postfix_stack);
  104.         }
  105.         push(postfix_stack, infix_str[i]);
  106.     }
  107.     while (is_empty(postfix_stack) == 0)
  108.     {
  109.         postfix_str[j++] = pop(postfix_stack);
  110.     }
  111.     free(postfix_stack);
  112.     return postfix_str;
  113. }
  114.  
  115.  
  116. int main()
  117. {
  118.     freopen("input.txt", "r", stdin);
  119.     freopen("output.txt", "w", stdout);
  120.     char in_str[1001] = { 0 };
  121.     gets_s(in_str, 1001);
  122.     printf("%s", to_postfix(in_str));
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement