#include
#include
#include
#include
using namespace std;
const int MaxRow = 3;
const int MaxCol = 3;
class Matrix
{
public:
Matrix(){};
~Matrix(){};
int row()
{
return rowNum; //得到某行数
}
int col()
{
return colNum; //得到列行数
}
void setRow(int newRow)
{
rowNum=newRow; //重置行数
}
void setCol(int newCol)
{
colNum=newCol; //重置列数
}
void set(int newRow,int newCol); //重置某一元素
double & get(int r, int c);//得到某元素的值
void initialize(); //系统自动定义矩阵初始值
void display();//自定义矩阵初始值
Matrix operator +(Matrix other);// 重载加法运算符+,将两个矩阵相加。
Matrix operator -(Matrix other);//重载加法运算符-,将两个矩阵相减。
Matrix & operator *(Matrix other);//重载加法运算符*,将两个矩阵相乘。
Matrix operator =(Matrix other);// 重载赋值运算符=,将一个矩阵赋给另外一个矩阵。
double Det(); //求行列式的值
Matrix& turn(); //求转置
void out();//输出矩阵
private:
int rowNum;
int colNum;
double m[MaxRow][MaxCol];
};
Matrix Matrix::operator +(Matrix other)
{
double total =0;
Matrix temp;
if (rowNum!=other.row()||colNum!=other.col())
{
cout<<"\nTwo matrix has different row or col number!\n";
return (*this);
}
else
{
for (int r=0;r
total=m[r][c]+other.get(r,c);
temp.get(r,c)=total;
}
// *this=temp;
return temp;
}
}
Matrix Matrix::operator-(Matrix other)
{
double total =0;
Matrix temp;
if (rowNum!=other.row()||colNum!=other.col())
{
cout<<"\nTwo matrix has different row or col number!\n";
return (*this);
}
else
{
for (int r=0;r
total=(m[r][c]-other.get(r,c));
temp.get(r,c)=total;
}
// *this=temp;
return temp;
}
}
Matrix & Matrix::operator-(Matrix other)
{
double total =0;
Matrix temp;
if (rowNum!=other.row()||colNum!=other.col())
{cout<<"\nTwo matrix has different row or col number!\n";
return (*this);}
else{
for(int r=0;r
total=(m[r][c]-other.get(r,c));
temp.get(r,c)=total;}
return temp;}
}