cl1p.net - The internet clipboard
Login/Sign Up
cl1p.net/postfix
cl1p.net/postfix
Login/Sign Up
This cl1p will be deleted in in 21 days.
Copy
#include
#include
#include
typedef struct stack { int stck[100]; int top; }stack; int pop(stack *s) { return s->stck[(s->top)--]; } int isempty(stack *s) { if(s->top==-1) return 0; else return 1; } void push(stack *s,int value) { s->top++; s->stck[s->top]=value; } int evaluate(char exp[]) { stack s1; s1.top=-1; int i=0,a,b,res; while(exp[i]!='\0') { if(isdigit(exp[i])) { push(&s1,exp[i]-'0'); } else { switch(exp[i]) { case '+': a=pop(&s1); b=pop(&s1); res=(b)+(a); break; case '-': a=pop(&s1); b=pop(&s1); res=b-a; break; case '/': a=pop(&s1); b=pop(&s1); res=b/a; break; case '*': a=pop(&s1); b=pop(&s1); res=b*a; break; } push(&s1,res); } i=i+1; } return pop(&s1); } int main() { char exp[100]; printf("Enter the post fix exp\n"); scanf("%s",exp); int res=evaluate(exp); printf("res=%d\n",res); return 0; }