14092: 【原4092】动态二维double型数组类Matrix的编写
题目
题目描述
author: 程序设计思想与方法助教组蒋永康 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/4092
问题描述
编写一个程序,定义一个安全、动态二维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。
程序运行示例
Sample Input 1
1 3
1 1 1
2 3
2 2 2 2 2 2
Sample Output 1
1
ERROR!
ERROR!
2 2 2
2 2 2
程序运行示例
Sample Input 2
2 3
1 1 1 1 1 1
3 2
2 2 2 2 2 2
Sample Output 2
1
6 6
6 6
ERROR!
2 2
2 2
2 2
程序运行示例
Sample Input 3
2 2
1 1 1 1
2 2
1 0 0 1
Sample Output 3
1
1 1
1 1
2 1
1 2
1 0
0 1
注意
- 不要显示多余的提示信息,避免输出判定错误。
- 输出结束后不要输出任何内容,包括空格和换行。
- 注意判断输出信息是否符合要求。
ligongzzz's solution
#include "iostream"
using namespace std;
class Matrix {
public:
int row = 0, col = 0;
bool status = true;
double **data;
double operator() (int i, int j) {
status = true;
return data[i+1][j+1];
}
Matrix(void) {
status = true;
}
Matrix(const Matrix &table) {
status = true;
row = table.row;
col = table.col;
data = new double*[row + 1];
for (int i = 1; i <= row; i++) {
data[i] = new double[col + 1];
for (int j = 1; j <= col; j++)
data[i][j] = table.data[i][j];
}
}
Matrix &operator=(const Matrix &table) {
status = true;
row = table.row;
col = table.col;
data = new double*[row + 1];
for (int i = 1; i <= row; i++) {
data[i] = new double[col + 1];
for (int j = 1; j <= col; j++)
data[i][j] = table.data[i][j];
}
return *this;
}
Matrix &operator+=(const Matrix &table) {
status = true;
if (table.row != row || table.col != col) {
status = false;
return *this;
}
for (int i = 1; i <= row; i++)
for (int j = 1; j <= col; j++)
data[i][j] += table.data[i][j];
}
Matrix &operator*=(const Matrix &table) {
status = true;
if (col != table.row) {
status = false;
return *this;
}
//存储原来的数据
Matrix temp;
temp = *this;
//申请新的空间
data = new double*[row+1];
for (int i = 1; i <= row; i++) {
data[i] = new double[table.col + 1];
for (int j = 1; j <= table.col; j++) {
data[i][j] = 0;
//向量乘法
for (int n = 1; n <= col; n++) {
data[i][j] += temp.data[i][n] * table.data[n][j];
}
}
}
//修改
col = table.col;
return *this;
}
};
istream &operator>>(istream &input, Matrix &table) {
table.status = true;
input >> table.row >> table.col;
table.data = new double*[table.row+1];
for (int i = 1; i <= table.row; i++) {
table.data[i] = new double[table.col+1];
for (int j = 1; j <= table.col; j++)
input >> table.data[i][j];
}
return input;
}
ostream &operator<<(ostream &output, const Matrix &table) {
for (int i = 1; i <= table.row; i++) {
for (int j = 1; j <= table.col; j++) {
output << table.data[i][j]<<(j<table.col?" ":"");
}
if(i<table.row)
cout << endl;
}
return output;
}
int main() {
Matrix table1, table2;
cin >> table1 >> table2;
cout << table1(table1.row / 2, table1.col / 2) << endl<<endl;
table1 *= table2;
if (!table1.status) {
cout << "ERROR!" << endl << endl;
}
else
cout << table1 << endl << endl;
table1 += table2;
if (!table1.status) {
cout << "ERROR!" << endl << endl;
}
else
cout << table1 << endl << endl;
table1 = table2;
if (!table1.status) {
cout << "ERROR!";
}
else
cout << table1;
return 0;
}