// Postfix Evaluation
#include <stdio.h>
void sum();
void diff();
void mult();
void divide();
int stack[50], top = -1;
int main() {
char st[30];
int i;
printf("Enter the postfix expression: ");
scanf("%[^\n]s", st);
for (i = 0; st[i] != '\0'; i++) {
if (st[i] != ' ') {
switch (st[i]) {
case '+':
sum();
break;
case '-':
diff();
break;
case '*':
mult();
break;
case '/':
divide();
break;
default:
top++;
stack[top] = st[i] - '0';
}
}
}
printf("\nThe result is = %d\n", stack[top]);
return 0;
}
void sum() {
int res, op1, op2;
op1 = stack[top];
top--;
op2 = stack[top];
top--;
res = op2 + op1;
top++;
stack[top] = res;
}
void diff() {
int res, op1, op2;
op1 = stack[top];
top--;
op2 = stack[top];
top--;
res = op2 - op1;
top++;
stack[top] = res;
}
void mult() {
int res, op1, op2;
op1 = stack[top];
top--;
op2 = stack[top];
top--;
res = op2 * op1;
top++;
stack[top] = res;
}
void divide() {
int res, op1, op2;
op1 = stack[top];
top--;
op2 = stack[top];
top--;
res = op2 / op1;
top++;
stack[top] = res;
}