33,311
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class city //基类
{
int time, ID, id[5], hp[5], M, mhp;
bool nn1, nn2;
public:
city(int *n, int m){
for (int i = 0; i < 5; i++)
hp[i] = n[i];
M = m;
time = ID = mhp = 0;
id[5] = { 0 };
nn1 = nn2 = 0;
}
void nn_switch(void)
{
nn1 = 0;
}
bool nn2_switch(void)
{
return nn2;
}
void print();
friend class red;
friend class blue;
};
void city::print()
{
cout.fill('0');
cout.width(5);
cout << time << " " << "with strength" << hp[1];
}
class product //生产武士及装备
{
string *wp;
float morale;
public:
product()
{
morale = 0;
wp[0] = "sword";
wp[1] = "bomb";
wp[2] = "arrow";
}
void dragon(int mhp, int hp, int ID); //四个生产武士的成员函数
void ninja(int ID);
void iceman(int ID);
void lion(int mhp);
};
void product::dragon(int mhp, int hp, int ID)
{
morale = mhp / hp;
cout << "It has a " << wp[ID % 3] << ",and it's morale is " << morale << endl;
}
void product::ninja(int ID)
{
cout << "It has a " << wp[ID % 3] << " and a " << wp[(ID + 1) % 3] << endl;
}
void product::iceman(int ID)
{
cout << "It has a " << wp[ID % 3] << endl;
}
void product::lion(int mhp)
{
cout << "It's loyalty is " << mhp << endl;
}
class red : public city //派生类
{
string *s;
int num;
public:
bool stop;
red(int *n, int m) :city(n, m)
{
s[0] = "iceman";
s[1] = "lion";
s[2] = "wolf";
s[3] = "ninja";
s[4] = "dragon";
num = 0;
stop = 0;
}
void print();
product p;
};
void red::print()
{
if (nn1 == 0 && nn2 == 0)
{
if (stop == false)
{
int num_s = 0;
cout.fill('0');
cout.width(5);
cout << time;
cout << " " << "red " << s[num] << " " << city::ID << " born with strength " << hp[num] << ", " << id[num] << " " << s[num] << " in red headquarter" << endl;
switch (num)
{
case 0:
p.iceman(ID);
case 1:
p.lion(mhp);
case 3:
p.ninja(ID);
case 4:
p.dragon(mhp, hp[num], ID);
default:
break;
}
mhp = M - hp[num];
id[num]++;
time++; num++; ID++;
if (num > 4)
num = 0;
while (hp[num] > mhp && num_s <= 5)
{
num++;
num_s++;
if (num > 4)
num = 0;
}
if (num_s > 5)
stop = true;
nn1 = 1;
}
else
{
cout.fill('0');
cout.width(5);
cout << time;
cout << " red headquarter stops making warriors" << endl;
nn2 = 1;
}
}
}
class blue : public city
{
string *s;
int num;
public:
bool stop;
blue(int *n, int m) :city(n, m)
{
s[0] = "lion";
s[1] = "dragon";
s[2] = "ninja";
s[3] = "iceman";
s[4] = "wolf";
num = 0;
stop = 0;
}
void print();
product p;
};
void blue::print()
{
if (nn1 == 0 && nn2 == 0)
{
if (stop == false)
{
int num_s = 0;
cout.fill('0');
cout.width(5);
cout << time;
cout << " " << "blue " << s[num] << " " << city::ID << " born with strength " << hp[num] << ", " << id[num] << " " << s[num] << " in blue headquarter" << endl;
switch (num)
{
case 0:
p.lion(mhp);
case 1:
p.dragon(mhp, hp[num], ID);
case 2:
p.ninja(ID);
case 3:
p.iceman(ID);
default:
break;
}
mhp = M - hp[num];
id[num]++;
time++; num++; ID++;
if (num > 4)
num = 0;
while (hp[num] > mhp && num_s <= 5)
{
num++;
num_s++;
if (num > 4)
num = 0;
}
if (num_s > 5)
stop = true;
nn1 = 1;
}
else
{
cout.fill('0');
cout.width(5);
cout << time;
cout << " blue headquarter stops making warriors" << endl;
nn2 = 1;
}
}
}
int main() {
int n, m, hp[5];
bool stop1, stop2;
stop1 = stop2 = 0;
cin >> n;
cin >> m;
for (int s = 0; s < 5; s++)
cin >> hp[s];
red r(hp, m);
blue b(hp, m);
for (int i = 1; i <= n; i++)
{
cout << "Case:" << i << endl;
do
{
r.print();
b.print();
r.nn_switch();
b.nn_switch();
stop1 = r.nn2_switch();
stop2 = b.nn2_switch();
} while (stop1 == true && stop2 == true);
}
return 0;
}
id[5] = { 0 };
这个编译能通过?