Reply
Tue 29 Dec, 2009 06:44 am
hey everyone, i have written a C program to evaluate the value of a postfix expression, but im not getting the desired output. The output always comes out to be ZERO at runtime, there is no compilation error,
please help me out quickly to identify the error, the program is as follows:->
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define MAX 50
float stack[MAX];
int top=-1;
void push(float a)
{
if(top==MAX-1)
return;
else
stack[++top]=a;
}
float pop()
{
if(top==-1)
return -999;
else
return stack[top--];
}
void main()
{
char pos[50];
float a, b, c,p;
int i=0,j=0;
clrscr();
printf("\n Enter the postfix expression");
gets(pos);
while(pos!='\0')
{
if(isdigit(pos))
{
p = (float)pos - 48;
push(p);
}
else if( (pos=='*') || (pos=='/') || (pos=='-') || (pos=='+'))
{
a = pop();
b = pop();
if(pos=='+')
c = b+a;
if(pos=='-')
c = b-a;
if(pos=='*')
c = b*a;
if(pos=='/')
c = b/a;
push(c);
}
else if(pos==32)
continue;
else
{
printf("\n Enter only digits and 4 operators");
j=1;
break;
}
i++;
}
if(j==1)
exit();
else
printf("\n The value of the postfix expression is = %d",stack[0]);
getch();
}