1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

problem is postfix evaluation

Discussion in 'C++' started by arvind_khadri, Oct 6, 2007.

  1. arvind_khadri

    arvind_khadri New Member

    Joined:
    Sep 4, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    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....
     
  2. arvind_khadri

    arvind_khadri New Member

    Joined:
    Sep 4, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    here is the code...plz help....plz shabbir!!!!
     
    Last edited by a moderator: Oct 6, 2007
  3. arvind_khadri

    arvind_khadri New Member

    Joined:
    Sep 4, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #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;
    }
    
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    What is the problem in the code ? If you can help us let us know that it would be of great help.
     
  5. arvind_khadri

    arvind_khadri New Member

    Joined:
    Sep 4, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    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....
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    What do you mean by not modified one?
     
  7. arvind_khadri

    arvind_khadri New Member

    Joined:
    Sep 4, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    it means tat this is the original thing i wrote...after postin i did some changes.....
     
  8. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    So what do you want us to do? Working with old code isn't effective.
     
  9. arvind_khadri

    arvind_khadri New Member

    Joined:
    Sep 4, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    yeah it aint effective.....jus help me rectify tat...plz
     
  10. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    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.
     
  11. arvind_khadri

    arvind_khadri New Member

    Joined:
    Sep 4, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    AAAAhhhhhhhhhh....u r too much confusing...neways forget it...thanks for all the help u could give(i aint taunting)....in the mean time i was able to come up a cod of mine without the structures...will try to make this work on my own....am really sorry for wastin u ppl's time....thanks neways!!!!cya!!
     

Share This Page