作业帮 > 综合 > 作业

有两个矩阵A和B,均为3行4列,求两个矩阵之和.重载运算符+和-,实现两个矩阵想加和想减

来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/12 12:52:27
有两个矩阵A和B,均为3行4列,求两个矩阵之和.重载运算符+和-,实现两个矩阵想加和想减
这是一个c++题,
有两个矩阵A和B,均为3行4列,求两个矩阵之和.重载运算符+和-,实现两个矩阵想加和想减
#define maxh 100
#include
using namespace std;
class Matrix { public:
int r, c;
int s[maxh][maxh];
Matrix operator + ( const Matrix &b );
Matrix operator - ( const Matrix &b );
};
Matrix Matrix :: operator + ( const Matrix &b )
{
Matrix res; res.r = r, res.c = c;
for ( int i = 0; i < r; i ++ )
for ( int j = 0; j < c; j ++ )
res.s[i][j] = s[i][j] + b.s[i][j];
return res;
}
Matrix Matrix :: operator - ( const Matrix &b )
{
Matrix res; res.r = r, res.c = c;
for ( int i = 0; i < r; i ++ )
for ( int j = 0; j < c; j ++ )
res.s[i][j] = s[i][j] - b.s[i][j];
return res;
}
Matrix a, b, c;
int main ( )
{
a.r = 4; b.r = 4;
a.c = 3; b.c = 3;
for ( int i = 0; i < a.r; i ++ )
for ( int j = 0; j < a.c; j ++ )
cin >> a.s[i][j];
for ( int i = 0; i < b.r; i ++ )
for ( int j = 0; j < b.c; j ++ )
cin >> b.s[i][j];

c = a + b;
for ( int i = 0; i < c.r; i ++ ) {
for ( int j = 0; j < c.c; j ++ )
cout