有两个矩阵A和B,均为3行4列,求两个矩阵之和.重载运算符+和-,实现两个矩阵想加和想减这是一个c++题,
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/20 01:46:30
有两个矩阵A和B,均为3行4列,求两个矩阵之和.重载运算符+和-,实现两个矩阵想加和想减这是一个c++题,
有两个矩阵A和B,均为3行4列,求两个矩阵之和.重载运算符+和-,实现两个矩阵想加和想减
这是一个c++题,
有两个矩阵A和B,均为3行4列,求两个矩阵之和.重载运算符+和-,实现两个矩阵想加和想减这是一个c++题,
#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