Home >> Sem 2 >> Data-structur >> postfix
/* Write a Program for to evaluate postfix expression */
char postfix[100];
static i,top;
long int stack[20];
void main()
{
int temp,p;
clrscr();
printf("Enter Postfix Expression : ");
scanf("%s",postfix);
for(i=0;postfix[i]!='\0';i++)
{
if(postfix[i]=='$' || postfix[i]=='^')
{
temp = stack[top-1];
p = stack[top-2];
while(temp>1)
{
stack[top-2] *=p;
temp--;
}
top--;
}
else if(postfix[i] == '+')
{
stack[top-2] += stack[top-1];
top--;
}
else if(postfix[i] == '-')
{
stack[top-2] -= stack[top-1];
top--;
}
else if(postfix[i] == '*')
{
stack[top-2] *= stack[top-1];
top--;
}
else if(postfix[i] == '/')
{
stack[top-2] /= stack[top-1];
top--;
}
else if(postfix[i]!=',')
{
stack[top] = postfix[i]-48;
while(postfix[i+1]!=',')
{
stack[top] *= 10 + postfix[i+1]-48;
i++;
}
top++;
}
}
printf("\nValue of Postfix Expression : %ld",stack[top-1]);
getch();
}