您好,欢迎访问三七文档
Matrix.h#ifndefMATRIX_H_INCLUDED#defineMATRIX_H_INCLUDED#includeiostream#includecstdlib#includestdexceptusingnamespacestd;classmatrix1{public:matrix1(){mat=NULL;r=0;c=0;}matrix1(int,int);matrix1(constmatrix1&b);~matrix1(){delete[]mat;r=0;c=0;}intgetrow(){returnr;}intgetcol(){returnc;}protected:intr;intc;double*mat;private:matrix1mul(matrix1&b);};classmatrix:publicmatrix1{friendistream&operator(istream&,constmatrix&);//重载输入运算符friendostream&operator(ostream&,constmatrix&);//重载输出运算符frienddoubledet(constmatrix&);//求行列式private:public:matrix(){mat=NULL;r=0;c=0;}matrix(intm,intn):matrix1(m,n){}matrix(constmatrix&b):matrix1(b){}~matrix(){}matrixoperator+(constmatrix&);//重载+运算符matrixoperator-(constmatrix&);//重载-运算符matrixoperator*(constmatrix&);//重载*运算符matrixoperator=(constmatrix&);//重载=运算符matrixoperator%(int);//数乘matrixoperator^(int);//幂double&operator()(inti,intj);matrixtrans();//转置boolswapp();//对称matrixleftt(int,int)const;//求余子式matrixinverse();//求逆};#endif//MATRIX_H_INCLUDEDMatrix.cpp#includeMatrix.hdouble&matrix::operator()(inti,intj)//加上异常处理{if(i0||i=r||j0||j=c)throwout_of_range(theelementisoutofrange);returnthis-mat[i*c+j];}istream&operator(istream&in,constmatrix&a){coutpleaseinputthematrixendl;for(intm=0;ma.r;m++)for(intn=0;na.c;n++)ina.mat[m*(a.c)+n];returnin;}ostream&operator(ostream&out,constmatrix&a){for(intm=0;ma.r;m++){for(intn=0;na.c;n++)outa.mat[m*(a.c)+n];outendl;}returnout;}matrix1matrix1::mul(matrix1&b){if((*this).c!=b.r)throwinvalid_argument(thematrixisnotmatched(mul));matrix1m(r,b.c);for(inti=0;ir;i++)for(intj=0;jb.c;j++)for(intk=0;kc;k++)m.mat[i*b.c+j]+=mat[i*c+k]*b.mat[k*b.c+j];returnm;}matrix1::matrix1(intm,intn):r(m),c(n){if(r1||c1)throwout_of_range(Theroworcolumnnumbershouldbelargerthan0.);mat=newdouble[r*c];for(inti=0;im*n;i++)mat[i]=0;}matrix1::matrix1(constmatrix1&b):r(b.r),c(b.c){mat=newdouble[r*c];for(inti=0;ir*c;i++)mat[i]=b.mat[i];}matrixmatrix::operator+(constmatrix&b){if(r!=b.r||c!=b.c)throwinvalid_argument(thematrixisnotmatched(+));matrixm(r,c);for(inti=0;ir*c;i++)m.mat[i]=mat[i]+b.mat[i];returnm;}matrixmatrix::operator-(constmatrix&b){if(r!=b.r||c!=b.c)throwinvalid_argument(thematrixisnotmatched(-));matrixm(r,c);for(inti=0;ir*c;i++)m.mat[i]=mat[i]-b.mat[i];returnm;}matrixmatrix::operator=(constmatrix&b){if(r!=b.r||c!=b.c)throwinvalid_argument(thematrixisnotmatched(=));for(inti=0;ithis-r*c;i++)this-mat[i]=b.mat[i];return*this;}matrixmatrix::operator*(constmatrix&b){if(c!=b.r)throwinvalid_argument(thematrixisnotmatched(*));matrixm(r,b.c);for(inti=0;ir;i++)for(intj=0;jb.c;j++)for(intk=0;kc;k++)m.mat[i*b.c+j]+=mat[i*c+k]*b.mat[k*b.c+j];returnm;}boolmatrix::swapp(){if((*this).r!=(*this).c)returnfalse;for(inti=0;ir;i++)for(intj=0;j=i;j++)if(this-mat[i*c+j]!=(*this).mat[j*c+i])returnfalse;returntrue;}matrixmatrix::trans(){matrixm(c,r);for(inti=0;ir;i++)for(intj=0;jc;j++)m.mat[j*r+i]=mat[i*c+j];returnm;}matrixmatrix::operator%(intn){matrixm(r,c);for(inti=0;ir;i++)for(intj=0;jc;j++)m.mat[i*c+j]=mat[i*c+j]*n;returnm;}matrixmatrix::operator^(intn){matrixm(r,c);matrixb(*this);for(inti=0,j=0;ir;i++,j++)m.mat[i*c+j]=1;while(n){if(n&1)m=m*b;n=1;b=b*b;}returnm;}matrixmatrix::leftt(inta,intb)const{matrixleft(r-1,c-1);inti,j,count1=0;for(i=0;ir;i++){if(i==a)continue;for(j=0;jc;j++){if(j==b)continue;left.mat[count1++]=mat[i*c+j];}}returnleft;}doubledet(constmatrix&a)//加上不是方针的异常处理{if(a.r==0)return0;elseif(a.r==1)returna.mat[0];elseif(a.r==2)return(a.mat[0]*a.mat[3]-a.mat[1]*a.mat[2]);else{doublenum=0;intaa=1,i;for(i=0;ia.r;i++){num+=aa*a.mat[i*(a.c)]*det(a.leftt(i,0));aa=-aa;}returnnum;}}matrixmatrix::inverse()//须包括不存在逆矩阵的异常处理{doublexx;if((xx=det(*this))==0)throwinvalid_argument(thedetofthismatrixiszero);matrixa(r,c);inti,j,aa=1;for(i=0;ir;i++)for(j=0;jc;j++){aa=(i+j)%2==0?1:-1;a.mat[j*r+i]=aa*det((*this).leftt(i,j))/xx;}returna;}
本文标题:矩阵类程序(全)
链接地址:https://www.777doc.com/doc-2175027 .html