C++矩阵类重载运算符.docx

上传人:scccc 文档编号:13593448 上传时间:2022-01-19 格式:DOCX 页数:7 大小:36.29KB
返回 下载 相关 举报
C++矩阵类重载运算符.docx_第1页
第1页 / 共7页
C++矩阵类重载运算符.docx_第2页
第2页 / 共7页
C++矩阵类重载运算符.docx_第3页
第3页 / 共7页
C++矩阵类重载运算符.docx_第4页
第4页 / 共7页
C++矩阵类重载运算符.docx_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《C++矩阵类重载运算符.docx》由会员分享,可在线阅读,更多相关《C++矩阵类重载运算符.docx(7页珍藏版)》请在三一文库上搜索。

1、C+数据与结构第三次作业潘隆武21425202机械工程题目:在已建立N*M的矩阵类前提下,重裁运算符“+” 、“*”、廿、vv ,*0#in CIUdeVioStream#in cludeUSing n amespace std;class Matrix(public:MatriX();默认构造由数MatriX(int ,int);初始化构造函数MatriX(COnst Matrix &);拷贝构造函数Matrix();析构函数Matrix OPerator+(Matrix &);Matrix OPerator*(Matrix &);Matrix OPeratOr=(MatriX &);fri

2、e nd istream & OPerator(istream &,M atrix &);frie nd OStream & OPerator(oStream &,M atrix &); private:int row;int colu mn;double *pt;主函数int mai n()(SyStem(uCOLOR 0eH);int r,c;Matrix tempi,temp2;coutv”请输入矩阵M1的行数(N)和列数(M ) : endl;cin rc;Matrix M1(r,c);Cin M1;coutv”请输入矩阵M2的行数(N)和列数(M ) : endl;cin rc;Ma

3、trix M2(r,c);cin M2;cout=e ndl;coutHM 1 -e ndlM1e ndl;coutHM2=Me ndlM2e ndl;cout,= =e ndl;coutHM1 与 M2 的运算结果:vvendlwendl;temp1=M1+M2;temp2=M1*M2;coutHM 1 +M2=,e ndltemp1e ndl;coutHM 1 *M2=,e ndltemp2e ndl;return 0;)zz创建默认矩阵MatriX:MatriX()(row=0;colu mn=0;pt=NULL;)ZZ创建row行column列的矩阵,并将其初始化为零阵Matrix::

4、MatriX(i nt r,i nt c):row(r),colum n(c)(if(row1 | | colu mn 1)cout”矩阵的行数和列数都必须大于0! endl;else(pt=new double*row;for(i nt i=0;irow;i+)(*(pt+i)=new doublecolu mn;for(i nt j=0;jcolu mn ;j+)(*(*(pt+i)+j)=O.O;)拷贝构造函数,以矩阵 A创建新矩阵B并以A对B进行初始化MatriX:MatriX(CO nst Matrix &m)row=m.row;colu mn=m.colu mn;pt=new do

5、uble*row; for(i nt i=0;irow;i+)*(pt+i)=new doublecolu mn;for(i nt j=0;jcolu mn ;j+)(V(Pt+i)+j)=m.ptiO;)析构曲数,对堆中的动态空间进行释放MatriX:-Matrix()(if(pt!=NULL)(for(i nt i=0;i进行重载 使它具有输入矩阵的功能istream & OPerator(istream &in put,Matrix &m)(He ndl;coutvv”请输入vvm.rowm.columy个数作为矩阵元素:for(i nt i=0;im.row;i+)(for(i nt

6、j=0;jm.colu mn ;j+)(in putm.pti0;)return in put;)对 进行重载,使它具有输出矩阵的功能OStream & OPerator(ostream &output,Matrix &m)(for(i nt i=0;im.row;i+)(for(i nt j=0;jm.colu mn ;j+)(OUtPUtleftsetw(5)m.pti|j;OUtPUte ndl;)return output;)对+进行重载,使它具有将两个同型矩阵相加的功能Matrix Matrix::oPerator+(Matrix &m)(Matrix temp;if(row!=m.

7、row I I colu mn !=m.colu mn)cout矩阵M1和矩阵M2不是同类型矩阵,不能相加!else(temp.row=row;temp.colu mn=ColU mn;temp.pt=new double*temp.row;for(i nt i=0;itemp.row;i+)(temp.pti=new doubletemp.colu mn;for(i nt j=0;jtemp.colu mn ;j+)(temp.ptiD=ptiO+m.ptiOJ;)return temp;)对伊”进行重载,使它具有将两个符合相乘条件的矩阵相乘的功能Matrix Matrix::oPerato

8、r*(Matrix &m)(Matrix temp;if(colu mn !=m.row)cout”矩阵M1的列数和矩阵M2的行数不相等,不能相乘!else(temp.row=row;temp.colu mn=m.colu mn;temp.pt=new double*temp.row;for(i nt i=0;itemp.row;i+)(temp.pti=new doubletemp.colu mn;for(i nt j=0;jtemp.colu mn ;j+)Mendlendl;Hendlendl;double sum=0;for(i nt k=0;kcolu mn; k+)( SUm=SU

9、m+p tik*m.ptkj;) temp.ptiO=sum;)return temp;)对三进行重载,使它具有将一个矩阵赋给另一矩阵的功能 Matrix Matrix::oPerator=(MatriX &m)(row=m.row;colu mn=m.colu mn;pt=new double*m.row;for(i nt i=0;im.row;i+)(pti=new doublem.colu mn;for(i nt j=0;jCL M E NnC + + 2 CADebugVVext-1 三%三请输入矩阵M的行数(N)和列数(H), 褊|入12个数作为矩阵元素:1 2 3 6 S 4 7

10、8 9 10 11 12请输入矩阵2的行敌(H)和列数(H):京备入12个数作为矩阵元素:123456789 10 11 12m与H2的a宜寤豆矩阵取的列数和矩阵H2的行数不相等,不能相乘!Press any key to cont inue E:SOFTDOCUMENTC+ +20Debug20.exe-请输:拒阵M的行数(N)和列数(K), 三XI2个数作为矩阵元素:123456789 10 11 12请输入矩阵他的行数(N)和列数(M): 43请输入12个数作为矩阵元素*123456987 10 11 12M与2的运算结果:矩阵M和矩阵M2不是同类型矩阵,不能相加!M1*M2 =84196308Ml*tl2= 7680172184268288PreSS any key to COntinUe

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1