在堆栈中找到一个项目
Finding One Item in a Stack
在我的代码中,我试图找到一个特定的项目放在堆栈上。为此,我将所有项目移动到临时堆栈中,将其从原始堆栈中弹出。在弹出后,我将按照原来的顺序将所有项目移回原来的堆栈中。我的代码从未识别出该项目在堆栈中,所以当它实际上在堆栈中时,我得到它没有找到。你能帮我调试我的循环吗?
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
回答你的问题@TOM:
那么。cpp(堆栈。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 ?
我认为添加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;
}
应该是
while(!s.isempty() && g.color != temp.color)
{
gumballStack.push(temp);
g.counter++;
temp = s.pop(); //temp has been updated
cout << " " << temp.counter << endl << endl;
}
但是请注意,当吃掉的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();
}
如果没有看到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()
的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;
}
理想情况下,您可以使用迭代器重写整个内容。看一下std::find.
相关文章:
- 运行同一解决方案的另一个项目的项目
- 从链接列表c++中删除一个项目
- 仅从无序集合中删除一个项目
- 是否可以将 EXE 文件作为 lib 文件链接到另一个项目?
- 如何使用另一个项目命名空间?
- 如何在不创建新配置的情况下对两个不同解决方案使用的一个项目使用不同的 #defines
- 数组中无法解释的最后一个项目
- 单元测试类无法在我要测试的同一解决方案中的另一个项目中找到类
- 有没有更好的方法来检测向量中一个项目的多次出现?
- 是否有其他方法将.dll文件从一个项目复制到我的启动项目中的可执行文件旁边
- 如果我尝试将对话框从一个项目移动到另一个项目,我是否需要从 rc 文件中复制 DESIGNINFO、对话框信息和AFX_
- OpenCV:文件存储操作员">>"在一个项目中工作,但不在另一个项目中工作(在同一类上)
- 如何将另一个项目中用 C 编写的源代码包含在我自己的项目中,C++在 Visual Studio 中
- 什么 STL 算法可以确定容器中的一个项目是否满足谓词?
- 将过滤器中的所有.cpp和.h移动到另一个项目并更正其文件夹位置
- 在另一个项目中包括继承的类时,抽象类的链接器错误
- 创建在另一个项目中定义的类对象
- 我正在尝试在Eclipse中构建一个C 项目,但是即使在使用MINGW路径设置路径变量之后,也会获得构建错误
- 在包含相同项目的向量中删除一个项目
- 将多个原型文件包含在一个项目中会导致protobuf_AssignDescriptorsOnce()已经有一个主体