1// Aim: Program to perform Postfix Evaluation.
2#include<stdio.h>
3#include<stdlib.h>
4#include<ctype.h>
5
6int stack[20];
7int top = -1;
8
9void push(int x) {
10 stack[++top] = x;
11}
12
13int pop() {
14 return stack[top--];
15}
16
17int evaluatePostfix(char* postfix) {
18 int i;
19 char symbol;
20 int op1, op2;
21
22 for(i = 0; postfix[i] != '\0'; i++) {
23 symbol = postfix[i];
24 if(isdigit(symbol)) {
25 push(symbol - '0');
26 } else {
27 op2 = pop();
28 op1 = pop();
29 switch(symbol) {
30 case '+': push(op1 + op2); break;
31 case '-': push(op1 - op2); break;
32 case '*': push(op1 * op2); break;
33 case '/': push(op1 / op2); break;
34 }
35 }
36 }
37 return pop();
38}
39
40int main() {
41 char postfix[20];
42 printf("Enter the postfix expression\n");
43 scanf("%s", postfix);
44
45 int result = evaluatePostfix(postfix);
46 printf("Postfix expression is %s\n", postfix);
47 printf("Evaluation result is %d\n", result);
48
49 return 0;
50}