Matrix矩阵类_第1页
Matrix矩阵类_第2页
Matrix矩阵类_第3页
Matrix矩阵类_第4页
Matrix矩阵类_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/Matrix.h/矩阵类定义#ifndefMATRIX_H#defineMATRIX_H#include#include/usingnamespacestd;一般头文件中使用完全限定域名字,而不是包含命名空间,防止重复包含头文件时造成的资源浪费classMatrix/从流中读入矩阵friendstd:istream&operator(std:istream&is,MatrixA);/输出矩阵friendstd:ostream&operator(std:ostream&os,constMatrix&A);/将矩阵输出到名为str的文件中friendvoidprint_file(constMatrix&A,constchar*str);public:/定义空矩阵Matrix()elems=NULL;row=0;col=0;/定义m*n零矩阵Matrix(intm,intn);/定义m*n矩阵,由a初始化Matrix(intm,intn,double*a,intsize=0);/复制构造函数Matrix(constMatrix&B);/从文件str中读取矩阵Matrix(constchar*str);Matrix()deleteelems;row=0;col=0;/重载算数操作符Matrix&operator=(Matrix&B);Matrixoperator+(constMatrix&B)const;Matrixoperator-(constMatrix&B)const;Matrixoperator*(constMatrix&B)const;/返回矩阵第i行第j列元素double&operator()(inti,intj)const;doubleget_row()constreturnrow;doubleget_col()constreturncol;/矩阵转置Matrix&trans()const;protected:private:double*elems;introw,col;#endif1/Matrix.cpp2/函数实现34#includeMatrix.h5#include6#include7#include8#include9#include1011usingnamespacestd;1213/重载下标操作符,返回Ai,j14double&Matrix:operator()(inti,intj)const1516if(i=row|j=col)17throwout_of_range(Thesuffixisoutofrange);1819returnelemsi*col+j;202122/从输入流中读入矩阵23istream&operator(istream&is,Matrix&A)2425for(inti=0;i!=A.get_row();+i)26for(intj=0;j!=A.get_col();+j)27isA(i,j);28returnis;293031/输出矩阵32ostream&operator(ostream&os,constMatrix&A)3334for(inti=0;i!=A.get_row();+i)3536for(intj=0;j!=A.get_col();+j)37osA(i,j);38coutendl;3940cout-endl;4142returnos;4344;4546/将矩阵A输出到文件str中47voidprint_file(constMatrix&A,constchar*str)4849ofstreamoutfile(Matrix_out.txt,ios:app);50if(!outfile)51throwdomain_error(Cannotopenthisfile.);5253for(inti=0;i!=A.row;+i)5455for(intj=0;j!=A.col;+j)56outfileA(i,j);57outfileendl;5859outfile-endl;6061outfile.clear();62outfile.close();636465/构造m*n零矩阵66Matrix:Matrix(intm,intn):row(m),col(n)6768if(m1|n1)69throwout_of_range(Theroworcolumnnumbershouldbelargerthan0.);70elems=newdoublem*n;71for(inti=0;i!=m*n;+i)72elemsi=0;737475/构造m*n矩阵,从数组a中读入数据存储到矩阵中76Matrix:Matrix(intm,intn,double*a,intsize):row(m),col(n)7778if(m0|n0|sizeelemsi+;124125infile.clear();126infile.close();127128129130/矩阵复制构造函数131Matrix:Matrix(constMatrix&B):row(B.row),col(B.col)132133if(row!=B.row)|(col!=B.col)134throwinvalid_argument(TheMatrixshouldbematched.);135136elems=newdoublerow*col;137for(inti=0;i!=row*col;+i)138elemsi=B.elemsi;139140;141142/重载矩阵赋值操作符143Matrix&Matrix:operator=(Matrix&B)144145146if(row!=B.row)|(col!=B.col)147throwinvalid_argument(Thematrixshouldbematched.);148row=B.row;149col=B.col;150elems=newdoublerow*col;151for(inti=0;i!=row*col;+i)152elemsi=B.elemsi;153154return*this;155;156157/重载矩阵相加操作符158MatrixMatrix:operator+(constMatrix&B)const159160if(row!=B.row)|(col!=B.col)161throwinvalid_argument(Thematrixshouldbematched);162163Matrix&T=*newMatrix;164T.row=row;165T.col=col;166T.elems=newdoublerow*col;167168for(inti=0;i!=row*col;+i)169T.elemsi=elemsi+B.elemsi;170returnT;171172;173174/重载矩阵相减操作符175MatrixMatrix:operator-(constMatrix&B)const176177if(row!=B.row)|(col!=B.col)178throwinvalid_argument(Thematrixshouldbematched);179180Matrix&T=*newMatrix;181T.row=row;182T.col=col;183T.elems=newdoublerow*col;184185for(inti=0;i!=row*col;+i)186T.elemsi=elemsi-B.elemsi;187returnT;188189;190191/重载矩阵相乘操作符192MatrixMatrix:operator*(constMatrix&B)const193194if(col!=B.row)195throwinvalid_argument(Thematrixshouldbematched.);196197Matrix&T=*newMatrix;198T.row=row;199T.col=B.col;200T.elems=newdoubleT.row*T.col;201202for(inti=0;i!=T.row;+i)203for(intj=0;j!=T.col;+j)204205T.elemsi*T.col+j=0;206for(intk=0;k!=col;+k)207T.elemsi*T.col+j+=elemsi*col+k*B.elemsk*B.col+j;208209210returnT;211;212213/转置矩阵214Matrix&Matrix:trans()const215216Matrix&T=*newMatrix;/new返回的是指针,需要解引用217T.row=col;218T.col=row;219T.elems=newdoublerow*col;220for(inti=0;i!=T.row;+i)221for(intj=0;j!=T.col;+j)222T.elemsi*T.col+j=elemsj*col+i;223returnT;2241/Mainfun.cpp2/测试编写的矩阵类3#includeMatrix.h4#include5#include6#include7#include8#include910usingnamespacestd;1112intmain()1314doubled12=1,2,3,4,5,6,7,8,1,2,3,4;15doubled212=1,2,3,4,1,2,3,4,5,6,7,8;16MatrixA(3,4,d,12);17MatrixB(3,4,d2,12);18MatrixC=B.trans();1920coutA=nAB=nBC=nCendl;21coutA+BnA+BB*CnB*Cendl;2223/将矩阵输出到文件Matrix_out.txt中24print_file

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论