problem is postfix evaluation

Go4Expert Member
6Oct2007,09:19   #1
arvind_khadri's Avatar
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....
Go4Expert Member
6Oct2007,10:54   #2
arvind_khadri's Avatar
here is the code...plz help....plz shabbir!!!!

Last edited by shabbir; 6Oct2007 at 12:59.. Reason: Attachment Removed
Go4Expert Member
6Oct2007,11:02   #3
arvind_khadri's Avatar
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;
}
Go4Expert Founder
6Oct2007,13:03   #4
shabbir's Avatar
What is the problem in the code ? If you can help us let us know that it would be of great help.
Go4Expert Member
7Oct2007,18:05   #5
arvind_khadri's Avatar
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....
Go4Expert Founder
7Oct2007,20:26   #6
shabbir's Avatar
What do you mean by not modified one?
Go4Expert Member
13Oct2007,21:39   #7
arvind_khadri's Avatar
it means tat this is the original thing i wrote...after postin i did some changes.....
Team Leader
13Oct2007,22:09   #8
DaWei's Avatar
So what do you want us to do? Working with old code isn't effective.
Go4Expert Member
13Oct2007,22:40   #9
arvind_khadri's Avatar
yeah it aint effective.....jus help me rectify tat...plz
Team Leader
14Oct2007,00:49   #10
DaWei's Avatar
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.