C++重载实现m*n*k三维矩阵,且矩阵元素为复数
Posted Pistachiout
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++重载实现m*n*k三维矩阵,且矩阵元素为复数相关的知识,希望对你有一定的参考价值。
1.实验要求
- 设计实现三维(3D)矩阵A[i] [j] [k],支持矩阵运算符+,[],=等运算符重载,并完成结果展示
- 矩阵中元素为复数类型
2.实验截图
初始化A,B矩阵,可自由设定三维矩阵的行,列,高,以及对应元素的虚部实部
重载+,[],=运算符,输出3D矩阵C=A+B以及B[i]
3.代码展示
1.complex类
#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;
/*
Complex &Complex::operator=(const Complex &c1)
Complex* c;
c->real=c1.real;
c->imag=c1.imag;
return c;
*/
void display()
cout<<"("<<real<<","<<imag<<"i)";
double real;
double imag;
;
2.二维矩阵Martix_22类,矩阵元素为Complex类型
#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);// “=”运算符的重载
;
#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.三维矩阵类,包含二维矩阵的数组
#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);// []运算符的重载
;
#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].row=r;
res[j].col=c;
int i=r*c;
res[j].p=new Complex();
for(int a=0;a<i;a++)
cout<<"请输入第"<<a+1<<"个元素实部:"<<endl;
cin>>res[j].p[a].real;
cout<<"请输入第"<<a+1<<"个元素虚部:"<<endl;
cin>>res[j].p[a].imag;
*/
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;
/*temp.res[j].p->real=am.res[j].p->real+bm.res[j].p->real;
temp.res[j].p->imag=am.res[j].p->imag+bm.res[j].p->imag;*/
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函数
#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;
以上是关于C++重载实现m*n*k三维矩阵,且矩阵元素为复数的主要内容,如果未能解决你的问题,请参考以下文章