C++矩阵类重载运算符_第1页
C++矩阵类重载运算符_第2页
C++矩阵类重载运算符_第3页
免费预览已结束,剩余1页可下载查看

下载本文档

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

文档简介

1、C+ 数据与结构第三次作业潘隆武 21425202 机械工程题目:在已建立N*M的矩阵类前提下,重载运算符“ +、“*、“ = 、“<<、“>>#include<iostream>#include<iomanip>using namespace std;class Matrixpublic:Matrix();/ 默认构造函数Matrix(int ,int );/ 初始化构造函数Matrix(const Matrix &);/ 拷贝构造函数Matrix();/ 析构函数Matrix operator+(Matrix &);Matri

2、x operator*(Matrix &);Matrix operator=(Matrix &);friend istream & operator>>(istream &,Matrix &);friend ostream & operator<<(ostream &,Matrix &);private:int row;int column;double *pt;/主函数int main()system("COLOR 0e");int r,c;Matrix temp1,temp2;cou

3、t<<"请输入矩阵 M1的行数N和列数M : "<<endl;cin>>r>>c;Matrix M1(r,c);cin>>M1;cout<<"请输入矩阵 M2的行数N和列数M: "<<endl;cin>>r>>c;Matrix M2(r,c);cin>>M2; cout<<"="<<endl;cout<<"M1="<<endl<<M1&l

4、t;<endl;cout<<"M2="<<endl<<M2<<endl;cout<<"= ="<<endl;cout<<"M1 与 M2 的运算结果: "<<endl<<endl;temp1=M1+M2;temp2=M1*M2;cout<<"M1+M2="<<endl<<temp1<<endl;cout<<"M1*M2="&

5、lt;<endl<<temp2<<endl;return 0;/创立默认矩阵Matrix:Matrix()row=0;column=0;pt=NULL;/创立 row 行 column 列的矩阵,并将其初始化为零阵Matrix:Matrix(int r,int c):row(r),column(c)if(row<1|column<1)cout<<" 矩阵的行数和列数都必须大于0! "<<endl;elsept=new double*row;for(int i=0;i<row;i+)*(pt+i)=new

6、 doublecolumn;for(int j=0;j<column;j+)*(*(pt+i)+j)=0.0;/拷贝构造函数,以矩阵 A 创立新矩阵 B 并以 A 对 B 进行初始化 Matrix:Matrix(const Matrix &m)row=m.row;column=m.column;pt=new double*row;for(int i=0;i<row;i+)*(pt+i)=new doublecolumn;for(int j=0;j<column;j+)*(*(pt+i)+j)=m.ptij;/析构函数,对堆中的动态空间进行释放 Matrix:Matri

7、x()if(pt!=NULL)for(int i=0;i<row;i+) delete pti;delete pt;/对 ">>" 进行重载,使它具有输入矩阵的功能 istream & operator>>(istream &input,Matrix &m) "<<endl;cout<<" 请输入 "<<m.row*m.column<<" 个数作为矩阵元素: for(int i=0;i<m.row;i+)for(int j=0;

8、j<m.column;j+) input>>m.ptij;return input;/对 "<<" 进行重载,使它具有输出矩阵的功能 ostream & operator<<(ostream &output,Matrix &m) for(int i=0;i<m.row;i+)for(int j=0;j<m.column;j+) output<<left<<setw(5)<<m.ptij;output<<endl; return output;/对&q

9、uot;+" 进行重载,使它具有将两个同型矩阵相加的功能Matrix Matrix:operator+(Matrix &m)Matrix temp;if(row!=m.row|column!=m.column)cout<<" 矩阵 M1 和矩阵 M2 不是同类型矩阵,不能相加! "<<endl<<endl; else temp.row=row;temp.column=column;temp.pt=new double*temp.row;for(int i=0;i<temp.row;i+)temp.pti=new d

10、oubletemp.column;for(int j=0;j<temp.column;j+) temp.ptij=ptij+m.ptij; return temp;/对 "*" 进行重载,使它具有将两个符合相乘条件的矩阵相乘的功能Matrix Matrix:operator*(Matrix &m)Matrix temp;if(column!=m.row)cout<<" 矩阵 M1 的列数和矩阵 M2 的行数不相等,不能相乘! "<<endl<<endl; else temp.row=row;temp.column=m.column;temp.pt=new double*temp.row;for(int i=0;i<temp.row;i+)temp.pti=new doubletemp.column; for(int j=0;j<temp.column;j+)double sum=0;for(int k=0;k<column;k+)sum=sum+ptik*m.ptkj; temp.ptij=sum;return temp;/对"=" 进行重载,使它具有将一个矩阵赋给另一矩阵的功能Matrix Matrix:operator

温馨提示

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

评论

0/150

提交评论