C++矩陣類重載運算符_第1頁
C++矩陣類重載運算符_第2頁
C++矩陣類重載運算符_第3頁
免費預覽已結束,剩余3頁可下載查看

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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

提交評論