이 프로그램은 영웅을 서로 싸우게하고 상대방과 맞서 싸우는 승자와 라운드 수를 표시합니다. 이 형식과 비슷합니다. 내가 추측하고- = 연산자가 올바르게 작동하지 않고 숫자가 증가 할 때
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
#ifndef SICT_HERO_H_
#define SICT_HERO_H_
#include <iostream>
namespace sict {
class Hero {
char m_name[41];
int m_health;
int m_attack;
public:
Hero();
Hero(char name[], int health, int attack);
void operator-=(int attack);
bool isAlive() const;
int attackStrength() const;
friend std::ostream& operator<<(std::ostream& os, const Hero& hero);
};
const Hero& operator*(const Hero& first, const Hero& second);
}
#endif
하여 Main.cpp는
#include <iostream>
#include "Hero.h"
using namespace std;
using namespace sict;
int main() {
cout << "Greek Heroes";
Hero moneyhunger("", 40, 4);
Hero hercules("Hercules", 32, 4);
Hero theseus("Theseus", 14, 5);
Hero oddyseus("Odysseus", 15, 3);
Hero ajax("Ajax", 17, 5);
Hero achilles("Achilles", 20, 6);
Hero hector("Hector", 30, 5);
Hero atalanta("Atalanta", 10, 3);
Hero hippolyta("Hippolyta", 10, 2);
cout << endl << "Quarter Finals" << endl;
const Hero& greek_winner0 = moneyhunger * hector;
const Hero& greek_winner1 = achilles * hector;
const Hero& greek_winner2 = hercules * theseus;
const Hero& greek_winner3 = oddyseus * ajax;
const Hero& greek_winner4 = atalanta * hippolyta;
cout << endl << "Semi Finals" << endl;
const Hero& greek_winner_semifinal1 = greek_winner1 * greek_winner2;
const Hero& greek_winner_semifinal2 = greek_winner3 * greek_winner4;
cout << endl << "Finals" << endl;
greek_winner_semifinal1 * greek_winner_semifinal2;
system("pause");
return 0;
}
프로그램이 예상 한대로 작동하지 않는 곳을 정확히 판별하려면 디버거를 사용해야합니다. (특히, 당신은 예상되는 행동에 익숙합니다. 결과가 무엇인지 알아 내기 위해이 모든 코드를 검토하는 데 꽤 많은 시간이 걸릴 것입니다.) 그 지점을 정확히 지적했지만 여전히 어떤 것을 이해하지 못한다면 그런 혼란스러운 부분의 행동을 중심으로 프로그램을 축소 시키십시오. – aschepler
'void Hero :: operator - = (int attack)'은, 특히 어떻게 사용되는지는 알 수 없습니다. 값을 돌려 주도록하십시오. –
이걸 돌려주고 영웅과 리턴 타입이 있어야합니다. 또한 논리 오류가 있기 때문에 else 문을 사용해야합니다 –