Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //evolution if postfix
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- #include<math.h>
- #define size 100
- int top=-1,ans;
- char postfix[size];
- char stack[size];
- void push(int ch)
- {
- top++;
- stack[top]=ch;
- }
- int pop()
- {
- int ch;
- ch=(int)stack[top];
- top--;
- return ch;
- }
- int main()
- {
- int i,j=0,a=0,b=0;
- printf("\n Enter the postfix expression: ");
- scanf("%s",postfix);
- for(i=0;i<strlen(postfix);i++)
- {
- switch(postfix[i])
- {
- case '^':
- a=pop();
- b=pop();
- ans=pow(b,a);
- push(ans);
- break;
- case '*':
- a=pop();
- b=pop();
- ans=b*a;
- push(ans);
- break;
- case '/':
- a=pop();
- b=pop();
- ans=b/a;
- push(ans);
- break;
- case '+':
- a=pop();
- b=pop();
- ans=b+a;
- push(ans);
- break;
- case '-':
- a=pop();
- b=pop();
- ans=b-a;
- push(ans);
- break;
- default:
- push(postfix[i]-'0');
- break;
- }
- printf("\n %c \t %s \t %d \t %d \t %d",postfix[i],stack,a,b,ans);
- }
- ans=(int)pop();
- printf("\n ANs=> %d",ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement