求c++矩阵相加和相减,该怎么解决
求c++矩阵相加和相减
1.编程实现矩阵相加和相减。(提示:定义矩阵类,数据成员为数组,重载+,-两个运算符)。
例如:
123 213 336
231 + 132 = 363
321 321 633
------解决方案--------------------
正好我又写过。。
给你参考参考。。我多了个重载*号,,还有 = << >> ;,
头文件
1.编程实现矩阵相加和相减。(提示:定义矩阵类,数据成员为数组,重载+,-两个运算符)。
例如:
123 213 336
231 + 132 = 363
321 321 633
------解决方案--------------------
正好我又写过。。
给你参考参考。。我多了个重载*号,,还有 = << >> ;,
头文件
- C/C++ code
#ifndef MATRIX_H #define MATRIX_H class Matrix { public: Matrix( int =0, int =0); Matrix( double** , int , int ); Matrix( const Matrix& ); ~Matrix(); void Set( double**, int, int ); friend Matrix operator+ ( const Matrix&, const Matrix& ); friend Matrix operator- ( const Matrix&, const Matrix& ); friend Matrix operator* ( const Matrix&, const Matrix& ); Matrix& operator= ( const Matrix& ); friend istream& operator>> ( istream&, Matrix& ); friend ostream& operator<< ( ostream&, const Matrix& ); protected: double** ma; int length; int width; }; //functions Matrix::Matrix( int wid, int len ) { if( len==0 || wid==0 ) { length = width = 0; ma = '\0'; return ; } length = len; width = wid; ma = new double*[width]; for( int i=0; i<width; i++ ) ma[i] = new double[length]; } Matrix::Matrix( double** p, int wid, int len ) { if( wid<=0 || len <=0 ) { cout << "error !\n"; exit(0); } Set( p, wid, len ); } Matrix::Matrix( const Matrix& Matr ) { Set( Matr.ma, Matr.width, Matr.length ); } Matrix::~Matrix() { for( int i=0; i<width; i++ ) delete[] ma[i]; delete[] ma; ma = NULL; width = length =0; } void Matrix::Set( double** p, int wid, int len ) { length = len; width = wid; ma = new double*[width]; for( int i=0; i<width; i++ ) { ma[i] = new double[length]; for( int j=0; j<length; j++ ) ma[i][j] = p[i][j]; } } Matrix operator+ ( const Matrix& ma1, const Matrix& ma2 ) { if( ma1.width != ma1.width || ma1.length != ma2.length ) { cout << "error !\n"; exit(0); } Matrix temp( ma1.width, ma1.length ); for( int i=0; i<temp.width; i++ ) for( int j=0; j<temp.length; j++ ) temp.ma[i][j] = ma1.ma[i][j] + ma2.ma[i][j]; return temp; } Matrix operator- ( const Matrix& ma1, const Matrix& ma2 ) { if( ma1.width != ma1.width || ma1.length != ma2.length ) { cout << "error !\n"; exit(0); } Matrix temp( ma1.width, ma1.length ); for( int i=0; i<temp.width; i++ ) for( int j=0; j<temp.length; j++ ) temp.ma[i][j] = ma1.ma[i][j] - ma2.ma[i][j]; return temp; } Matrix operator* ( const Matrix& ma1, const Matrix& ma2 ) { if( ma1.length != ma2.width ) { cout << "error !\n"; exit(0); } Matrix temp( ma1.width, ma2.length ); for( int i=0; i<temp.width; i++ ) for( int j=0; j<temp.length; j++ ) temp.ma[i][j] = 0; for( int i=0; i<ma1.width; i++ ) for( int j=0; j<ma2.length; j++ ) for( int k=0; k<ma1.length; k++ ) temp.ma[i][j] += ma1.ma[i][k] * ma2.ma[k][j]; return temp; } Matrix& Matrix:: operator= ( const Matrix& matr ) { for( int i=0; i<width; i++ ) for( int j=0; j<length; j++ ) ma[i][j] = matr.ma[i][j]; return *this; } istream& operator>> ( istream& input, Matrix& matr ) { for( int i=0; i<matr.width; i++ ) for( int j=0; j<matr.length; j++ ) cin >> matr.ma[i][j]; cout << endl; return input; } ostream& operator<< ( ostream& output, const Matrix& matr ) { for( int i=0; i<matr.width; i++ ) { for( int j=0; j<matr.length; j++ ) cout << setw(8) << matr.ma[i][j] << " "; cout << endl; } cout << endl; return output; } #endif