problem is postfix evaluation

arvind_khadri's Avatar, Join Date: Sep 2007
Go4Expert Member
hi ppl...am gettin a prob in the o/p of the postfix evaluation...i checked out the prog in the other forum,but this prog is of my own so pls help me....nd yeah please tell me how to post the code....
0
arvind_khadri's Avatar, Join Date: Sep 2007
Go4Expert Member
here is the code...plz help....plz shabbir!!!!

Last edited by shabbir; 6Oct2007 at 12:59.. Reason: Attachment Removed
0
arvind_khadri's Avatar, Join Date: Sep 2007
Go4Expert Member
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;
}
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
What is the problem in the code ? If you can help us let us know that it would be of great help.
0
arvind_khadri's Avatar, Join Date: Sep 2007
Go4Expert Member
one error i could myself rectify later was tat i wasnt implementing stacks completly...i.e my top pointer was pointing somewhere else...when i rectified it the code works properly for 2 variables but not for 3 and higher....the above code *isnt* the modified one....
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
What do you mean by not modified one?
0
arvind_khadri's Avatar, Join Date: Sep 2007
Go4Expert Member
it means tat this is the original thing i wrote...after postin i did some changes.....
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
So what do you want us to do? Working with old code isn't effective.
0
arvind_khadri's Avatar, Join Date: Sep 2007
Go4Expert Member
yeah it aint effective.....jus help me rectify tat...plz
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
I don't think you understand what I mean. You say you have problems with the code. You also say that you have changed the code. Suppose that we invest our time, on the old code, and point out some problems. Perhaps your newer code has already addressed those problems, and solved them. That would be a waste of our time. I realize that the time that we devote is free. Two hours invested at zero dollars per hour costs the same as two hundred hours at zero dollars per hour.

Nevertheless, I would prefer to invest my time in a productive manner. Go figure.