如何在同一时间弹出2堆栈
How to pop off 2 stacks at the same time
我目前正在开发一个程序,该程序模拟具有2个团队的游戏,两个团队都包含许多玩家。每个团队被表示为一个堆栈(alienStack1和alienStack2);这两个堆栈各包含若干玩家。为了让2个玩家进行战斗(每个堆栈中有一个),我必须从堆栈中弹出相应的外星人以对抗另一个,但我不知道如何同时弹出两个堆栈。然后我们应该将弹出的项目发送到队列中,但在我尝试之前,我想先弄清楚这个。如果有人能帮我解决这个问题,我会很感激的。
下面是我到目前为止的代码:在我的battlefield()函数中,我遇到了2堆栈弹出的问题。#include <iostream>
#include <string>
#include <stack>
using namespace std;
class Alien
{
public:
Alien();
Alien(int h, int w, char g); //set height to h,
void setHeight(int h); //set height to h
void setWeight(int w); //set weight to w
void setGender(char g); //sets the gender to g
int getHeight(); //return the height
int getWeight(); //return the weight
char getGender(); //return the gender
bool operator==(const Alien& alien) const;
bool operator!=(const Alien& alien) const;
bool operator<=(const Alien& alien) const;
bool operator<(const Alien& alien) const;
bool operator>=(const Alien& alien) const;
bool operator>(const Alien& alien) const;
void putPlayersInStack(Alien alien, Alien alien2, Alien alien3, Alien alien4);
void battlefield();
private:
int height; //inches
int weight; //pounds
char gender; //the gender. Either 'M' or 'F'
stack <Alien> alienStack1;
stack <Alien> alienStack2;
};
Alien::Alien()
{
height = 60;
weight = 100;
gender = 'M';
int statusPoints = 0;
}
Alien::Alien(int h, int w, char g)
{
height = h;
weight = w;
gender = g;
int statusPoints = 0;
}
void Alien::setHeight(int h)
{
if (height > 0)
{
height = h;
}
else
{
cout << "Invalid height. Must be greater than zero. " << endl;
}
}
void Alien::setWeight(int w)
{
if (weight > 0)
{
weight = w;
}
else
{
cout << "Invalid weight. Must be greater than zero. " << endl;
}
}
void Alien::setGender(char g)
{
if (gender == 'M' && gender == 'F')
{
gender = g;
}
else
{
cout << "Invalid gender. Must be either M or F. " << endl;
}
}
int Alien::getHeight()
{
return height;
}
int Alien::getWeight()
{
return weight;
}
char Alien::getGender()
{
return gender;
}
static int getGenderValue(char g)
{
int genderValue = 0;
int statusPoints = 0;
if (g == 'F')
{
genderValue = 3;
}
else
genderValue = 2;
return genderValue;
}
static int getStatusPoint(Alien alien)
{
int genderValue = getGenderValue(alien.getGender());
return (alien.getHeight() * alien.getWeight() * genderValue);
}
bool Alien::operator==(const Alien& alien) const
{
return getStatusPoint(*this) == getStatusPoint(alien);
}
bool Alien::operator!=(const Alien& alien) const
{
return getStatusPoint(*this) != getStatusPoint(alien);
}
bool Alien::operator<=(const Alien& alien) const
{
return getStatusPoint(*this) <= getStatusPoint(alien);
}
bool Alien::operator>=(const Alien& alien) const
{
return getStatusPoint(*this) >= getStatusPoint(alien);
}
bool Alien::operator<(const Alien& alien) const
{
return getStatusPoint(*this) < getStatusPoint(alien);
}
bool Alien::operator>(const Alien& alien) const
{
return getStatusPoint(*this) > getStatusPoint(alien);
}
void Alien::putPlayersInStack(Alien alien, Alien alien2, Alien alien3, Alien alien4)
{
stack <Alien> alienStack1;
stack <Alien> alienStack2;
// Team 1
alienStack1.push(alien);
alienStack1.push(alien2);
//Team 2
alienStack2.push(alien3);
alienStack2.push(alien4);
}
void Alien::battlefield()
{
cout << "Prepare for battle " << endl;
while (!alienStack1.empty())
{
alienStack1.top();
alienStack1.pop();
}
while (!alienStack2.empty())
{
alienStack2.top();
alienStack2.pop();
}
}
int main()
{
// Driver to test all 6 operators
Alien alien1(40, 120, 'M');
Alien alien2(50, 130, 'F');
Alien alien3(60, 140, 'M');
Alien alien4(70, 150, 'F');
Alien sendToStack;
sendToStack.putPlayersInStack(alien1, alien2, alien3, alien4);
/*if (player1 == player2)
{
cout << "Same score, it's a tie! " << endl;
cout << endl;
}
if (player1 != player2)
{
cout << "players are NOT equal " << endl;
cout << endl;
}
if (player1 <= player2)
{
cout << "It's a tie or the player 2 wins!" << endl;
cout << endl;
}
if (player1 < player2)
{
cout << "Player 2 wins!" << endl;
cout << endl;
}
if (player1 >= player2)
{
cout << "It's a tie or player 1 wins!" << endl;
cout << endl;
}
if (player1 > player2)
{
cout << "Player 1 wins!" << endl;
cout << endl;
}*/
system("pause");
return 0;
}
除了"Cheers and hth"指出的语法错误…
(1)异形类有两个栈作为成员。我认为你需要将玩家插入到这两个堆栈中。相反,Alien::putPlayersInStack函数将玩家插入两个临时堆栈中,这些堆栈超出了作用域并在函数结束时被销毁。
(2)我不知道游戏规则是什么,但我猜你是想做这样的事情:
void Alien::battlefield()
{
cout << "Prepare for battle " << endl;
Alien p1,p2;
while (1)
{
if (alienStack1.empty()) { /*battle ended*/ return; }
if (alienStack2.empty()) { /*battle ended*/ return; }
p1=alienStack1.top(); alienStack1.pop();
p2=alienStack1.top(); alienStack2.pop();
/* Now compare players p1, p2 */
}
}
相关文章:
- 为什么具有静态存储持续时间的同一内联变量在包含在 VS2017 编译的两个翻译单元中时会构造和销毁两次
- 图问题:找出两个节点是否在每个节点的O(1)时间和O(2)存储中共享同一分支
- 在给定程序中降低矢量数组实现堆栈的时间复杂度有哪些不同的可能方法?
- 同一程序的运行时间差异很大,有时达到1000+us
- 对于同一任务,线程的等待时间在 0 到 30000 微秒之间系统地切换
- 在O(1)时间C 中找到堆栈中的最小元素
- 运行时间检查:变量周围的堆栈已损坏
- 我如何仅使用一个Arduino uno才能在同一时间和同一方向上旋转两个步进电动机
- 如何确定网络堆栈何时准备好再次打开到同一主机/端口的套接字?
- 堆栈分配的确切时间
- 网络传输时间从TCP/IP堆栈
- 为什么同一次执行的时间不同
- 在同一列表的while循环中迭代for循环所需的Big-O时间
- 同一类型的多个声明是否会影响编译时间
- 堆栈分配结构总是在同一位置
- 当一个std::vector对象在堆栈中而另一个在堆中时,vector的交换函数的时间复杂度为O(1)或O(n)
- 我可以读取两个值的选项在同一时间使用popt库
- OpenGL的平移和旋转在同一时间
- 如何在同一时间弹出2堆栈
- c++游戏-循环和按键在同一时间