私のコードでは、特定のアイテムをスタックに配置しようとしています。これを行うために、私は元のスタックからそれをポップするために、一時スタックに途中のすべての項目を移動しています。それがポップされた後、元のスタックの元の順序ですべてのアイテムを元に戻します。私のコードは、アイテムがスタックに入っていることを認識しないので、実際にスタックに入っているときは見つからないということになります。あなたは私が...メインスタック内の1つのアイテムを探す
int型私のループをデバッグする助けてくださいすることができます:ここから
#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
あなたの質問にお答えください:
(stack.h用)も.cppファイルは、私はそれはあなたが質問のほとんどに答えるだろうと思い、次のとおりです。
私は私がスタックに戻って一時を入れていますので、あなたが述べてきた問題を参照してください#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;
}
複数回...間違いなく私の意図は、それを指摘していただきありがとうございます。どのように私はそれが同じものの代わりに、私が探しているアイテムと同じではないスタックに各ガンボールを追加するには?
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;
}
私はそれがあるGumball.hと.cppファイルを追加するので、ここでウル他の質問に答えるだろうと思い
のようなものを使用する必要がありますか? –