Advertisement
Shohag_Rana_Hriday

evaluate postfix expression

Aug 25th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. /* This program is for evaluation of postfix expression
  2.  * This program assume that there are only four operators
  3.  * (*, /, +, -) in an expression and operand is single digit only
  4.  * Further this program does not do any error handling e.g.
  5.  * it does not check that entered postfix expression is valid
  6.  * or not.
  7.  * */
  8.  
  9.  #include<stdio.h>
  10.  #include<ctype.h>
  11.  
  12.  
  13.  # define MAXSTACK 100        /* for max size of stack */
  14.  # define POSTFIXSIZE 100     /* define max number of charcters in postfix expression */
  15.  
  16.  /* declare stack and its top pointer to be used during postfix expression
  17.     evaluation*/
  18.  int stack[MAXSTACK];
  19.  int top = -1 ;             /* because array index in C begins at 0 */
  20.  /* can be do this initialization somewhere else */
  21.  
  22.  /* define push operation */
  23.  void push(int item)
  24.  {
  25.  
  26.      if(top >= MAXSTACK -1)
  27.      {
  28.          printf("stack over flow");
  29.          return;
  30.      }
  31.      else
  32.      {
  33.          top = top + 1 ;
  34.          stack[top]= item;
  35.      }
  36.  }
  37.  
  38.  /* define pop operation */
  39.  int pop()
  40.  {
  41.      int item;
  42.      if(top <0)
  43.      {
  44.         printf("stack under flow");
  45.      }
  46.      else
  47.      {
  48.          item = stack[top];
  49.          top = top - 1;
  50.          return item;
  51.      }
  52.  }
  53.  
  54.  /* define function that is used to input postfix expression and to evaluate it */
  55.  void EvalPostfix(char postfix[])
  56.  {
  57.  
  58.     int i ;
  59.     char ch;
  60.     int val;
  61.     int A, B ;
  62.  
  63.  
  64.     /* evaluate postfix expression */
  65.     for (i = 0 ; postfix[i] != ')'; i++)
  66.     {
  67.         ch = postfix[i];
  68.         if (isdigit(ch))
  69.         {
  70.             /* we saw an operand,push the digit onto stack
  71.             ch - '0' is used for getting digit rather than ASCII code of digit */
  72.             push(ch - '0');
  73.         }
  74.         else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
  75.         {
  76.             /* we saw an operator
  77.             * pop top element A and next-to-top elemnet B
  78.             * from stack and compute B operator A
  79.             */
  80.             A = pop();
  81.             B = pop();
  82.  
  83.             switch (ch) /* ch is an operator */
  84.             {
  85.                 case '*':
  86.                 val = B * A;
  87.                 break;
  88.  
  89.                 case '/':
  90.                 val = B / A;
  91.                 break;
  92.  
  93.                 case '+':
  94.                 val = B + A;
  95.                 break;
  96.  
  97.                 case '-':
  98.                 val = B - A;
  99.                 break;
  100.             }
  101.  
  102.             /* push the value obtained above onto the stack */
  103.             push(val);
  104.         }
  105.     }
  106.     printf( " \n Result of expression evaluation : %d \n", pop()) ;
  107.  }
  108.  
  109.  int main()
  110.  {
  111.  
  112.     int i ;
  113.  
  114.     /* declare character array to store postfix expression */
  115.     char postfix[POSTFIXSIZE];
  116.     printf("ASSUMPTION: There are only four operators(*, /, +, -) in an expression and operand is single digit only.\n");
  117.     printf( " \nEnter postfix expression,\npress right parenthesis ')' for end expression : ");
  118.  
  119.     /* take input of postfix expression from user */
  120.  
  121.     for (i = 0 ; i <= POSTFIXSIZE - 1 ; i++)
  122.     {
  123.         scanf("%c", &postfix[i]);
  124.  
  125.         if ( postfix[i] == ')' ) /* is there any way to eliminate this if */
  126.         { break; } /* and break statement */
  127.     }
  128.  
  129.     /* call function to evaluate postfix expression */
  130.  
  131.     EvalPostfix(postfix);
  132.    
  133.     return 0;
  134.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement