C++类的设计与使用、运算符重载

《三国志》游戏中,每位英雄角色均有统率、武力、智力、政治、魅力等5种能力值,假设带兵打仗时5种能力值的权重分别为(0.3, 0.2, 0.3, 0.1, 0.1)。蜀国现有关羽、张飞、赵云、马超、黄忠等五虎上将,现要派遣一位将领出征讨伐魏国,问选择哪位将领出征(即从综合能力值中寻找最大值)?本题要求利用类的设计与使用、运算符重载等知识点。
①设计一维数据块类或向量类Vector1(数据成员包括数据块长度、数据块首地址),表示5种能力值的权重; ②设计一维数据块类或向量类Vector2(数据成员包括数据块长度、数据块首地址,成员函数Max寻找最大综合能力值),表示5位将领的综合能力值; ③设计二维数据块类或矩阵类Mat③rix(数据成员包括数据块行数与列数、数据块首地址),表示5位将领的5种能力值; ④在主函数中,建立Vector1类对象b、 Vector2 类对象c以及Matrix类对象A,实现c=Ab(即矩阵与向量的乘积)与cout<<c(即输出所有将领的综合能力值信息),并调用对象c的Max函数以输出选择的将领信息。 ⑤要求对、<<运算符进行重载。

海大?

现在就打钱,马上发给

#include
using namespace std;
class Vector1//Vector1一维数组,表示权重
{
private:
int n;
public:
float* p;
Vector1(int n, float* p);//Vector1构造函数
};
Vector1::Vector1(int n1, float* p1)//Vector1构造函数
{
n = n1;p = p1;
}
float array1[5] = { 0.3,0.2,0.3,0.1,0.1 };
Vector1 b(5, &array1[0]);
class Vector2//一维数组,表示每个人的综合分数
{
private:
int m;
float* q;
public:
Vector2(int m, float* q);//Vector2构造函数
void Max(int row);//求出综合分数最大的
friend ostream& operator<<(ostream&, Vector2&);//"<<"运算符重载
};
float array2[5] = { 0.0,0.0,0.0,0.0,0.0 };
Vector2 c(2, &array2[0]);
float a[5][5] = { {69,68,67,96,85},{97,68,82,96,96},{89,98,77,79,95},{99,80,97,69,85},{93,78,79,95,92} };
Vector2::Vector2(int m1, float* q1)//Vector2构造函数
{
m = m1;q = q1;
}
void Vector2::Max(int row)//求出综合分数最大的
{
int j;
cout << "综合值最大的人物的各项能力值为:" << endl;
for (j = 0;j < 5;j++)cout << a[row][j] << " ";
}
ostream& operator<<(ostream&output, Vector2 &t)
{
int i;
for(i=0;i<5;i++)
output <<*(t.q + i)<< endl;
return output;
}
class Matrix //Matrix二维数组,列出每个人每项的分数
{
public:
Matrix(float* r1, int w1, int h1);
Vector2 operator *(Vector1& p);//符号重载"*"作为Matrix的成员函数
private:
float* r;
int w;
int h;
};
Matrix A(&a[0][0], 3, 2);
Vector2 Matrix::operator *(Vector1& r)
{
int i, j;
for (i = 0;i < 5;i++)
for (j = 0;j < 5;j++)
{
array2[i] += a[i][j] * *(r.p+j);
}
for (i = 0;i < 5;i++)cout << "综合能力值分别为:" << array2[i] << endl;
for (i = 0;i < 5;i++)return c;
}
Matrix::Matrix(float* p1, int w1, int h1)
{
r = p1;w = w1;h = h1;
}
int main()
{
int i, j, row;
float t = 0.0, max = array2[0];
c=A* b;
cout << "综合值分别为:" << endl;
cout << c;
for (i = 0;i < 5;i++)if (array2[i] > max) { max = array2[i];row = i; }
cout << "综合值最大的是第" << row << "位";
cout << endl;
c.Max(row);
return 0;
}

实锤你🐏学子