配色: 字号:
复数的加减乘除运算
2012-06-07 | 阅:  转:  |  分享 
  
#include

#include

usingnamespacestd;

classcomplex

{

private:

doublereal,image;

public:

complex(){real=image=0;}

complex(doubler,doublei){real=r;image=i;}

complexoperator+(constcomplex&c);

complexoperator-(constcomplex&c);

complexoperator(constcomplex&c);

complexoperator/(constcomplex&c);

friendvoidprint(constcomplex&c);

};

inlinecomplexcomplex::operator+(constcomplex&c)

{

returncomplex(real+c.real,image+c.image);

}

inlinecomplexcomplex::operator-(constcomplex&c)

{

returncomplex(real-c.real,image-c.image);

}

inlinecomplexcomplex::operator(constcomplex&c)

{

returncomplex(realc.real-imagec.image,realc.real+imagec.image);

}

inlinecomplexcomplex::operator/(constcomplex&c)

{

returncomplex((realc.real+image+c.image)/(c.realc.real+c.imagec.image),(imagec.real-realc.image)/(c.realc.real+c.imagec.image));

}//注意分母都是(c.realc.real+c.imagec.image)

voidprint(constcomplex&c)

{

if(c.image<0)

cout<
else

cout<
}

intmain(intargc,charargv[])

{

complexc1(2.0,3.0),c2(4.0,-2.0),c3;

c3=c1+c2;

cout<
print(c3);

c3=c1-c2;

cout<
print(c3);

c3=c1c2;

cout<
print(c3);

c3=c1/c2;

cout<
献花(0)
+1
(本文系Honey_Dog首藏)