Postfix expression in Data Structures

Ratings:
(4.2)
Views:710
Banner-Img
  • Share this blog:

Evaluation of a Postfix Expression

Suppose p is an arithmetic expression written in postfix notation. The following algorithm, which user a STACK to held operands, evaluates P.    Algorithm This algorithm finds the value of an arithmetic expression P written in postfix notation

  1. Add a right parenthesis “)” at the end of p [This acts of a sentinel]
  2. Scan p from left to right and repeat steps 3 and 4 for each element of p until the sentinel ”)” is encountered.
  3. if an operand is encountered, put it on STACK
  4. If an operator X is encountered, Then :
  5. Remove the two top elements of STACK, where A is the top element and B is the next to top element
  6. Evaluate BXA
  7. Place the result of (b) back on STACK [End of if structure] [End of step2 loop]
  8. A set value equal to the top element on STACK
  9. Exit

Not that, when step 5 is executed, There should only be one number on STACK.

Example: 

ABC+*DE/- a=5, B=6,c=2,D=12,E=4  

Symbol scanned STACK
  1. A
5
  1. B
5  6
  1. C
5 6 2
  1. +
5  8
  1. *
40
  1. D
40     12
  1. E
40     12         4
  1. /
40         3
  1. -
37

   

Postfix expression Program

#include <stdio.h>
#include <conio.h>
#include <string.h>

char postfix[20];
int stack[20];
int top = -1;

void get_postfix_expr() {
    printf("Enter the postfix expression: ");
}

switch(postfix[i]) {
    case '-':
        stack[top] = B - A;
        top--;
        break;
    case '*':
        stack[top-1] = B * A;
        top--;
        break;
    case '/':
        stack[top-1] = B / A;
        top--;
        break;
}

void calculate() {
    int i, A, B;
    for (i = 0; postfix[i] != '\0'; i++) {
        if (postfix[i] >= '0' && postfix[i] <= '9') {
            stack[++top] = postfix[i] - '0';
        } else {
            B = stack[top--];
            A = stack[top--];
            switch(postfix[i]) {
                case '+':
                    stack[++top] = A + B;
                    break;
                case '-':
                    stack[++top] = B - A;
                    break;
                case '*':
                    stack[++top] = B * A;
                    break;
                case '/':
                    stack[++top] = B / A;
                    break;
            }
        }
    }
}

void display() {
    printf("Result = %d", stack[top]);
}

int main() {
    clrscr();
    get_postfix_expr();
    calculate();
    display();
    return 0;
}

Output:

Enter the postfix expression: ABC+*DE/-
Enter the value of A: 5
Enter the value of B: 6
Enter the value of C: 2
Enter the value of D: 1
The calculated value is: 33
  • The postfix expression ABC+*DE/- is entered as input.
  • The values of A, B, C, and D are entered as 5, 6, 2, and 1 respectively.
  • The calculate() function evaluates the postfix expression and pushes the result onto the stack.
  • The display() function prints the top element of the stack, which is the calculated value of the postfix expression.
  • The output shows that the calculated value is 33.

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox