在堆栈中查找一个项目

发布于 2024-12-10 19:48:12 字数 4062 浏览 0 评论 0原文

在我的代码中,我试图找到放入堆栈的特定项目。为此,我将所有项目移至临时堆栈,将其从原始堆栈中弹出。弹出后,我要将所有项目按原始顺序移回到原始堆栈中。我的代码永远无法识别该项目在堆栈中,因此当它实际上在堆栈中时我发现它没有找到。你能帮我调试我的循环吗?

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;
            }

从这里开始,我相信是错误:

            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;
        case 'q':
        case 'Q':
            choice_flag = false;
            break;
    }
} while(choice_flag);

return 0;
}

.h 文件:

#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

回答你的问题 @TOM:

好吧,我认为 .cpp (用于 stack.h)是它将回答您提出的大多数问题:

#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;
}

我看到您所说的问题,我多次将 temp 放回到堆栈中......绝对不是我的意图,谢谢您指出这一点。我如何才能将每个口香糖球添加到堆栈中,该口香糖球不等于我正在寻找的物品,而不是相同的物品?

我认为添加 Gumball.h 和 .cpp 将回答您的其他问题,所以这里是:

gumball.h 文件:

#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 文件:

#include "Gumball.h"

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

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;
        case '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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你搞怪i 2024-12-17 19:48:12

应该是,

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

但请注意,当吃掉的口香糖是堆栈中的最后一个时,此代码将不起作用,因为 s 将为空。

除了同意汤姆的观点并指出您考虑其他解决方案(例如 stl::list )之外,您应该使用类似的东西

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();
}

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();
}
彼岸花ソ最美的依靠 2024-12-17 19:48:12

如果没有看到 Stack 的实现,很难说问题出在哪里。但是,由于我发现您的代码的几个部分令人困惑,因此我认为指出其中的位置可能对您有用。如果您更改代码的接口以使其更清晰,那么您的问题可能会变得明显。

// 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;
};

在上述问题中,isempty() 的常量性在接下来的内容中尤为重要。

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;
  }

理想情况下,您将使用迭代器重写整个过程。看一下 std::find 的界面。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文