floatrmz//纯段运实部
floatlmz//做梁虚部
}
//产生一个复数.
complexgetacomplex(floata,floatb){
complexnode=newcomplex()
node.rmz=a
node.lmz=b
returnnode}
//两个复数求和
complexaddcomplex(complexcomplex1,complexcomplex2)
{
complexnode=newcomplex()
node.rmz=complex1.rmz+complex2.rmz
node.lmz=complex1.lmz+complex2.lmz
returnnode
}
//求两燃衫个复数的差
complexsubcomplex(complexcomplex1,complexcomplex2)
{
complexnode=newcomplex()
node.rmz=complex1.rmz-complex2.rmz
node.lmz=complex1.lmz-complex2.lmz
returnnode
}
//求两个复数的积
complexproductcomplex(complexcomplex1,complexcomplex2)
{
complexnode=newcomplex()
node.rmz=complex1.rmz*complex2.rmz-complex1.lmz*complex2.lmz
node.lmz=complex1.lmz*complex2.rmz+complex2.lmz*complex2.rmz
returnnode
}
//求实部
floatgetcomplexrmz(complexcomplex1)
{
returncomplex1.rmz
}
//求虚部
floatgetcomplexlmz(complexcomplex1)
{
returncomplex1.lmz
}
#include <iostream.h>class Complex{
private:
float _real
float _image
public:
Complex(float real=0,float image=0)
Complex operator +(const Complex &rhs)
Complex operator -(const Complex &rhs)
Complex operator *(const Complex &rhs)
float GetReal()const
float GetImage()const
}
Complex::Complex(float real,float image)
{
_real=real
_image=image
}
Complex Complex::operator +(const Complex &rhs)
{
_real+=rhs.GetReal()
_image+=rhs.GetImage()
return *this
}
Complex Complex::operator -(const Complex &rhs)
{
_real-=rhs.GetReal()
_image-=rhs.GetImage()
return *this
}
Complex Complex::operator *(const Complex &rhs)
{
_real=_real*rhs.GetReal()-_image*rhs.GetImage()
_image=_real*rhs.GetImage()+_image*rhs.GetReal()
return *this
}
float Complex::GetReal()const
{
return _real
}
float Complex::GetImage()const
{
return _image
}
void main()
{
cout<<"依次输入两个复数的实部和孙租宴虚部"<<endl
float realA,imageA,realB,imageB
cin>>realA>>imageA>>realB>>imageB
Complex A(realA,imageA)
Complex B(realB,imageB)
Complex temp
//减法和乘法类似
temp=A+B
cout<<"两者之型锋和为:"<<temp.GetReal()<<"+"<<temp.GetImage()<<"i"<则银<endl
cout<<"其实部为"<<temp.GetReal()<<"虚部为"<<temp.GetImage()<<endl
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)