C++重载矩阵类运算符

//重载 运算符 矩阵  + * = <<  >>  
#include  
using namespace std;  
  
template  
class Matrix{  
public:  
    Matrix(int N, int M); //构造函数  
    Matrix(const Matrix &mat);  //拷贝构造函数  
    ~Matrix();  //析构函数  
    Matrix operator+(Matrix &mat);    //重载+  
    Matrix operator*(Matrix &mat);    //重载*  
    Matrix& operator=(const Matrix &mat);     //重载=  
    friend ostream& operator<<(ostream&out, Matrix &mat);  //重载<<  
    friend istream& operator>>(istream&in, Matrix &mat);   //重载>>  
    void SetElement();  
    void Display();  
  
private:  
    int _rows;  
    int _cols;  
    T** _p;  
};  
  
template  
Matrix::Matrix(int N, int M){  
    if (N <= 0 || M <= 0)  
        throw "矩阵初始化行数和列数需为正整数!";  
    _rows = N;  
    _cols = M;  
    _p = new T*[_rows];  
    for (int i = 0; i < _rows; i++){  
        _p[i] = new T[_cols];  
    }  
}  
  
template  
Matrix::Matrix(const Matrix &mat){  
    _rows = mat._rows;  
    _cols = mat._cols;  
    _p = new T*[_rows];  
    for (int i = 0; i < _rows; i++){  
        _p[i] = new T[_cols];  
        for (int j = 0; j < _cols; j++){  
            _p[i][j] = mat._p[i][j];  
        }  
    }  
}  
  
template  
Matrix::~Matrix(){  
    for (int i = 0; i <_rows; i++){  
        delete[]_p[i];  
    }  
    delete[]_p;   
}  
  
template  
Matrix Matrix::operator+ (Matrix &mat){  
    if (_rows != mat._rows || _cols != mat._cols){        
        throw "矩阵相加需满足行数和列数均相等!";   //抛出异常  
    }  
    else{  
        Matrix matSum(_rows,_cols);  
        for (int i = 0; i < _rows; i++){  
            for (int j = 0; j < _cols; j++){  
                matSum._p[i][j] = _p[i][j] + mat._p[i][j];  
            }  
        }  
        return matSum;  
    }     
}  
  
template  
Matrix Matrix::operator* (Matrix &mat){  
    if (_cols != mat._rows){  
        throw "矩阵相乘需满足第一个矩阵的列数和第二个矩阵的行数相等!";  
    }  
    else{  
        Matrix matProduct(_rows,mat._cols);  
        for (int i = 0; i < _rows; i++){  
            for (int j = 0; j < mat._cols; j++){  
                matProduct._p[i][j] = 0;  
                for (int k = 0; k < _cols; k++){  
                    matProduct._p[i][j] += _p[i][k] * mat._p[k][j];  
                }  
            }  
        }  
        return matProduct;  
    }  
}  
  
template  
Matrix& Matrix::operator=(const Matrix &mat){  
    if (_rows != mat._rows || _cols != mat._cols){  
        _p = new T*[mat._rows];  
        for (int i = 0; i < mat._rows; i++){  
            _p[i] = new T[mat._cols];         
        }  
    }  
  
    for (int i = 0; i < mat._rows; i++){  
        for (int j = 0; j < mat._cols; j++){  
            _p[i][j] = mat._p[i][j];  
        }  
    }  
    return (*this);  
}  
  
template  
ostream& operator<<(ostream&out, Matrix &mat){  
    for (int i = 0; i < mat._rows; i++){  
        for (int j = 0; j < mat._cols; j++){  
            out << mat._p[i][j];  
            if (j == mat._cols - 1)  
                out << endl;  
            else out << " ";  
        }         
    }  
    return out;  
}  
  
template  
istream& operator>>(istream&in, Matrix &mat){  
    for (int i = 0; i < mat._rows; i++){  
        for (int j = 0; j < mat._cols; j++){  
            in >> mat._p[i][j];  
        }  
    }  
    return in;  
}  
  
template  
void Matrix::SetElement(){  
    cout << "请输入矩阵的元素,共" << _rows*_cols << "个:" << endl;  
    for (int i = 0; i < _rows; i++){  
        for (int j = 0; j < _cols; j++){  
            cin >> _p[i][j];  
        }  
    }  
}  
  
template  
void Matrix::Display(){  
    for (int i = 0; i < _rows; i++){  
        for (int j = 0; j < _cols; j++){  
            if (j)cout << "  ";  
            cout << _p[i][j];  
        }  
        cout << endl;  
    }  
}  
int main()  
{  
    int n, m;  
    cout << "请输入行数: ";  
    cin >> n;  
    cout << "请输入列数: ";  
    cin >> m;   
    Matrix mat1(n, m);  
    cout << "请输入矩阵的元素,共" << n*m << "个:" << endl;  
    cin >> mat1;  
    cout << mat1;  
    cout << "请输入行数: ";  
    cin >> n;  
    cout << "请输入列数: ";  
    cin >> m;  
    Matrix mat2(n,m);  
    cout << "请输入矩阵的元素,共" << n*m << "个:" << endl;  
    cin >> mat2;  
    cout << mat2;  
    try{  
        Matrix mat3 = mat1 + mat2;  
        cout << "矩阵相加:" << endl;  
        cout << mat3;  
    }  
    catch (const char* msg){  
        cerr<< msg << endl;  
    }  
  
    try{  
        Matrix mat3 = mat1 * mat2;  
        cout << "矩阵相乘:" << endl;  
        cout << mat3;  
    }  
    catch (const char* msg){  
        cerr << msg << endl;  
    }  
      
    return 0;  
} 

请输入行数: 2

请输入列数: 3

请输入矩阵的元素,共6个:

1 2 3 4 5 6

1 2 3

4 5 6

请输入行数: 2

请输入列数: 3

请输入矩阵的元素,共6个:

6 5 4 3 2 1

6 5 4

3 2 1

矩阵相加:

7 7 7

7 7 7

矩阵相乘需满足第一个矩阵的列数和第二个矩阵的行数相等!

请按任意键继续. . .

 

 

请输入行数: 2

请输入列数: 3

请输入矩阵的元素,共6个:

1 2 3 4 5 6

1 2 3

4 5 6

请输入行数: 3

请输入列数: 2

请输入矩阵的元素,共6个:

1 2 3 4 5 6

1 2

3 4

5 6

矩阵相加需满足行数和列数均相等!

矩阵相乘:

22 28

49 64

请按任意键继续. . .

你可能感兴趣的:(C++)