矩阵类 重载(Matrix class overloading).doc

上传人:bao****ty 文档编号:132471405 上传时间:2020-05-16 格式:DOC 页数:24 大小:48.50KB
返回 下载 相关 举报
矩阵类 重载(Matrix class overloading).doc_第1页
第1页 / 共24页
矩阵类 重载(Matrix class overloading).doc_第2页
第2页 / 共24页
矩阵类 重载(Matrix class overloading).doc_第3页
第3页 / 共24页
矩阵类 重载(Matrix class overloading).doc_第4页
第4页 / 共24页
矩阵类 重载(Matrix class overloading).doc_第5页
第5页 / 共24页
点击查看更多>>
资源描述

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

1、矩阵类 重载(Matrix class overloading)Assignment operations and function call operators () to transpose the matrix, add, subtract, multiply, and find negative - must deal with various exceptions(cross bounds, subscript errors, mismatches, etc.) and then write the main function for a thorough test. */*/#in

2、clude#include /getch prototypeUsing namespace std;/ / space application exception classClass Wrongmem;/ / exception class.Class Outofrange;/ / definition error exception classClass Differ;/* matrix class began */Const int MAX_SIZE=1000;TemplateClass MatrixPublic:Two / / constructor parametersMatrix

3、(int, R, int, c): rows (R), cols (c)If (rowsMAX_SIZE|rowsMAX_SIZE|colsMAX_SIZE|rowsMAX_SIZE|cols1), throw, Outofrange ();If (size & & sizesizeof (rows*cols) /sizeof (int) throw (Outofrange);Mem=new T rows*cols;If (mem=NULL), throw, Wrongmem ();For (int i=0; irows*cols; i+)Memi=datai;/ / destructorMa

4、trix (delete MEM) ;T, setrows (), constreturn, rows;T, setcols (), constreturn, cols;/ / modify matrixVoid setMatrix (T, *data, int, size);Void setMatrix (T *data);/ / overloaded function call operator ()T, &operator () (int, I, int, J);/ / copy constructorMatrix (const, Matrix, &x);/ / overloaded a

5、ssignment operatorMatrix operator= (Matrix &x);Transpose / / heavy!Matrix operator! ();/ / + overloadingMatrix operator+ (const, Matrix, &x);/ / - heavyMatrix operator- (const, Matrix, &x);* / / heavyMatrix operator* (const, Matrix, &x);For the negative - / / heavyMatrix, operator- ();Private:T *mem

6、;Const, int, rows, cols;/* function */ / modify matrixTemplate Void Matrix: setMatrix (T, *data, int, size)If (rows*colssize), throw, Outofrange ();For (int i=0; isize; i+)Memi=datai;TemplateVoid Matrix: setMatrix (T, *data)For (int i=0; irows*cols; i+)Memi=datai;/ / overloaded function call operato

7、r ()Template T &Matrix: Operator () (int, I, int, J)If (i=rows|j=cols), throw, Outofrange ();Else return memi*cols+j;/ / input operator overloadingTemplateIstream &operator (istream, &in, Matrix, &x)For (int i=0; ix.setrows (); i+)For (int j=0; jx (I, J);Return in;The output / loadTemplate Ostream &

8、operator (ostream, &out, Matrix, &x)For (int i=0; ix.setrows (); i+)For (int j=0; jx.setcols (); j+)Outx (I, J) ;Outendl;Outendl;Return out;/ / copy constructorTemplate Matrix: Matrix (const, Matrix, &x): rows (x.rows), cols (x.cols)Mem=new T rows*cols;If (mem=NULL), throw, Differ ();ElseFor (int i=

9、0; ix.rows*x.cols; i+)Memi=x.memi;/ /重载附值运算符=模板矩阵如果(行)!= x.rows | |列!= x.cols)把differ();如果(这个)!= x)删除 ;MEM =新行*列;如果(MEM = = null)把wrongmem();其他的为(int i = 0;i x.rows * x.cols;i+)MEM 我 = x.mem 我;返回*;/ /重载转置!模板矩阵!()矩阵温度(列、行);为(int i = 0;i 列;i+)为(int = 0;j 行;j + +)温度(i,j)=(*)(j,i);回复;/ /重载+模板矩阵矩阵:操作符+(常

10、量矩阵和X)int i;如果(行)!= x.rows | |列!= x.cols)把differ();矩阵(*此);如果(行= = x.rows &列= = x.cols)为(i = 0;i 行*列;i+)温度。MEM 我 = MEM 我 + x.mem 我;回复;别人把differ();/ /重载模板矩阵矩阵:操作符(常量矩阵和X)int i;如果(行)!= x.rows | |列!= x.cols)把differ();矩阵(*此);如果(行= = x.rows &列= = x.cols)为(i = 0;i # 3/ /重载矩阵乘法运算符*模板矩阵矩阵:操作符*(常量矩阵和X)如果(列!= x.rows)把differ();矩阵温度(行,x.cols);为(int = i 0;i 行;i + +)为(j = 0;J x.cols;j+)温度(i,j)0;为(int k = 0;Kx.rows;K + +)温度(I,J)+ = MEM 我 *一+ K x.mem k x.cols +;回复;/ /重载求负模板矩阵矩阵(*此);为(int i = 0;i 行*列;i+)临时文件(= 1)*;回复;/ *主函数开始*

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 其它相关文档

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