#include<stdio.h>
int sp=-1,stack[20],i=0,op,res=0,r1; char exp[20]; void main() { clrscr(); printf("\nEnter Postfix Expression :"); gets(exp); for(i=0;exp[i] != '\0';i++) { if(exp[i] == '+' || exp[i] == '-' || exp[i] == '*' || exp[i] == '/') { flushall(); op = stack[sp] ; sp--; r1 = stack[sp] ; switch(exp[i]) { case '+' : res = op + r1;break; case '-' : res = op - r1;break; case '*' : res = op * r1;break; case '/' : res = op / r1;break; } stack[sp] = res; } else { sp++; stack[sp] = exp[i] - 48; } } printf("\n Result Is : %d ",res); getch(); }
0 Comments