题目描述
(1)在向量CVector类的代码上,定义n阶矩阵类CMatrix,包含私有数据成员data存储矩阵数据,n存储矩阵阶数。
(2)将CMatrix定义为CVector的友元类。
(3)为CMatrix添加成员函数:CVector multi(const CVector &v1),计算n阶矩阵与n维向量v1的乘积。
(4)为CMatrix添加成员函数,判定矩阵与向量v1是否可计算乘积。
(5)为CMatrix添加需要的构造函数、析构函数和其它成员函数。
主函数输入数据,测试矩阵与向量的乘积。
输入
测试次数t
对每组测试数据,格式如下
第一行,矩阵阶数n 向量维数m
n阶矩阵
m维向量
输出
对每组测试数据,若矩阵与向量不能计算乘积,输出error;否则输出计算结果
输入样例1
1
3
1 0 0
0 1 0
0 0 1
3
1 2 3
输出样例1
1 2 3
该代码较冗长的原因是与前面题目向量1234的代码结合了
该题难点在于实现n阶矩阵与m维向量的乘法
我们使用一个嵌套循环实现
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
num[i] += (data[i][j] * datav[j]);//注意这里面的逻辑关系
}
}
数组num存放相乘后的向量
#include<iostream>
#include<string>
#include<iomanip>
#include<cstring>
using namespace std;
class cvector
{
private:
static int sum;
int* data;
int n;
friend class cmatrix;
public:
int getn()
{
return n;
}
int* getdata()
{
return data;
}
float Average();
friend cvector add(const cvector v1, const cvector v2);
cvector() { ; }
cvector(int n1, int* a)
{
n = n1;
data = new int[n];
for (int i = 0; i < n1; i++)
{
data[i] = a[i];
}
}
//~cvector() { delete[]data; }
void print();
void init(int n1, int* a);
void init2()
{
sum = 0;
}
static int pr_sum()
{
return sum;
}
};
class student
{
private:
string name;
cvector score;
public:
student(string name, int n1, int* a1);
void print();
};
class cmatrix
{
private:
int** data;
int n;
public:
cmatrix(int n1,int **ma);
cvector multi(const cvector& v1);
bool check(cvector& v1);
};
int cvector::sum = 0;
cvector cmatrix::multi(const cvector& v1)
{
int* datav = v1.data;
int* num = new int[n];
for (int i = 0; i < n; i++)//先初始化
{
num[i] = 0;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
num[i] += (data[i][j] * datav[j]);//注意这里面的逻辑关系
}
}
cvector ans(n, num);
return ans;
//cout << num[0];
//for (int i = 1; i < n; i++)
//{
// cout << ' ' << num[i];
//}
}
cmatrix::cmatrix(int n1, int** ma)
{
n = n1;
data = new int* [n];
for (int i = 0; i < n; i++)
{
data[i] = new int[n];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
data[i][j] = ma[i][j];
}
}
}
bool cmatrix::check(cvector& v1)
{
return v1.getn() == n ? true : false;
}
student::student(string name, int n1, int* a1)
{
score.init2();
this->name = name;
score.init(n1, a1);
}
void student::print()
{
cout << name<<' ';
score.print();
cout <<' ' << fixed << setprecision(2) << score.Average() << endl;
}
float cvector::Average()
{
float av = 1.0*sum / this->n;
return av;
}
void cvector::init(int n1, int* a)
{
n = n1;
data = new int[n];
for (int i = 0; i < n1; i++)
{
data[i] = a[i];
sum += data[i];
}
}
cvector add(const cvector v1, const cvector v2)
{
int sum=v1.n;
int num[100];
for (int i = 0; i < sum; i++)
{
num[i] = v1.data[i] + v2.data[i];
}
cvector c(sum, num);
return c;
}
void cvector::print()
{
cout << data[0];
for (int i = 1; i < n; i++)
{
cout << ' ' << data[i];
}
}
int main()
{
int t,n,m;
cin >> t;
while (t--)
{
cin >> n;
int** ma = new int* [n];
for (int i = 0; i < n; i++)
{
ma[i] = new int[n];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> ma[i][j];
}
}
cin >> m;
int* num = new int[m];
for (int i = 0; i < m; i++)
{
cin >> num[i];
}
cvector v(m, num);
cmatrix mtx(n, ma);
if (n != m)
{
cout << "error" << endl;
}
else
{
cvector ans=mtx.multi(v);
ans.print();
}
delete[]num;
for (int i = 0; i < n; i++)
{
delete ma[i];
}
delete[]ma;
}
return 0;
}