下面的程序代码提供的矩阵的创建、矩阵的乘法、矩阵的加法、矩阵的减法、矩阵的元素访问操作符号()、矩阵的输出运算符>>、矩阵的输入运算符<<等相关操作。其中运算符采用成员函数和友元函数两种形式实现。具体代码如下:
#include <iostream.h>
#include <string.h>
#include <iomanip.h>
class MatrixOper
{
public:
friend istream& operator>>(istream& in,MatrixOper& m_matrix);
friend ostream& operator<<(ostream& out,MatrixOper& m_matrix);
friend MatrixOper& operator+(MatrixOper& m_matrix1,MatrixOper& m_matrix2);
friend MatrixOper& operator-(MatrixOper& m_matrix1,MatrixOper& m_matrix2);
friend MatrixOper& operator*(MatrixOper& m_matrix1,MatrixOper& m_matrix2);
private:
int row;
int col;
double* Value;
public:
MatrixOper(int m_row,int m_col):row(m_row),col(m_col)
{
if (m_col<=0||m_row<=0)
{
cout<<"Invalidate row or col "<<endl;
return ;
}
Value=new double[m_row*m_col];
memset(Value,0,sizeof(double)*m_col*m_row);
}
~MatrixOper()
{
if (this->Value)
{
delete []Value;
Value=NULL;
}
return ;
}
void InitMatrix()
{
cout<<"please input the matrix data:"<<endl;
cin>>*this;
}
double& operator()(int row,int col);
MatrixOper& operator=(const MatrixOper&);
};
double& MatrixOper::operator()(int m_row,int m_col)
{
if(m_col>=1&&m_col<=col&&m_row>=1&&m_row<=row)
{
return Value[(m_row-1)*col+(m_col-1)];
}
else
{
return Value[0];
}
}
MatrixOper& MatrixOper::operator=(const MatrixOper& m_matrix1)
{
if (this==&m_matrix1)
{
cout<<"This is the same object"<<endl;
}
else
{
this->row=m_matrix1.row;
this->col=m_matrix1.col;
memcpy(this->Value,m_matrix1.Value,sizeof(double)*m_matrix1.row*m_matrix1.col);
}
return *this;
}
istream& operator>>(istream& in,MatrixOper& m_matrix)
{
int Index=0;
while (Index<m_matrix.col*m_matrix.row)
{
in>>m_matrix.Value[Index];
Index++;
}
return in;
}
ostream& operator<<(ostream& out,MatrixOper& m_matrix)
{
int i=0,j=0;
for (i=1;i<=m_matrix.row;i++)
{
for (j=1;j<=m_matrix.col;j++)
{
out<<setw(5)<<m_matrix(i,j);
}
cout<<endl;
}
return out;
}
MatrixOper& operator+(MatrixOper& m_matrix1,MatrixOper& m_matrix2)
{
int i=1,j=1;
static MatrixOper result(m_matrix1.row,m_matrix1.col);
if (m_matrix1.row!=m_matrix2.row||m_matrix1.col!=m_matrix2.col)
{
cout<<"The two matrix cannot be added"<<endl;
}
for (;i<=m_matrix1.row;i++)
{
for (;j<=m_matrix1.col;j++)
{
result(i,j)=m_matrix1(i,j)+m_matrix2(i,j);
}
}
return result;
}
MatrixOper& operator-(MatrixOper& m_matrix1,MatrixOper& m_matrix2)
{
static MatrixOper result(m_matrix1.row,m_matrix1.col);
if (m_matrix1.row!=m_matrix2.row||m_matrix1.col!=m_matrix2.col)
{
cout<<"the two matrix cannot be match"<<endl;
}
else
{
for (int i=1;i<=m_matrix1.row;i++)
{
for (int j=1;j<=m_matrix1.col;j++)
{
result(i,j)=m_matrix1(i,j)-m_matrix2(i,j);
}
}
}
return result;
}
MatrixOper& operator*(MatrixOper& m_matrix1,MatrixOper& m_matrix2)
{
static MatrixOper result(m_matrix1.row,m_matrix2.col);
if (m_matrix1.col!=m_matrix2.row)
{
cout<<"The row of the matrix is not match"<<endl;
}
else
{
double temp=0;
for (int i=1;i<=m_matrix1.row;i++)
{
for (int j=1;j<=m_matrix2.col;j++)
{
temp=0;
for (int k=1;k<=m_matrix1.col;k++)
{
temp+=m_matrix1(i,k)*m_matrix2(k,j);
}
result(i,j)=temp;
}
}
}
return result;
}
int main()
{
MatrixOper a(2,2),b(2,2),c(2,3);
a.InitMatrix();
b.InitMatrix();
cout<<"The Data of matrix A is:"<<endl<<a;
cout<<"The Data of matrix B is:"<<endl<<b;
cout<<"The Data of matrix A+B="<<endl<<a+b;
cout<<"The Data of matrix A-B="<<endl<<a-b;
cout<<"The Data of matrix A*B="<<endl<<a*b;
a=b;
cout<<"The Data of matrix A is:"<<endl<<a;
c.InitMatrix();
cout<<"The Data of matrix A*C="<<endl<<a*c;
return 0;
}