Matrix矩阵类.doc

上传人:灯火****19 文档编号:136066924 上传时间:2020-06-23 格式:DOC 页数:8 大小:221.50KB
返回 下载 相关 举报
Matrix矩阵类.doc_第1页
第1页 / 共8页
Matrix矩阵类.doc_第2页
第2页 / 共8页
Matrix矩阵类.doc_第3页
第3页 / 共8页
Matrix矩阵类.doc_第4页
第4页 / 共8页
Matrix矩阵类.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《Matrix矩阵类.doc》由会员分享,可在线阅读,更多相关《Matrix矩阵类.doc(8页珍藏版)》请在金锄头文库上搜索。

1、/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(constMat

2、rix&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

3、&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#i

4、nclude6#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)26

5、for(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)4849ofstrea

6、moutfile(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)69thr

7、owout_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

8、(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;+

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 中学教育 > 教学课件 > 高中课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号