0
   

Evaluation of postfix expression

 
 
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();
}
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Question • Score: 0 • Views: 1,520 • Replies: 0
No top replies

 
 

Related Topics

Clone of Micosoft Office - Question by Advocate
Do You Turn Off Your Computer at Night? - Discussion by Phoenix32890
The "Death" of the Computer Mouse - Discussion by Phoenix32890
Windows 10... - Discussion by Region Philbis
Surface Pro 3: What do you think? - Question by neologist
Windows 8 tips thread - Discussion by Wilso
GOOGLE CHROME - Question by Setanta
.Net and Firefox... - Discussion by gungasnake
Hacking a computer and remote access - Discussion by trying2learn
 
  1. Forums
  2. » Evaluation of postfix expression
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 12/27/2024 at 05:47:56