#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
char s[80];
int stack[80],n,top=-1,x=0,y=0,i;
printf("Enter postfix
expression=");
gets(s);
n=strlen(s);
for(i=0;i<n;i++)
{
switch(s[i])
{
case '+':
y=stack[top];
x=stack[top-1];
top=top-1;
x=x+y;
stack[top]=x;
break;
case '-':
y=stack[top];
x=stack[top-1];
top=top-1;
x=x-y;
stack[top]=x;
break;
case '*':
y=stack[top];
x=stack[top-1];
top=top-1;
x=x*y;
stack[top]=x;
break;
case '/':
y=stack[top];
x=stack[top-1];
top=top-1;
x=x/y;
stack[top]=x;
break;
default:
top=top++;
if(s[i]>=48&&s[i]<=57)
x=s[i]-48;
stack[top]=x;
x=0;
break;
}
}
printf("The result
is=%d",stack[top]);
getch();
}
No comments:
Post a Comment