Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   problem is postfix evaluation (http://www.go4expert.com/forums/postfix-evaluation-t6703/)

arvind_khadri 6Oct2007 09:19

problem is postfix evaluation
 
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....

arvind_khadri 6Oct2007 10:54

Re: problem is postfix evaluation
 
here is the code...plz help....plz shabbir!!!!

arvind_khadri 6Oct2007 11:02

Re: problem is postfix evaluation
 
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;
}


shabbir 6Oct2007 13:03

Re: problem is postfix evaluation
 
What is the problem in the code ? If you can help us let us know that it would be of great help.

arvind_khadri 7Oct2007 18:05

Re: problem is postfix evaluation
 
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....

shabbir 7Oct2007 20:26

Re: problem is postfix evaluation
 
What do you mean by not modified one?

arvind_khadri 13Oct2007 21:39

Re: problem is postfix evaluation
 
it means tat this is the original thing i wrote...after postin i did some changes.....

DaWei 13Oct2007 22:09

Re: problem is postfix evaluation
 
So what do you want us to do? Working with old code isn't effective.

arvind_khadri 13Oct2007 22:40

Re: problem is postfix evaluation
 
yeah it aint effective.....jus help me rectify tat...plz

DaWei 14Oct2007 00:49

Re: problem is postfix evaluation
 
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.


All times are GMT +5.5. The time now is 14:41.