📄 matrix.cpp
字号:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MatrixElem double
//..................................................................................
void exchangeMatrixElem(MatrixElem &a,MatrixElem &b){
MatrixElem temp; temp=a; a=b; b=temp;
}
//..................................................................................
class Matrix{
public:
Matrix();
Matrix(int r, int c);
~Matrix(){ delete [] loc;}
void copyMatrix(Matrix &pMat,const Matrix &m); //。。。。复制两个矩阵
Matrix& operator =(const Matrix & );
friend void Print(const Matrix& toPrint);//.....................................打印一个矩阵
friend void exchangeMatrixElem(MatrixElem &a,MatrixElem &b);
MatrixElem& getElem(int r,int c);////。。。。。。。。。。。得到第r行 第c列的元素
const MatrixElem& getElem(int r,int c)const;
void fillMatrix();//.......................................填充矩阵
Matrix& multiplyMatrix(Matrix &a,Matrix &b);//。。。。。。 两个矩阵相乘
Matrix& powerMatrix(int power);//..........................矩阵求幂
void subMatrix(Matrix &a, Matrix &b,int i,int j);//。。。。第i行 第j列的余子式赋给a
double valueOfMatrix(Matrix &m);//.........................方阵的行列式的值
void test(Matrix &a,Matrix &b); //。。。。。。。。。。。。 求逆矩阵
int zhi(const Matrix &m);//......................................求矩阵的秩
void exchangeTwoColOrRow(Matrix &m,int k,int j,char whatToDo);//交换矩阵的两行或两列
void multiplyRowOrColWithAValue(Matrix &m,int k,MatrixElem d,char whatToDo);
void multiplyAndAdd(Matrix &m,int in,int out,MatrixElem d,char whatToDo);
private:
MatrixElem *loc;
int r;
int c ;
};
//...................................
Matrix::Matrix(){ int i; cout<<"\nMatrix::Matrix()";
cout<<"\nrow="; cin>>r;
cout<<"col="; cin>>c;
loc=new MatrixElem[r*c];
for(i=0;i<r*c;++i)
loc[i]=0;
}
Matrix::Matrix(int rr, int cc){int i; // cout<<"\nMatrix::Matrix(int rr, int cc)";
loc=new MatrixElem[rr*cc];
for(i=0;i<rr*cc;++i) loc[i]=0;
this->r=rr; this->c=cc;
}
//.....................................................
int Matrix::zhi(const Matrix &m){
Matrix temp(0,0); temp=m;// temp.copyMatrix(temp,m);
if(temp.r==1) {
for(int i=1;i<=temp.c;++i) if(temp.getElem(1,i)!=0)
return 1;
return 0; }
if(temp.c==1){
for(int i=1;i<=temp.r;++i) if(temp.getElem(i,1)!=0)
return 1;
return
0; }
Matrix theSubMatrix(0,0);
if(temp.getElem(1,1)==0){
for(int i=1;i<=temp.r;++i)
if(temp.getElem(i,1)!=0) {
temp.exchangeTwoColOrRow(temp,1,i,'r');
break;
}
}
if(temp.getElem(1,1)==0){
for(int i=1;i<=temp.c;++i)
if(temp.getElem(1,i)!=0) {
temp.exchangeTwoColOrRow(temp,1,i,'c');
break;
}
}
if(temp.getElem(1,1)==0) {
temp.subMatrix(theSubMatrix,temp,1,1);
return theSubMatrix.zhi(theSubMatrix);}
for(int i=2;i<=temp.r;++i)
temp.multiplyAndAdd(temp,i,1, (-1)*temp.getElem(i,1)/temp.getElem(1,1) ,'r');
temp.subMatrix(theSubMatrix,temp,1,1);
return 1+theSubMatrix.zhi(theSubMatrix);
}
//...................................
MatrixElem& Matrix::getElem(int rr,int cc){
if(rr>this->r||cc>this->c){
cout<<"ERROR in getElem(int rr,int cc)\n";
exit(0);
}
return loc[(rr-1)*c+cc-1];
}//...................................
const MatrixElem& Matrix::getElem(int rr,int cc)const{
if(rr>this->r||cc>this->c){
cout<<"ERROR in getElem(int rr,int cc)\n";
exit(0);
}
return loc[(rr-1)*c+cc-1];
}
void Matrix::copyMatrix(Matrix &pMat,const Matrix &m){
delete pMat.loc; pMat.loc=new MatrixElem[m.r*m.c];
for(int i=0;i<m.r*m.c;++i)
pMat.loc[i]=m.loc[i];
pMat.r=m.r; pMat.c=m.c;
}
//.........................
Matrix& Matrix::operator=(const Matrix& m){
if(this==&m)return *this;
delete this->loc; this->loc=new MatrixElem[m.r*m.c];
for(int i=0;i<m.r*m.c;++i)
this->loc[i]=m.loc[i];
this->r=m.r; this->c=m.c;
return *this;
}
//......................................................
void Matrix::fillMatrix(){ cout<<endl;
int i,j;
for(i=1;i<=r;++i){// cout<<endl;
for(j=1;j<=c;++j){cout<<"["<<i<<","<<j<<"]=";
cin>>getElem(i,j);}
} cout<<endl;
}
void Print(const Matrix& toPrint){ int i,j; cout<<"the Matrix is :"<<endl;
for(i=1;i<=toPrint.r;++i){
for(j=1;j<=toPrint.c;++j){
cout<<"\t"<<toPrint.getElem(i,j);
}cout<<endl;
}
}
//.....................................................................
void Matrix::test(Matrix &a,Matrix &b){ //求逆矩阵
if (valueOfMatrix(b)==0) {
cout<<"\nERROR IN Matrix::test(Matrix &a,Matrix &b)\n... FOR THE INPUT MATRIX'S VALUE IS ZERO\n"<<endl;
return;
}
delete a.loc; a.loc=new MatrixElem[ (a.r=b.r)*(a.c=b.c) ];
Matrix tempSubMatrix(0,0);
double theValue=b.valueOfMatrix(b);
for(int i=1,j=1;i<=a.r;++i)
for(j=1;j<=a.c;++j){
b.subMatrix(tempSubMatrix,b,j,i);
a.getElem(i,j) = b.valueOfMatrix(tempSubMatrix)*pow(-1,i+j)/theValue;
}
}
//...........................................................................
void Matrix::subMatrix(Matrix &a, Matrix &b,int k,int l){//
int i,j;
delete a.loc; a.loc=new MatrixElem[(a.r=b.r-1)*(a.c=b.c-1)];
for(i=1;i<=a.r;++i)
for(j=1;j<=a.c;++j)
if(i<k&&j<l) a.getElem(i,j)=b.getElem(i,j);
else if(i<k&&j>=l) a.getElem(i,j)=b.getElem(i,j+1);
else if(i>=k&&j<l) a.getElem(i,j)=b.getElem(i+1,j);
else if(i>=k&&j>=l) a.getElem(i,j)=b.getElem(i+1,j+1);
}//...................................................
void Matrix::exchangeTwoColOrRow(Matrix &m,int k,int j,char whatToDo){
if(m.c<k||m.c<j) {cout<<"ERROR"; exit(0);}//交换矩阵的两行或两列
for(;
!(whatToDo=='c'||whatToDo=='C'||whatToDo=='r'||whatToDo=='R')
; cout<<"WHAT DO YOU WANT ROW OR COL:",cin>>whatToDo);
if(whatToDo=='c'||whatToDo=='C')
{ for(int i=1;i<=m.r;++i)
exchangeMatrixElem(m.getElem(i,k),m.getElem(i,j)); return;}
else if(whatToDo=='r'||whatToDo=='R')
{ for(int i=1;i<=m.c;++i)
exchangeMatrixElem(m.getElem(k,i),m.getElem(j,i)); return;}
}
//....................................................................................
void Matrix::multiplyAndAdd(Matrix &m,int in,int out,MatrixElem d,char whatToDo){
for(;
!(whatToDo=='c'||whatToDo=='C'||whatToDo=='r'||whatToDo=='R')
; cout<<"WHAT DO YOU WANT? ROW OR COL:",cin>>whatToDo);
if(whatToDo=='c'||whatToDo=='C')
{ if(in>m.c||out>m.c){cout<<"ERROR IN multiplyAndAdd() "; exit(0);}
for(int i=1;i<=m.r;++i) m.getElem(i,in)+=m.getElem(i,out)*d;
return; }
else if(whatToDo=='r'||whatToDo=='R')
{ if(in>m.r||out>m.r){cout<<"ERROR IN multiplyAndAdd() "; exit(0);}
for(int i=1;i<=m.c;++i) m.getElem(in,i)+=m.getElem(out,i)*d;
return; }
}
//...............................................
double Matrix::valueOfMatrix(Matrix &m){
if(m.r!=m.c) {cout<<"ERROR"; exit(0);}
double value=0; Matrix tempSubMatrix(0,0);// char ch;
if(m.r==1&&m.c==1) return m.getElem(1,1);
for(int k=1;k<=m.c;++k){ cout<<".";
subMatrix(tempSubMatrix,m,1,k);
value += m.getElem(1,k)*( valueOfMatrix( tempSubMatrix )*pow(-1,k+1) );
}
return value;
}
//.................................
Matrix& Matrix::multiplyMatrix(Matrix &a,Matrix &b){
if(a.c!=b.r) {cout<<"INVALID MATRIXS"<<endl;exit(0);}
Matrix *pMatrix=new Matrix(a.r,b.c);
int c,r,k;
for(r=1;r<=a.r;++r)
for(c=1;c<=b.c;++c){
for(k=1;k<=a.c;++k)
pMatrix->getElem(r,c)+=a.getElem(r,k)*(b.getElem(k,c));}
return *pMatrix;
}
//........................................................
void Matrix::multiplyRowOrColWithAValue(Matrix &m,int k,MatrixElem d,char whatToDo){
for(;
!(whatToDo=='c'||whatToDo=='C'||whatToDo=='r'||whatToDo=='R')
;cout<<"WHAT DO YOU WANT ROW OR COL:", cin>>whatToDo);
if(whatToDo=='c'||whatToDo=='C')
{ if(k>m.c){cout<<"ERROR IN multiplyRowOrColWithAValue() FOR k>m.c"; exit(0);}
for(int i=1;i<=m.r;++i) m.getElem(i,k)*=d;
return; }
else if(whatToDo=='r'||whatToDo=='R')
{ if(k>m.r){cout<<"ERROR IN multiplyRowOrColWithAValue() FOR k>m.r"; exit(0);}
for(int i=1;i<=m.c;++i) m.getElem(k,i)*=d;
return; }
}
//................................
Matrix& Matrix::powerMatrix(int power){
if(power<1) exit(0);
if(power==1) return *this;
// this->copyMatrix(*tempMatrix,*this);
Matrix *tempMatrix=new Matrix(0,0);
*tempMatrix=*this;
for(int i=1;i<power;++i){
this->copyMatrix(*tempMatrix,this->multiplyMatrix(*this,*tempMatrix));
}
return *tempMatrix;
}
//........................................................
class InheritMatrix : public Matrix
{
public:
InheritMatrix():Matrix(){ cout<<"\nInheritMatrix()"; }
InheritMatrix(int row,int col): Matrix(row,col){ cout<<"\nInheritMatrix construct"; }
protected:
private:
};
int main(){ //int power;
Matrix a ,c(0,0) ,b(0,0);
// InheritMatrix inheritM(2,1); inheritM.fillMatrix(); inheritM.Print();
a.fillMatrix(); // b.fillMatrix();
Print(a); // b.Print();
// a.multiplyMatrix(a,b).Print();
//cout<<"The Power Is:";cin>>power; a.powerMatrix(power).Print();
//a.subMatrix(b,a,2,3); b.Print();
// cout<<a.valueOfMatrix(a)<<endl;//
// test(Matrix &a,Matrix &b){ //求逆矩阵
a.test(b,a); Print(b);
cout<<"\na zhi: "<<a.zhi(a);
cout<<"E="<<endl;
Print( a.multiplyMatrix(a,b) );
// c.PrincopyMatrix(c, a.multiplyMatrix(a,b));
// c.Print();
//cout<<a.zhi(a);
// a.exchangeTwoColOrRow(a,2,3,'Y'); a.Print();
// a.multiplyRowOrColWithAValue(a,2,1.2,'c'); a.Print();
//a.multiplyAndAdd(a,2,3,1.2,'r'); a.Print();
// cout<<a.zhi(a)<<endl;
return 0;
// test(Matrix &a,Matrix &b){ //求逆矩阵
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -