c++ - 在一堆中查找一个项目

标签 c++ loops stack

在我的代码中,我试图找到放入堆栈的特定项目。为此,我正在将所有项目移动到临时堆栈,以将其从原始堆栈中弹出。弹出后,我将按原始顺序将所有项目移回原始堆栈。我的代码从不识别该项目在堆栈中,所以我发现它实际上在堆栈中时找不到。你能帮我调试我的循环吗...

主要内容:

#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 将回答您的其他问题,所以这里是:

口香糖.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

口香糖.cpp 文件:

#include "Gumball.h"

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

最佳答案

应该是

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

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

除了同意 tom 并指出您考虑其他解决方案(例如 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();
}

关于c++ - 在一堆中查找一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7790987/

相关文章:

c++ - 在声明前使用静态常量成员

java - 每 x 秒读取新文件夹并使用其数据

python - python的控制流语句的性能

java - 为什么 Java Stack.push 会给出一个返回类型,即使我不需要它?

c - 有没有同时使用堆栈和队列(双端队列)ADT 的有趣算法?

程序集调用堆栈 - 术语问题

java - 如何使用 Hadoop Mapreduce 运行非 Java 程序(首选 C++/Matlab)

c++ - 在静态成员函数中无效使用成员 ‘xx::x’

c++ - 迭代位域的成员

r - 按照以下方法计算销售额