开发者

Finding One Item in a Stack

In my code, I am trying to find a specific item put onto the stack. To do so, i am moving all the items in the way to a temp stack, to pop it off the original stack. After it is popped, I am to move all the items back in the original stack in the original order. My code never recognizes that the item was in the stack so I get it is not found when it is in fact in the stack. Can you please help me debug my loop ...

int main:

#include <iostream>
#include "Stack.h"
#include "Gumball.h"

using namespace std;

int main()
{
  Stack s, gumballStack;
  Gumball g, temp;
  char choice;
  bool choice_flag = true;

  do {
    cin >> choice;
    cin >> g.color;
    switch(choice)
    {
        case 'b':
        case 'B':
            cout << "A" << " " << g.color << " gumball has been bought." << endl << endl;
            g.counter = 0;
            s.isempty();
            s.push(g);
            if(!s.isfull())
                cout << "The gumball is" << " " << g.color << " and has been stored." << endl << endl;
            else
                cout << "There is no room for another gumball." << endl << endl;
            break;
        case 'e':
        case 'E':
            s.isempty();
            temp = s.pop();
            if(s.isempty() && temp.color == g.color)
            {
                cout << "The " << g.color << " gumball has been eaten." << endl << endl;
            }

From here on, i believe is the error:

            while(!s.isempty() && g.color != temp.color)
            {
                gumballStack.push(temp);
                g.counter++;
                s.pop();
                cout << " " << temp.counter << endl << endl;
            }
            if(!s.isempty())
            {
                cout << "The " << " " << g.color << " gumball has been eaten." << endl << endl;
            }
            else
            {
                cout << "The gumball cannot be found." << endl << endl;
            }
            while(!gumballStack.isempty())
            {
                //gumballStack.pop();
                s.push(gumballStack.pop());
                gumballStack.pop();
            }
            break;
        c开发者_开发技巧ase 'q':
        case 'Q':
            choice_flag = false;
            break;
    }
} while(choice_flag);

return 0;
}

The .h file:

#ifndef STACK_H
#define STACK_H
#include "Gumball.h"

// Interface file  - Stack class definition
class Stack {
    public:
        Stack();
        void push(Gumball);
        Gumball pop();
        bool isempty();
        bool isfull();
    private:
        Gumball gumballs[6+1];
        int top;
};


#endif // STACK_H

TO ANSWER YOUR QUESTION @TOM:

well the .cpp (for stack.h) is, I think it will answer most of the questions you asked:

#include "Stack.h"
#include "Gumball.h"

using namespace std;

// Constructor to initialize the stack
Stack::Stack()
{
   top = -1;
}

// Function to add item x to stack
void Stack::push(Gumball x)
{
   if(!isfull()){
    top++;
    gumballs[top] = x;
    return; }
   else
    return;
}

// Function to remove and return top item of stack
Gumball Stack::pop()
{
    Gumball x;

    if(!isempty()) {
      x = gumballs[top];
      top--;
      return x; }
   else
      return x;
}

// Function to check if stack is empty
bool Stack::isempty()
{
    if (top == -1)
      return true;
    else
      return false;
}

// Function to check if stack is full
bool Stack::isfull()
{
    if (top == 6)
      return true;
    else
      return false;
}

I see the problem you've stated, that i am putting temp back onto the stack multiple times... definitely not my intentions, thank you for pointing that out. How do i get it to add each gumball in the stack that is not equal to the item i am looking for ,instead of the same one?

I think adding the Gumball.h and .cpp will answer ur other questions so here it is:

gumball.h file:

#ifndef GUMBALL_H
#define GUMBALL_H
#include <iostream>

using namespace std;

// Interface file  - Gumball class definition
class Gumball
{
    public:
        Gumball();
        string color;
        int counter;
    private: 
};

#endif // GUMBALL_H

gumball.cpp file:

#include "Gumball.h"

Gumball::Gumball()
{
    color = " ";
    counter = 0;
}


It should be

while(!s.isempty() && g.color != temp.color)
        {
            gumballStack.push(temp);
            g.counter++;
            temp = s.pop();  //temp has been updated
            cout << " " << temp.counter << endl << endl;
        }

but pay attention that this code will not work when eaten gumball is the last in the stack, because s will be empty.

Other than agreeing with tom and pointing you in thinking about other solutions (stl::list for example), you should use something like this

if (s.isempty()) {
    cout << "The gumball cannot be found." << endl << endl;
}
while(!s.isempty()) {
    Gumball temp = s.pop();
    if(temp.color == g.color) {
        cout << "The " << " " << g.color << " gumball has been eaten." << endl << endl;
    } else {
        gumballStack.push(temp);
        g.counter++;
        if (s.isempty()) {
             cout << "The gumball cannot be found." << endl << endl;
        }
    }
}
while(!gumballStack.isempty()) {
      s.push(gumballStack.pop());
      gumballStack.pop();
}


Its difficult to say what the problem is without seeing the implementation of Stack. However, since I found several parts of your code confusing, I thought it might be useful to you to indicate where. If you change the interface to your code so that it is clearer, it might be that your problems become apparent.

// Interface file  - Stack class definition
class Stack {
    public:
        Stack();
        void push(Gumball); //Does this push to the front or back?  
                            //  The stl uses push_back, and push_front, 
                            //  its good to keep this convention 
        Gumball pop();   //Does this pop the front or back?
        bool isempty();  //This function doesn't change Stack, right? 
                         // if so it should be marked const.
        bool isfull();   //Mark as const?
    private:
        Gumball gumballs[6+1];
        int top;
};

Of the above questions, the constness of isempty() is particularly important in what follows

case 'E':
  s.isempty();  //This should be redundent? 
                // isempty is a question, it shouldnt change s.
  temp = s.pop();
  if(s.isempty() && temp.color == g.color)
  {
     cout << "The " << g.color << " gumball has been eaten." << endl << endl;
  }  
  //Here isempty is being used as a question (as if it doesn't change s 
  // - I presume this is the intended use.
  //Also, .color is not a function, it should be, as will be seen. 
  //Also, temp never gets updated in your loop.
  while(!s.isempty() && g.color != temp.color)
  {
    gumballStack.push(temp);  //Why are you pushing multiple copies 
                              // of the same temp
    g.counter++;   //The counter should be an implementation detail, 
        // it should not be exposed like this. 
        // perhaps overload operator++()?
        //Presumably you want g.color to update when you increase the counter? 
        //this doesn't currently happen because g.color is not a function -
        // it points to data.  
        //I'm guessing that When you call color(), 
        // the function should check the value of the counter
        // and obtain the appropriate color.
    s.pop();  //Did you want to update temp here?
    cout << " " << temp.counter << endl << endl;
  }

Ideally, you would rewrite the whole thing with iterators. Have a look at the interface for std::find.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜