Advertisement
Guest User

Evaluate Postfix

a guest
Jun 24th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include<stdio.h> #include<conio.h> #include<math.h> #include<string.h> void push(int); int pop(); int vstack[100]; int tos=-1; void main() { int i,res,l,op1,op2,value[100]; char postfix[100],ch; clrscr(); printf("Enter a valid postfix\n"); gets(postfix); l=strlen(postfix); for(i=0;i<=l-1;i++) { if(isalpha(postfix[i])) { printf("Enter value of %c",postfix[i]); scanf("%d",&value[i]); push(value[i]); } else { ch=postfix[i]; op2=pop(); op1=pop(); switch(ch) { case '+': push(op1+op2); break; case'-': push(op1-op2); break; case'*': push(op1*op2); break; case'/': push(op1/op2); break; case'$': push(pow(op1,op2)); break; case'%': push(op1%op2); break; } } } printf("The reault is:"); res=pop(); printf("%d", res); getch();
  2. } /***********insertion function*************/ void push(int val) { vstack[++tos]=val; } /***********deletion function***************/ int pop() { int n; n=vstack[tos--]; return(n); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement