Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX 100
- int num[MAX],tos=-1;
- push(int x)
- {
- if(tos==MAX)
- {
- printf("the stack is full");
- }
- else
- {
- printf(" l");
- tos++;
- num[tos]=x;
- }
- }
- int pop()
- {
- if(tos<0)
- {
- printf("stack underflow");
- }
- else
- return num[tos--];
- }
- int main()
- {
- char postfix[MAX],exp[MAX],ch,val;
- int a,b;
- printf("enter the postfix expression");
- fgets(postfix,MAX,stdin);
- strcpy(exp,postfix);
- for(int i=0;i<strlen(postfix);i++)
- {
- printf(" xox ");
- ch=postfix[i];
- if(isdigit(ch))
- {
- push(ch - '0');
- printf(" %d ",atoi(ch));
- }
- else
- {
- printf("%d",tos);
- a=pop();
- b=pop();
- switch(ch)
- {
- case '+':
- val=a+b;
- break;
- case '-':
- val=a-b;
- break;
- case '*':
- val=a*b;
- break;
- case '/':
- val=a/b;
- break;
- }
- printf("%d",val);
- push(val);
- }
- }
- printf("the result of the expression %s = %d",exp,num[0]);
- return 0;
- }
Add Comment
Please, Sign In to add comment