C++编程,写一个复数的类(内详)
写一个复数(a+bi)的类,来实现加减乘数。例:(a+bi)×(c+di),ac-bd+(ad+bc)i,(a+c)+(b+d)i提示:construetordefaul...
写一个复数(a+bi)的类,来实现加减乘数。例:(a+bi)×(c+di),ac-bd+(ad+bc)i,(a+c)+(b+d)i
提示:
construetor
default 初始化
in it 设置实部,虚部
copy 实现“==”,“+”“-”“×”“/”
destretor
能否写个输入任意复数,来实现我的要求的,麻烦了各位 展开
提示:
construetor
default 初始化
in it 设置实部,虚部
copy 实现“==”,“+”“-”“×”“/”
destretor
能否写个输入任意复数,来实现我的要求的,麻烦了各位 展开
展开全部
正解见下:
#include<iostream>
using namespace std;
class complex//复数类声明
{
private:
double real;
double image;
public:
complex(double r=0.0,double i=0.0)//构造函数
{
real=r;
image=i;
}
complex operator+(complex c2);//+重载为成员函数
complex operator-(complex c2);//-重载为成员函数
complex operator*(complex c2);//*重载为成员函数
friend complex operator/(complex,complex);///重载为成员函数
bool operator==(complex c2);//==重载为成员函数
void display();
};
complex complex::operator +(complex c2)//重载的实现
{
complex c;
c.real=c2.real+real;
c.image=c2.image+image;
return complex(c.real,c.image);
}
complex complex::operator -(complex c2)//重载的实现
{
complex c;
c.real=real-c2.real;
c.image=image-c2.image;
return complex(c.real,c.image);
}
complex complex::operator *(complex c2)//重载的实现
{
complex c;
c.real=c2.real*real;
c.image=c2.image*image;
return complex(c.real,c.image);
}
complex operator/(complex c1,complex c2)//重载的实现
{
return complex(c1.real/c2.real,c1.image/c2.image);
}
bool complex::operator ==(complex c2)//重载的实现
{
if((real==c2.real)||(image==c2.image))return true;
else return false;
}
void complex::display()
{
cout<<"("<<real<<","<<image<<")"<<endl;
}
void main()
{
complex c1(5.0,4),c2(5.0,4),c3;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
c3=c1+c2;//使用重载运算符完成复数加法
cout<<"c3=c1+c2=";
c3.display();
c3=c1-c2;//使用重载运算符完成复数减法
cout<<"c3=c1-c2=";
c3.display();
c3=c1*c2;//使用重载运算符完成复数乘法
cout<<"c3=c1*c2=";
c3.display();
c3=c1/c2;//使用重载运算符完成复数除法
cout<<"c3=c1/c2=";
c3.display();
//使用重载运算符完成两个复数的比较
bool result=(c1==c2);
cout<<"(c1==c2)="<<result<<endl;
}
#include<iostream>
using namespace std;
class complex//复数类声明
{
private:
double real;
double image;
public:
complex(double r=0.0,double i=0.0)//构造函数
{
real=r;
image=i;
}
complex operator+(complex c2);//+重载为成员函数
complex operator-(complex c2);//-重载为成员函数
complex operator*(complex c2);//*重载为成员函数
friend complex operator/(complex,complex);///重载为成员函数
bool operator==(complex c2);//==重载为成员函数
void display();
};
complex complex::operator +(complex c2)//重载的实现
{
complex c;
c.real=c2.real+real;
c.image=c2.image+image;
return complex(c.real,c.image);
}
complex complex::operator -(complex c2)//重载的实现
{
complex c;
c.real=real-c2.real;
c.image=image-c2.image;
return complex(c.real,c.image);
}
complex complex::operator *(complex c2)//重载的实现
{
complex c;
c.real=c2.real*real;
c.image=c2.image*image;
return complex(c.real,c.image);
}
complex operator/(complex c1,complex c2)//重载的实现
{
return complex(c1.real/c2.real,c1.image/c2.image);
}
bool complex::operator ==(complex c2)//重载的实现
{
if((real==c2.real)||(image==c2.image))return true;
else return false;
}
void complex::display()
{
cout<<"("<<real<<","<<image<<")"<<endl;
}
void main()
{
complex c1(5.0,4),c2(5.0,4),c3;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
c3=c1+c2;//使用重载运算符完成复数加法
cout<<"c3=c1+c2=";
c3.display();
c3=c1-c2;//使用重载运算符完成复数减法
cout<<"c3=c1-c2=";
c3.display();
c3=c1*c2;//使用重载运算符完成复数乘法
cout<<"c3=c1*c2=";
c3.display();
c3=c1/c2;//使用重载运算符完成复数除法
cout<<"c3=c1/c2=";
c3.display();
//使用重载运算符完成两个复数的比较
bool result=(c1==c2);
cout<<"(c1==c2)="<<result<<endl;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询