Promi_38

Evaluation of a postfix expression(without LL)

Nov 21st, 2020 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. // 12, 12, 12, +, *, 12, -, 12, +
  2. //is the example of input postfix expression
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. #define MAX_SIZE 10000
  9.  
  10. struct STACK
  11. {
  12.     double data[MAX_SIZE];
  13.     int cur_size = 0;
  14.     void push(double x)
  15.     {
  16.         if (cur_size == MAX_SIZE)
  17.             printf("Full\n");
  18.         else
  19.         {
  20.             data[cur_size] = x;
  21.             cur_size++;
  22.         }
  23.     }
  24.  
  25.     // returns the popped value
  26.     void pop()
  27.     {
  28.         if (cur_size == 0)
  29.             printf("Empty\n");
  30.         else
  31.         {
  32.             cur_size--;
  33.         }
  34.     }
  35. } st;
  36.  
  37. int isOperand(char x)
  38. {
  39.     if (x == '+' || x == '-' || x == '*' || x == '/' || x == '^')
  40.         return 0;
  41.     else
  42.         return 1;
  43. }
  44.  
  45. double eval(char *postfix)
  46. {
  47.     // r stores the value of each operation
  48.     double x1, x2, r = 1;
  49.     int i, j;
  50.     for (i = 0; postfix[i] != '\0'; i++)
  51.     {
  52.         if (postfix[i] == ',')
  53.         {
  54.             double flag = 0, value_of_extracted_int = 0, Positional_value = 1;
  55.             for (j = i - 1; (postfix[j] >= '0' && postfix[j] <= '9') ; j--)
  56.             {
  57.                     value_of_extracted_int += (double)(postfix[j] - '0') * Positional_value;
  58.                     Positional_value *= 10;
  59.                     flag = 1;
  60.             }
  61.             if(flag == 1) st.push(value_of_extracted_int);
  62.            // printf("n %lf\n", value_of_extracted_int);
  63.         }
  64.         else if (isOperand(postfix[i]) == 0)
  65.         {
  66.             // x1 x2
  67.             x2 = st.data[st.cur_size-1];
  68.             st.pop();
  69.             x1 = st.data[st.cur_size-1];
  70.             st.pop();
  71.            //printf("%lf %c %lf = ", x1, postfix[i], x2);
  72.             switch (postfix[i])
  73.             {
  74.             case '+':
  75.                 r = x1 + x2;
  76.                 break;
  77.             case '-':
  78.                 r = x1 - x2;
  79.                 break;
  80.             case '*':
  81.                 r = x1 * x2;
  82.                 break;
  83.             case '/':
  84.                 r = x1 / x2;
  85.                 break;
  86.             case '^':
  87.                 r = 1;
  88.                 for (int i = 0; i < x2; i++)
  89.                     r *= x1;
  90.                 break;
  91.             }
  92.            // printf(" %lf\n", r);
  93.             st.push(r);
  94.         }
  95.     }
  96.     return st.data[0];
  97. }
  98.  
  99. int main()
  100. {
  101.     char post[90000];
  102.     printf("Enter the postix expression: ");
  103.     gets(post);
  104.     printf("The answer is %.0lf\n", eval(post));
  105. }
  106.  
  107. //2, 1, +, 5, 1, -, *, 7, 3, +, /, 2, 3, +, *
Advertisement
Add Comment
Please, Sign In to add comment