C++矩陣類重載運(yùn)算符_第1頁
C++矩陣類重載運(yùn)算符_第2頁
C++矩陣類重載運(yùn)算符_第3頁
免費(fèi)預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、C+ 數(shù)據(jù)與結(jié)構(gòu)第三次作業(yè)潘隆武 21425202 機(jī)械工程題目:在已建立N*M的矩陣類前提下,重載運(yùn)算符“ +、“*、“ = 、“<<、“>>#include<iostream>#include<iomanip>using namespace std;class Matrixpublic:Matrix();/ 默認(rèn)構(gòu)造函數(shù)Matrix(int ,int );/ 初始化構(gòu)造函數(shù)Matrix(const Matrix &);/ 拷貝構(gòu)造函數(shù)Matrix();/ 析構(gòu)函數(shù)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;/主函數(shù)int main()system("COLOR 0e");int r,c;Matrix temp1,temp2;cou

3、t<<"請輸入矩陣 M1的行數(shù)N和列數(shù)M : "<<endl;cin>>r>>c;Matrix M1(r,c);cin>>M1;cout<<"請輸入矩陣 M2的行數(shù)N和列數(shù)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 的運(yùn)算結(jié)果: "<<endl<<endl;temp1=M1+M2;temp2=M1*M2;cout<<"M1+M2="<<endl<<temp1<<endl;cout<<"M1*M2="&

5、lt;<endl<<temp2<<endl;return 0;/創(chuàng)立默認(rèn)矩陣Matrix:Matrix()row=0;column=0;pt=NULL;/創(chuàng)立 row 行 column 列的矩陣,并將其初始化為零陣Matrix:Matrix(int r,int c):row(r),column(c)if(row<1|column<1)cout<<" 矩陣的行數(shù)和列數(shù)都必須大于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;/拷貝構(gòu)造函數(shù),以矩陣 A 創(chuàng)立新矩陣 B 并以 A 對 B 進(jìn)行初始化 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;/析構(gòu)函數(shù),對堆中的動態(tài)空間進(jìn)行釋放 Matrix:Matri

7、x()if(pt!=NULL)for(int i=0;i<row;i+) delete pti;delete pt;/對 ">>" 進(jìn)行重載,使它具有輸入矩陣的功能 istream & operator>>(istream &input,Matrix &m) "<<endl;cout<<" 請輸入 "<<m.row*m.column<<" 個數(shù)作為矩陣元素: for(int i=0;i<m.row;i+)for(int j=0;

8、j<m.column;j+) input>>m.ptij;return input;/對 "<<" 進(jìn)行重載,使它具有輸出矩陣的功能 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;+" 進(jìn)行重載,使它具有將兩個同型矩陣相加的功能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;/對 "*" 進(jìn)行重載,使它具有將兩個符合相乘條件的矩陣相乘的功能Matrix Matrix:operator*(Matrix &m)Matrix temp;if(column!=m.row)cout<<" 矩陣 M1 的列數(shù)和矩陣 M2 的行數(shù)不相等,不能相乘! "<<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;/對"=" 進(jìn)行重載,使它具有將一個矩陣賦給另一矩陣的功能Matrix Matrix:operator

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論