Jump to content
  • Advertisement
Sign in to follow this  
Cinshe

Need help with stacks

This topic is 4240 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

good morning. I am in need of help. I have an assignment due soon and for some reason i can fix the bugs in the program. The program will do basic mathematical operations using stacks. It uses a postfix method (writing the +,-, etc after the numbers. so 11+ is the same as 1+1, and 123+* would equal 2+3*1). there are no errors that Dev-C picks up, but when ran it will more or less freaze after entering the equation, please help! the code is...
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;



class Stack
{
    private:
        char *stk;
        int top;
        int size;        
    public:
        Stack(void);
        Stack(int);
        bool Stack_empty(void);
        bool Stack_full(void);
        void push(char);
        char pop(void);
        void Stack_write(void);
        void Stack_clear(void);
        void eval(void);
};

    Stack::Stack(void)
    {
        size = 20;
        top = -1;
        stk = new char[size];
    }
    
    Stack::Stack(int set_size)
    {
        size = set_size;
        top = -1;
        stk = new char[size];
    }

    bool Stack::Stack_empty(void)
    {
        return(top < 0); //Return truth matrix for top < 0.
        //True if top < 0; False if top > 0.  Please don't forget in the
        //distant future.
    }
    
    bool Stack::Stack_full(void)
    {
        return(top > size); //See above comment.
    }
    
    void Stack::push(char data)
    {
        if(!Stack_full())
        {
            stk[++top] = data;
        } 
        else 
        {
            cout << "Stack OVERFLOW" << endl;
        }
    }
    
    char Stack::pop(void)
    {
        if(!Stack_empty())
        {
            return (stk[top--]);
        } 
        else 
        {
            cout << "Stack UNDERFLOW" << endl;
        }
    }
    
    void Stack::Stack_write(void)
    {
        int index = top;
        
        if(Stack_empty())
        {
            cout << "Stack is empty" << endl;
        }
        else
        {
            while (index != -1)
            {
                cout << "Stack " << index << ": " << stk[index--] << endl;
            }
        }
    }

    void eval(Stack a){
         char first,second,com=' ',test='~';
         cout<<"Please enter the equation in postfix form: ";
         
         cin>>com; 
         a.push(com);
         while(!(a.Stack_empty())){        
         switch (com){
                if (test!='~')
                   a.push(test);
                cin>>com;
                case '+':
                     first=a.pop();
                     second=a.pop();
                     a.push(first+second);
                     a.Stack_write();
                     break;
                case '-':
                     first=a.pop();
                     second=a.pop();
                     a.push(second-first);
                     a.Stack_write();
                     break;
                case '*':
                     first=a.pop();
                     second=a.pop();
                     a.push((first*second));
                     a.Stack_write();
                     break;
                case '/':
                     first=a.pop();
                     second=a.pop();
                     a.push((second/first));
                     a.Stack_write();
                     break;
                default: 
                     a.push(com);
                     break;
                     }
                     
                test=a.pop();

         }
         
         cout<<"The answer is: " <<test<<endl;
    }

int main(int argc, char *argv[])
{
    Stack a(20);
    char com;
    
    while(true){
    eval(a);
    cout<<"Would you like to continue? (Y or N): ";
    cin>>com;
    com=toupper(com);
    if (com!='Y')
       exit(0);
    else
        com=' ';
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}


thanks

Share this post


Link to post
Share on other sites
Advertisement
compilers only catch syntax (language) errors. They don't catch semantic errors (meaning). I recommend using cout or a debugger to get a better understanding of what is going on in your program. For example if you had a function with five lines that seemed to be giving you trouble you could put a cout after every line of the function. Then run it and see where it hangs. You will generally be able to narrow the symptom of the bug down to a single line of code that way. Then use cout for the values of its subexpressions. If one of the variables seems to have incorrect state you can go back and use cout to see whether it ever had the right value and if so where it went astray. You could use a debugger, in a lot of ways they are nicer but I don't currently have one and I just like using cout so that is what I use.

Share this post


Link to post
Share on other sites
Your eval function's pretty broken. For starters, rather than pop the operation from the stack, you're trying to read it from console input. Secondly, you're not adding it to the stack correctly. You're adding the entire user-input formula to the stack as a single item and then when you pop it off, you're expecting only to get a single operation off, which means you'll default every time.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!