矩阵 C++算法

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include<iostream>
#include<cmath>
using namespace std;
const maxSize=100;
class KJL_CMatrix
{
public:
KJL_CMatrix(); // 默认构造函数
KJL_CMatrix(int row, int column); // 构造函数一
KJL_CMatrix(const KJL_CMatrix& m); // 复制构造函数
~KJL_CMatrix(); // 默认析构函数
KJL_CMatrix& operator=(const KJL_CMatrix& m); // 赋值运算符
bool operator==(const KJL_CMatrix& m); // 比括较运算符
bool operator!=(const KJL_CMatrix& m); // 比括较运算符
KJL_CMatrix operator+(const KJL_CMatrix& m); // 加运算符
KJL_CMatrix operator-(const KJL_CMatrix& m); // 减运算符
KJL_CMatrix& operator+=(const KJL_CMatrix& m); // 自加运算符
KJL_CMatrix& operator-=(const KJL_CMatrix& m); // 自减运算符
KJL_CMatrix operator-();// 取负数
KJL_CMatrix operator*(const KJL_CMatrix& m); // 乘法运算符
void input();//矩阵输入
void output(); // 输出该矩阵
KJL_CMatrix transpose(); // 矩阵转置
KJL_CMatrix yuzishi(int i,int j);//求矩阵的第(i,j)的余子式
int hanglieshi();//求矩阵的行列式
KJL_CMatrix bansui();//求矩阵的伴随矩阵
KJL_CMatrix inverse(); // 矩阵求逆

// 设置(i,j)的值
void setValue(int row, int column, double value) { _A[row][column] = value; }
double getValue(int row, int column) const { return _A[row][column]; }
// 设置行、列的值
void setRow(const int row) { _row = row; }
int getRow() const { return _row; }
void setColunm(const int column) { _column = column; }
int getColumn() const { return _column; }
private:// 成员变量
double** _A; // 或用这个定义vector<vector<double> > _A;
int _row, /*行*/ _column; // 列
};
KJL_CMatrix::KJL_CMatrix()//矩阵默认构造函数
{
_row=0;
_column=0;
_A=(double * *) new double [maxSize];
int i;
for(i=0;i<maxSize;i++)
_A[i]=new double[maxSize];
int j;
for(i=0;i<maxSize;i++)
for(j=0;j<maxSize;j++)
_A[i][j]=0;
}
KJL_CMatrix::KJL_CMatrix(int row, int column)//构造函数重载
{
_row=row;
_column=column;
_A=(double * *) new double [maxSize];
int i;
for(i=0;i<maxSize;i++)
_A[i]=new double[maxSize];
int j;
for(i=0;i<maxSize;i++)
for(j=0;j<maxSize;j++)
_A[i][j]=0;
}
KJL_CMatrix::KJL_CMatrix(const KJL_CMatrix& m)//复制构造函数
{
_row=m._row;
_column=m._column;
int i,j;
_A=(double * *) new double [maxSize];
for(i=0;i<maxSize;i++)
_A[i]=new double[maxSize];
for(i=0;i<maxSize;i++)//初始化
for(j=0;j<maxSize;j++)
_A[i][j]=0;
for(i=0;i<_row;i++)
for(j=0;j<_column;j++)
{
_A[i][j]=m._A[i][j];
}
}
KJL_CMatrix::~KJL_CMatrix()//析构函数
{
delete []_A;
}
void KJL_CMatrix::input()//输入函数
{
int i,j;
cout<<"请输入矩阵的行数:"<<endl;
cin>>_row;
cout<<"请输入矩阵的列数:"<<endl;
cin>>_column;
cout<&l

t;"请输入矩阵:"<<endl;
for(i=0;i<_row;i++)
for(j=0;j<_column;j++)
{
cin>>_A[i][j];
}
}
void KJL_CMatrix::out
put()//输出函数
{
int i,j;
for(i=0;i<_row;i++)
{ for(j=0;j<_column;j++)
{
cout<<_A[i][j]<<" ";
}
cout<<endl;
}
}
KJL_CMatrix & KJL_CMatrix::operator=(const KJL_CMatrix & m)//=函数重载
{
_row=m._row;
_column=m._column;
int i,j;
for(i=0;i<_row;i++)
for(j=0;j<_column;j++)
{
_A[i][j]=m._A[i][j];
}
return *this;
}
KJL_CMatrix KJL_CMatrix::operator-()//取负号负号重载
{
int i,j;
for(i=0;i<_row;i++)
for(j=0;j<_column;j++)
{
_A[i][j]=-_A[i][j];
}
return *this;
}
KJL_CMatrix KJL_CMatrix::operator+(const KJL_CMatrix& m)//加号函数重载
{
if(_row!=m._row||_column!=m._column)
{
cout<<"矩阵不能相加!"<<endl;
}
int i,j;
for(i=0;i<_row;i++)
for(j=0;j<_column;j++)
{
_A[i][j]=_A[i][j]+m._A[i][j];
}
return *this;

}
KJL_CMatrix& KJL_CMatrix::operator+=(const KJL_CMatrix& m)//+=符号函数重载
{
if(_row!=m._row||_column!=m._column)
{
cout<<"矩阵不能+=!"<<endl;
}
int i,j;
for(i=0;i<_row;i++)
for(j=0;j<_column;j++)
{
_A[i][j]+=m._A[i][j];
}
return *this;

}
KJL_CMatrix& KJL_CMatrix::operator-=(const KJL_CMatrix& m)//-=符号函数重载
{
if(_row!=m._row||_column!=m._column)
{
cout<<"矩阵不能-=!"<<endl;
}
int i,j;
for(i=0;i<_row;i++)
for(j=0;j<_column;j++)
{
_A[i][j]-=m._A[i][j];
}
return *this;

}
KJL_CMatrix KJL_CMatrix::operator-(const KJL_CMatrix& m)//减号函数重载
{
if(_row!=m._row||_column!=m._column)
{
cout<<"矩阵不能相减!"<<endl;
}
int i,j;
for(i=0;i<_row;i++)
for(j=0;j<_column;j++)
{
_A[i][j]=_A[i][j]-m._A[i][j];
}
return *this;
}
KJL_CMatrix KJL_CMatrix::operator*(const KJL_CMatrix& m)//乘号符号函数重载
{
KJL_CMatrix temp(_row,m._column);
if(_column!=m._row)
{
cout<<"矩阵不能相乘!"<<endl;
}
int i,j,n;
for(i=0;i<_row;i++)
for(j=0;j<_row;j++)
{
for(n=0;n<_column;n++)
temp._A[i][j]+=_A[i][n]*m._A[n][j];
}
return temp;

}
bool KJL_CMatrix::operator ==(const KJL_CMatrix& m)//==函数重载
{
if(_row!=m._row||_column!=m._column)
return false;
int i,j;
for(i=0;i<_row;i++)
for(j=0;j<_column;j++)
{
if(_A[i][j]!=m._A[i][j])
return false;
}
return true;
}
bool KJL_CMatrix::operator !=(const KJL_CMatrix& m)//!=函数重载
{
if(_row!=m._row||_column!=m._column)
return true;
int i,j;
for(i=0;i<_row;i++)
for(j=0;j<_column;j++)
{
if(_A[i][j]!=m._A[i][j])
return true;
}
return false;
}
KJL_CMatrix KJL_CMatrix::transpose()//转置函数
{
KJL_CMatrix tem;
tem._row=this->_column;
tem._column=this->_row;
int i,j;
for(i=0;i

<_row;i++)
for(j=0;j<_column;j++)
{
tem._A[j][i]=_A[i][j];
}
*this=tem;
return *this;
}
KJL_CMatrix KJL_CMatrix::yuzishi(int i,int j)//求矩阵的余子式
{
KJL_CMatrix temp;
temp._row=this->_row-1;
temp._column=this->_column-1;
int m,n,k=0,l;
for(m=
0;m<_row;m++)
{
l=0;
for(n=0;n<_column;n++)
{
if(m!=i&&n!=j)
{
temp._A[k][l]=_A[m][n];
}
if(n!=j) l++;
}
if(m!=i) k++;
}
return temp;
}
int KJL_CMatrix::hanglieshi()//求矩阵的行列式
{
if(_row!=_column)
{
cout<<"此矩阵无行列式"<<endl;
}
if(_row==1&&_column==1)
return _A[0][0];
else
{
int i,sum=0;
for(i=0;i<_column;i++)
{
sum+=pow(-1,i)*_A[0][i]*this->yuzishi(0,i).hanglieshi();
}
return sum;
}
}
KJL_CMatrix KJL_CMatrix::bansui()//求伴随矩阵
{
KJL_CMatrix temp;
temp._column=this->_column;
temp._row=this->_row;
int i,j;
for(i=0;i<_row;i++)
for(j=0;j<_column;j++)
{
temp._A[i][j]=pow(-1,i+j)*this->yuzishi(i,j).hanglieshi();
}
return temp;
}
KJL_CMatrix KJL_CMatrix::inverse()//矩阵求逆
{
KJL_CMatrix temp;
int n;
n=this->hanglieshi();
temp=this->bansui();
int i,j;
for(i=0;i<_row;i++)
for(j=0;j<_column;j++)
{
temp._A[i][j]/=n;
}
return temp;
}

void main()
{
/*
KJL_CMatrix array;
array.input();
cout<<"当前矩阵array如下:"<<endl;
array.output();
KJL_CMatrix array1(array);//检查复制构造函数的正确性
cout<<"当前矩阵array1(array)如下:"<<endl;
array1.output();
KJL_CMatrix array2;
array2=array1;//检查赋值重载符的正确性
cout<<"当前矩阵array2=array1如下:"<<endl;
array2.output();
KJL_CMatrix array3;
array3=array1+array2;//检查+重载的正确性
cout<<"当前矩阵array3=array1+array2如下:"<<endl;
array3.output();
KJL_CMatrix array4,array5,array6;
array4.input();
array5.input();
cout<<"当前矩阵array4如下:"<<endl;
array4.output();
cout<<"当前矩阵array5如下:"<<endl;
array5.output();
array6=array4*array5;//检查乘号重载符号的正确性
cout<<"当前矩阵array6=array4*array5如下:"<<endl;
array6.output();
array6=-array6;//检查取负号重载符号的正确性
cout<<"当前矩阵-array6如下:"<<endl;
array6.output();
KJL_CMatrix array7;
array7.input();
cout<<"当前矩阵array7如下:"<<endl;
array7.output();
array7.transpose();
cout<<"矩阵array7转置后的矩阵如下:"<<endl;
array7.output();
cout<<endl;
array7+=array7;
cout<<"当前矩阵array7+=array7如下:"<<endl;
array7.output();
KJL_CMatrix array8;
array8.input();
cout<<"当前矩阵array8如下:"<<endl;
array8.output();
cou

t<<"当前矩阵array8(1,1)的余子式如下:"<<endl;
array8.yuzishi(1,1).output();//检查矩阵取余子式
KJL_CMatrix array9;
array9.input();
cout<<"当前矩阵array9如下:"<<endl;
array9.output();
cout<<"当前矩阵array9的行列式为:"<<endl;
cout<<array9.hanglieshi()<<endl;//检查求矩阵的行列式
cout<<"当前矩阵array9的伴随矩阵为:"<<endl;
array9.bansui().output();
KJL_CMatrix array10;//为了检查矩阵求逆
array10.input();
cout<<"当前矩阵array10如下:"<<endl;
cout<<"当前
矩阵array10的逆矩阵如下:"<<endl;
array10.inverse().output();
*/
KJL_CMatrix array_xishu,array_zhi,array_jie;
cout<<"输入方程的系数矩阵"<<endl;
array_xishu.input();
cout<<"输入方程右边的常数矩阵"<<endl;
array_zhi.input();
cout<<"系数阵的行列式为:"<<endl;
cout<<array_xishu.hanglieshi()<<endl;
cout<<"方程的解为:"<<endl;
array_jie=array_xishu.inverse()*array_zhi;
array_jie.output();
}


相关文档
最新文档