C++ -=运算符工作不正常,在其应为
该程序应该让英雄们互相战斗,并显示获胜者以及他们互相战斗并获胜的回合数。 像这样的格式C++ -=运算符工作不正常,在其应为,c++,operator-keyword,C++,Operator Keyword,该程序应该让英雄们互相战斗,并显示获胜者以及他们互相战斗并获胜的回合数。 像这样的格式 Ancient Battle! Achilles vs Hector : Winner is Hector in 4 rounds. The thing is, the program would print out like this : Greek Heroes Quarter Finals Ancient Battle! vs Hector : Winner is in 7 rounds.
Ancient Battle! Achilles vs Hector : Winner is Hector in 4 rounds.
The thing is, the program would print out like this :
Greek Heroes
Quarter Finals
Ancient Battle! vs Hector : Winner is in 7 rounds.
Ancient Battle! Achilles vs Hector : Winner is Hector in 4 rounds.
Ancient Battle! Hercules vs Theseus : Winner is Hercules in 3 rounds.
Ancient Battle! Odysseus vs Ajax : Winner is Ajax in 3 rounds.
Ancient Battle! Atalanta vs Hippolyta : Winner is Atalanta in 3 rounds.
Semi Finals
Ancient Battle! Hector vs Hercules : Winner is Hector in 6 rounds.
Ancient Battle! Ajax vs Atalanta : Winner is Ajax in 2 rounds.
Finals
Ancient Battle! Hector vs Ajax : Winner is Hector in 3 rounds.
However, what it should logically print out is :
Greek Heroes
Quarter Finals
Ancient Battle! vs Hector : Winner is in 7 rounds.
Ancient Battle! Achilles vs Hector : Winner is Hector in 4 rounds.
Ancient Battle! Hercules vs Theseus : Winner is Hercules in 4 rounds.
Ancient Battle! Odysseus vs Ajax : Winner is Ajax in 3 rounds.
Ancient Battle! Atalanta vs Hippolyta : Winner is Atalanta in 4 rounds.
Semi Finals
Ancient Battle! Hector vs Hercules : Winner is Hector in 7 rounds.
Ancient Battle! Ajax vs Atalanta : Winner is Ajax in 2 rounds.
Finals
Ancient Battle! Hector vs Ajax : Winner is Hector in 4 rounds.
我猜它与-=运算符逻辑有关,但我似乎无法理解。有什么帮助吗
Hero.cpp
#include "Hero.h"
#include <iostream>
#include <cstring>
using namespace std;
namespace sict {
Hero::Hero()
{
m_name[0] = '\0';
m_health = 0;
m_attack = 0;
}
Hero::Hero(char name[], int health, int attack)
{
if (m_name != nullptr || m_name != "") {
strcpy(m_name, name);
}
else {
m_name[0] = '\0';
}
m_attack = attack;
m_health = health;
}
void Hero::operator-=(int attack) {
if (attack > 0) {
m_health -= attack;
}
if (attack > m_health) {
m_health = 0;
}
}
bool Hero::isAlive() const {
if (m_health > 0) {
return true;
}
else {
return false;
}
}
int Hero::attackStrength() const {
if (m_attack == 0) {
return 0;
}
else {
return m_attack;
}
}
ostream& operator<<(ostream& os, const Hero& hero) {
if (hero.m_name == '\0') {
os << "No Hero";
}
else {
os << hero.m_name;
}
return os;
}
const Hero& operator*(const Hero& first, const Hero& second) {
cout << "Ancient Battle! ";
cout << first;
cout << " vs ";
cout << second;
cout << " : ";
Hero A = first;
Hero B = second;
const Hero *winner = nullptr;
int max_rounds = 0;
while (A.isAlive() && B.isAlive() && max_rounds < 200) {
max_rounds++;
A -= B.attackStrength();
B -= A.attackStrength();
}
bool draw;
if (A.isAlive() && B.isAlive()) { draw = true; }
else { draw = false; }
if (draw) {
winner = &first;
}
else if (A.isAlive())
{
winner = &first;
}
else {
winner = &second;
}
cout << "Winner is ";
cout << *winner;
cout << " in " << max_rounds << " rounds. " << endl;
return *winner;
}
}
#包括“Hero.h”
#包括
#包括
使用名称空间std;
名称空间sict{
英雄::英雄()
{
m_name[0]='\0';
m_health=0;
m_攻击=0;
}
英雄::英雄(字符名[],整数生命值,整数攻击)
{
如果(m_name!=nullptr | | m_name!=“”){
strcpy(m_名称、名称);
}
否则{
m_name[0]='\0';
}
m_攻击=攻击;
m_health=健康;
}
虚空英雄::运算符-=(智力攻击){
如果(攻击>0){
m_health-=攻击;
}
如果(攻击>m_生命值){
m_health=0;
}
}
布尔英雄::isAlive()常量{
如果(m_运行状况>0){
返回true;
}
否则{
返回false;
}
}
int Hero::attackStrength()常量{
如果(m_攻击==0){
返回0;
}
否则{
返回m_攻击;
}
}
ostream&operator运算符需要返回*this
,并有一个Hero&
返回类型。此外,由于存在逻辑错误,您还需要使用else语句。您拥有该函数
void Hero::operator-=(int attack) {
if (attack > 0) {
m_health -= attack;
}
if (attack > m_health) {
m_health = 0;
}
}
这有时会不止一次地降低健康
假设生命值为15,攻击值为10。第一个if语句将生命值减少到5。第二个if语句将再次将生命值减少到0
要避免这种情况,您可以执行以下操作:
void Hero::operator-=(int attack) {
if (attack > 0) {
if (attack > m_health) {
m_health = 0;
} else {
m_health -= attack;
}
}
}
只有当攻击至少与剩余运行状况一样大时,这才会将运行状况降低到0。您应该使用调试器来准确确定程序未执行预期操作的位置。(特别是,您熟悉预期的行为,我需要花相当长的时间来分析所有这些代码,以确定结果应该是什么。)如果你找到了那个地方,但仍然不明白发生了什么,试着把程序简化成一个围绕着那个令人困惑的部分的行为而构建的程序。void Hero::operator-=(定点攻击)
毫无意义,特别是考虑到它的使用方式。让它返回一个值。它需要返回此值并具有Hero&return类型。此外,您应该使用else语句,因为您有一个逻辑错误谢谢!通过将else语句而不是另一个if语句用于my-=operator@SmithKyoif(m_name!=nullptr | | m_name!=“”){strcpy(m_name,name);
仔细查看此测试。如果m_name
是nullptr
,该怎么办?将尝试调用if
的后半部分。执行此操作的方法是检查它是否为null,并检查它是否为空。
void Hero::operator-=(int attack) {
if (attack > 0) {
m_health -= attack;
}
if (attack > m_health) {
m_health = 0;
}
}
void Hero::operator-=(int attack) {
if (attack > 0) {
if (attack > m_health) {
m_health = 0;
} else {
m_health -= attack;
}
}
}