急!!!复数运算C语言程序

急!!!复数运算C语言程序,第1张

structcomplex{

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

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12533707.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存