1.实验要求
1. 设计实现三维(3D)矩阵A[i][j] [k],支持矩阵运算符+,[],=等运算符重载,并完成结果展示
2. 矩阵中元素为复数类型
2.代码展示
1.complex类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include <iostream> using namespace std; class Complex { public: Complex() { real=0; imag=0; } Complex(double r,double i) { real=r; imag=i; } friend Complex operator+(Complex &c1, Complex &c2) { Complex c; c.real=c1.real+c2.real; c.imag=c1.imag+c2.imag; return c; } friend Complex operator-(Complex &c1, Complex &c2) { Complex c; c.real=c1.real-c2.real; c.imag=c1.imag-c2.imag; return c; }
void display(){ cout<<"("<<real<<","<<imag<<"i)"; } double real; double imag; };
|
2.二维矩阵Martix_22类,矩阵元素为Complex类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include<iostream> #include<stdlib.h> #include "Complex.cpp" using namespace std; class Matrix { public: int row,col; Complex *p; Matrix(); Matrix(int r,int c); Matrix(const Matrix &m); void disp(); friend Matrix operator + (const Matrix &am,const Matrix &bm); Matrix & operator = (const Matrix &a); };
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #include"Martix_22.h" Matrix::Matrix() { row=1; col=2; p=new Complex(); } Matrix::Matrix(int r,int c):row(r),col(c) { int i=r*c; this->row=r; this->col=c; p=new Complex(); for(int a=0;a<i;a++) { p[a]=*new Complex(); cout<<"请输入第"<<a+1<<"个元素实部:"<<endl; cin>>p[a].real; cout<<"请输入第"<<a+1<<"个元素虚部:"<<endl; cin>>p[a].imag; } } Matrix::Matrix(const Matrix &m) { row=m.row; col=m.col; int len=col*row; p=new Complex[len]; for(int i=0;i<len;i++) { p[i]=m.p[i]; } } void Matrix::disp() { for(int i=0;i<row;i++) { cout<<'\t'; for(int j=0;j<col;j++) p[i*col+j].display(); cout<<endl; } } Matrix operator + (const Matrix &am,const Matrix &bm) { if(am.row==bm.row&&am.col==bm.col) { int i,j; Matrix temp(am); i=am.row*am.col; for(j=0;j<i;j++) { temp.p[j]=am.p[j]+bm.p[j]; } return temp; } else { cout<<"++++program terminated!"<<endl; exit(1); } } Matrix & Matrix::operator = (const Matrix &bm) { if((row==0)&&(col==0)) { if(p) delete p; row=bm.row; col=bm.col; int len=bm.row*bm.col; p=new Complex[len]; for(int i=0;i<len;i++) { p[i]=bm.p[i]; } return *this; } else if((row==bm.row)&&(col==bm.col)) { for(int i=0;i<row*col;i++) { this->p[i]=bm.p[i]; } return *this; } else if((row!=bm.row)||(col!=bm.col)) { for(int i=0;i<row*col;i++) { this->p[i]=bm.p[i]; } return *this; } }
|
3.三维矩阵类,包含二维矩阵的数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include<iostream> #include<stdlib.h> #include "Martix_22.h" using namespace std; class Matrix_33 { public: Matrix *res; int h,row,col; Matrix_33(); Matrix_33(int r,int c,int h); Matrix_33(const Matrix_33 &m); void disp(); friend Matrix_33 operator + (const Matrix_33 &am,const Matrix_33 &bm); Matrix_33 & operator = (const Matrix_33 &a); Matrix & operator [] (int i); };
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| #include"Martix_33.h" Matrix_33::Matrix_33() { this->h=2; this->row=1; this->col=2; this->res=new Matrix(); } Matrix_33::Matrix_33(int r,int c,int h) { this->h=h; this->row=r; this->col=c; res=new Matrix(); for(int j=0;j<h;j++) {cout<<"请输入第"<<j+1<<"个二维矩阵:"<<endl; res[j].row=r; res[j].col=c; res[j].p=new Complex(); res[j]=*new Matrix(); res[j].row=r; res[j].col=c; res[j]=*new Matrix(r,c);
res[j].disp(); } }
Matrix_33::Matrix_33(const Matrix_33 &m_33) { h=m_33.h; row=m_33.row; col=m_33.col; res=m_33.res; if(!res) delete res; for(int j=0;j<h;j++) { res[j].row=row; res[j].col=col; res[j].p=new Complex(); res[j]=m_33.res[j]; } }
void Matrix_33::disp() { for(int j=0;j<h;j++) { cout<<"该三维矩阵的第"<<j+1<<"个"<<row<<'*'<<col<<"阶二维矩阵:" <<endl;
for(int i=0;i<row;i++) { for(int c=0;c<col;c++) { cout<<'\t'; res[j].p[i*col+c].display(); } cout<<endl; } cout<<endl; } } Matrix_33 operator + (const Matrix_33 &am,const Matrix_33 &bm) { if(am.h==bm.h) { Matrix_33 temp; temp.h=am.h; temp.row=am.row; temp.col=am.col; temp.res=new Matrix(); for(int j=0;j<am.h;j++) { temp.res[j].row=am.res[0].row; temp.res[j].col=am.res[0].col; temp.res[j].p=new Complex(); cout<<"第"<<j<<"个二维矩阵"<<endl;
for(int i=0;i<temp.row;i++) { for(int c=0;c<temp.col;c++) { temp.res[j].p[i*temp.col+c]=*new Complex(); temp.res[j].p[i*temp.col+c]=am.res[j].p[i*temp.col+c]+bm.res[j].p[i*temp.col+c]; temp.res[j].p[i*temp.col+c].display(); } cout<<endl; } } return temp; } }
Matrix & Matrix_33::operator [] (int i) { return this->res[i]; }
Matrix_33 & Matrix_33::operator = (const Matrix_33 &bm) { h=bm.h; row=bm.row; col=bm.col; this->res=new Matrix(); { for(int j=0;j<h;j++) { this->res[j].row=bm.res[j].row; this->res[j].col=bm.res[j].col; this->res[j].p=new Complex(); for(int i=0;i<row*col;i++) { this->res[j].p->real=bm.res[j].p->real; this->res[j].p->imag=bm.res[j].p->imag; } } } }
|
4.main函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include<iostream> #include "Martix_33.h" using namespace std; int main() { int h,row,col; cout<<"请输入三维矩阵的行数和列数和高:"<<endl; cin>>row>>col>>h; cout<<"请输入三维矩阵A:"<<endl; Matrix_33 am(row,col,h); cout<<"A"<<endl; am.disp(); cout<<"请输入三维矩阵B:"<<endl; Matrix_33 bm(row,col,h); cout<<"B:"<<endl; bm.disp(); int i; Matrix_33 cm; cout<<"C=A+B:"<<endl; cm=am+bm; cout<<"请输入i显示B[i],即第i个二维矩阵:"<<endl; cin>>i; bm[i].disp(); return 0; }
|
3.实验截图
初始化A,B矩阵,可自由设定三维矩阵的行,列,高,以及对应元素的虚部实部
![在这里插入图片描述]()
![在这里插入图片描述]()
![在这里插入图片描述]()
重载+,[],=运算符,输出3D矩阵C=A+B以及B[i]
![在这里插入图片描述]()