Results 1 to 3 of 3
hi 2 all...well am nt able to get the o/p for this program...wat i get is 0,but it accepts values for the variables.....plz ppl help me....
the code goes as ...
- 10-06-2007 #1Just Joined!
- Join Date
- Sep 2007
- Posts
- 70
have a prob in postfix evaluation...
hi 2 all...well am nt able to get the o/p for this program...wat i get is 0,but it accepts values for the variables.....plz ppl help me....
the code goes as following
[code=cpp]/
#include<iostream.h>
#include<ctype.h>
#define SS 100
#define TRUE 1
#define FALSE 0
struct stack
{
int items[SS],top;
};
struct stack s;
int empty(struct stack *ps)
{
if(ps->top==SS-1)
return TRUE;
else
return FALSE;
}
int popandtest(struct stack *ps,int *px,int *pund)
{
if(empty(ps))
{
*pund=TRUE;
return 1;
}
*pund=FALSE;
*px=ps->items[ps->top];
return(*px);
}
void pushandtest(struct stack *ps,int x,int *pover)
{
if(!empty(ps))
{
*pover=TRUE;
return;
}
*pover=FALSE;
ps->items[++(ps->top)]=x;
return;
}
int evalue(struct stack *ps,char post[SS])
{
int i=0,op1,op2,r,v,underflow,overflow;
for(;post[i]!='\0';i++)
{
if(!isalpha(post[i]))
{
op2=popandtest(&s,&s.items[s.top],&underflow);
op1=popandtest(&s,&s.items[s.top],&underflow);
switch(post[i])
{
case '+':r=op1+op2;
pushandtest(&s,r,&overflow);
break;
case '-':r=op1-op2;
pushandtest(&s,r,&overflow);
break;
}
}
}
v=popandtest(&s,&s.items[s.top],&underflow);
return(v);
}
main()
{
char p[SS];
int i,r;
cout<<"Enter a valid postfix expression";
cin>>p;
for(i=0;p[i]!='\0';i++)
{
if(isalpha(p[i]))
{
cout<<"enter value for"<<p[i]<<endl;
cin>>s.items[i];
s.top++;
}
}
r=evalue(&s,p);
cout<<"value="<<r;
return 0;
}
[code]
- 10-09-2007 #2Just Joined!
- Join Date
- Dec 2005
- Posts
- 30
If I am not mistaken, the function 'empty' is supposed to test for the emptiness of the stack. But the code you wrote for this function does not do that. I didnt go through the entire code, but this is something you need to correct before we go further.
- 10-09-2007 #3Just Joined!
- Join Date
- Sep 2007
- Posts
- 70
yeah u r rite...i too found it aftr posting it...nd the stack implementation isnt correct...my top points somewhere else!!!sorry for tat


Reply With Quote