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