【问题描述】
编写一个程序,定义一个安全、动态二维double型的数组类Matrix。
- 实现Matrix table(row,col)定义row行col列的二维数组, row和col为正整数;
- 实现table(i,j)访问table的第i行第j列的元素,行号和列号从0开始;
- 实现Matrix的输入输出(>>、<<);
- 实现矩阵加等、乘等运算(+=、*=),例:Matrix& operator+=(const Matrix&); Matrix& operator*=(const Matrix&);
- 实现矩阵的赋值运算(=),例:Matrix& operator=(const Matrix&)。
【输入形式】
- 第一行table1的行列值row1和col1,空格分隔;
- 第二行table1的初始化值,共row1*col1个数据,空格分隔;
- 第三行table2的行列值row2和col2,空格分隔;
- 第四行table2的初始化值,共row2*col2个数据,空格分隔;
【输出形式】
- Matrix的输出格式为row行col列, 数据空格分隔;
- 若table1和table2不满足矩阵的加法和乘法运算规则,输出ERROR!;
- 依次输出以下表达式的值,每个输出间隔一行;
- table1(row1/2,col1/2);
- table1 *= table2;
- table1 += table2;
- table1 = table2。
【样例输入1】
1 3
1 1 1
2 3
2 2 2 2 2 2
【样例输出1】
1
ERROR!
ERROR!
2 2 2
2 2 2
【样例输入2】
2 3
1 1 1 1 1 1
3 2
2 2 2 2 2 2
【样例输出2】
1
6 6
6 6
ERROR!
2 2
2 2
2 2
【样例输入3】
2 2
1 1 1 1
2 2
1 0 0 1
【样例输出3】
1
1 1
1 1
2 1
1 2
1 0
0 1
【样例说明】
- 不要显示多余的提示信息,避免输出判定错误。
- 输出结束后不要输出任何内容,包括空格和换行。
- 注意判断输出信息是否符合要求。
【完整代码如下】
#include<iostream>
#include<vector>
using namespace std;
class Matrix
{
public:
int row;//数组行数
int col;//数组列数
int flag = 1;//当flag=1:可以输出;否则flag=0:出错,输出Error
//用vector嵌套来存放动态二维数组
//因为用的是嵌套,所以为二维数组
vector< vector<double> >v;
//输出对应行列的元素
void table(const int i, const int j)
{
cout << v[i][j] << endl;
}
friend ostream& operator<<(ostream& output, Matrix &m);
friend istream& operator>>(istream& input, Matrix &m);
Matrix& operator+=(const Matrix&);
Matrix& operator*=(const Matrix&);
Matrix& operator=(const Matrix&);
};
istream& operator>>(istream& input, Matrix& m)
{
cin >> m.row >> m.col;
double x=0.0;//x为即将输入的数组元素
//vector作为顺序容器,长度是可以变化的
vector<double> vv;
m.v.clear();//先删除数组v中的所有元素(先清空,防止出错)
for (int i=0; i<m.row; i++)
{
vv.clear();//清除vv里上一次存过的元素,方便多次使用
for (int j=0; j<m.col; j++)
{
cin >> x;
vv.push_back(x);
}
m.v.push_back(vv);//一行输入完后就整体存入v,即将此时的vv存入v
}
return input;
}
ostream& operator<<(ostream& output, Matrix& m)
{
if (m.flag != 0)
{
for (int i=0; i<m.row-1; i++)
{
for (int j=0; j<m.col-1; j++)
{
cout << m.v[i][j] << " ";
}
cout << m.v[i][m.col-1] << endl;
}
for (int j = 0; j < m.col - 1; j++)
{
cout << m.v[m.row-1][j] << " ";
}
cout << m.v[m.row-1][m.col-1];
}
//如果被标志过,即table1和table2不满足矩阵的加法和乘法运算规则,输出ERROR!;
else
{
cout << "ERROR!";
}
return output;
}
Matrix& Matrix:: operator+=(const Matrix& m)
{
if (row==m.row && col==m.col)
{
for (int i=0; i<row; i++)
{
for (int j=0; j<col; j++)
{
v[i][j] += m.v[i][j];
}
}
}
else
{
flag = 0;
}
return *this;
}
Matrix& Matrix:: operator*=(const Matrix& m)
{
//注意矩阵相乘条件:前一个的列数=后一个的行数
if (col == m.row )
{
for (int i=0; i<row; i++)
{
double sum=0;
for (int j=0; j<m.col; j++)
{
for (int k=0; k<col; k++)
{
sum += v[i][k] * m.v[k][j];
}
v[i][j]=sum;
}
}
}
else
{
flag = 0;
}
return *this;
}
Matrix& Matrix::operator=(const Matrix& m)
{
flag = 1;
row = m.row;
col = m.col;
vector<double> vv;
v.clear();
for (int i=0; i<m.row; i++)
{
vv.clear();
for (int j=0; j<m.col; j++)
{
vv.push_back(m.v[i][j]);
}
v.push_back(vv);
}
return *this;
}
//测试程序
int main()
{
Matrix table1,table2;
cin >>table1>>table2;
table1.table(table1.row / 2, table1.col / 2);
table1 *= table2;
cout << table1 << endl;
table1 += table2;
cout << table1 << endl;
table1 = table2;
cout << table1;
return 0;
}