Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在堆栈中查找一项_C++_Loops_Stack - Fatal编程技术网

C++ 在堆栈中查找一项

C++ 在堆栈中查找一项,c++,loops,stack,C++,Loops,Stack,在我的代码中,我试图找到放在堆栈上的特定项。为此,我将所有项目移动到临时堆栈,将其从原始堆栈中弹出。弹出后,我将按原始顺序将所有项目移回原始堆栈中。我的代码从未识别出该项在堆栈中,因此当它实际上在堆栈中时,我得到它却找不到。你能帮我调试我的循环吗 int main: #include <iostream> #include "Stack.h" #include "Gumball.h" using namespace std; int main() { Stack s, gum

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

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;
            }
要回答您的问题@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

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

// 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()
const
在以下内容中尤为重要

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;
  }
案例“E”:
s、 isempty()//这应该是redundent吗?
//I空是个问题,它不应该改变。
温度=s.pop();
如果(s.isempty()&&temp.color==g.color)
{
应该是这样的

while(!s.isempty() && g.color != temp.color)
        {
            gumballStack.push(temp);
            g.counter++;
            temp = s.pop();  //temp has been updated
            cout << " " << temp.counter << endl << endl;
        }
while(!s.isempty()&&g.color!=temp.color)
{
胶球堆。推压(温度);
g、 计数器++;
temp=s.pop();//temp已更新

我可以在同一篇文章中回答您的问题等。只需向下滚动查看更改。另外,我最好不要使用std::find或迭代器。。。。
// 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;
};
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;
  }
while(!s.isempty() && g.color != temp.color)
        {
            gumballStack.push(temp);
            g.counter++;
            temp = s.pop();  //temp has been updated
            cout << " " << temp.counter << endl << endl;
        }
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();
}