Follow This Blog For more... 😊

program to evaluate postfix expression using stack. |Using C language |Data Structure |

👨‍💻Write a program to evaluate postfix expression using stack.

Click here >>  evaluate prefix expression using stack

Steps for Evaluation of Postfix Expression Using Stack:

  1. Create a stack to store operands (or values). 
  2. Check(scan) the given expression from left to right and do the following for every checked character (or element).
  3. If the character (or element) is a number, push it into the stack 
  4. If the character (or element) is an operator, pop two operands for the operator from the stack to perform an operation and push the result back to the stack.
  5. When the expression is ended (means the upcoming character is NULL), the number in the stack is the final answer.

For Example, the Expression is 456*+


⇛ CODE for evaluating Postfix expressions :


    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<math.h>

    int stack[20];
    int top = -1; /* Declaring global variable 'top' */

    void push(int x)
    {
        stack[++top] = x; /* Here,  ( ++ top ) means that it first Increamet top by 1, then use incremented value of top. */
    }

    int pop()
    {
        return stack[top--]; /* Here,  ( top - - ) means that it first use value of top, then top decrement by 1. */
    }

    int main()
    {
        char exp[20];
        int i=0;
        int n1,n2,n3,num;
        printf("Enter the expression :: ");
        gets(exp);

        while(exp[i] != '\0')
        {
            if(isdigit(exp[i])) /* isdigit() function checks that entered character is Digit or not. [From  ctyp.h  header file]  */
            {
                num = exp[i] - 48;
                push(num);
            }
            else
            {
                n1 = pop(); /* NOTE: In prefix evaluation, first n1 then n2 popped */
                n2 = pop();
                switch(exp[i])
                {
                    case '+':
                    {
                        n3 = n2 + n1;
                        break;
                    }
                    case '-':
                    {
                        n3 = n2 - n1;
                        break;
                    }
                    case '*':
                    {
                        n3 = n2 * n1;
                        break;
                    }
                    case '/':
                    {
                        n3 = n2 / n1;
                        break;
                    }
                    case '^':
                    {
                        n3 = pow(n2,n1); /* pow(x,y) function [From  math.h  header file]  */
                        break;
                    }
                    case '$':
                    {
                        n3 = pow(n2,n1);
                        break;
                    }
                }
                push(n3);
            }
            i++;
        }
        printf("\nThe result of expression %s  =  %d\n\n", exp, pop());
        return 0;
    }


OUTPUT:





Comments

Popular Posts