复数类Complex

#include
#include
#include
using namespace std;






class Complex
{
friend ostream& operator<<(ostream& _cout, const Complex& c);
public:


Complex(double r = 1.0, double i = 0.0)
: _real(r)
, _image(i)
{}
Complex operator+(const Complex&c)
{
return Complex(_real + c._real, _image + c._image);
}
Complex operator-(const Complex&c)
{
return Complex(_real - c._real, _image - c._image);
}


Complex operator*(const Complex&c)
{

return Complex(_real * c._real - _image*c._image, _image*c._real + _real*c._image);
}


Complex operator/(const Complex&c)
{
double m = c._real*c._real + c._image*c._image;
assert(m != 0);
_real = (_real*c._real + _image*c._image)/(c._real*c._real + c._image*c._image);
_image = (_image*c._real - _real*c._image)/(c._real*c._real + c._image*c._image);
return *this;
}
Complex& operator=(const Complex&c)//返回引用:Complex c3 = c2 = c1;连续赋值,如果返回值,则不能进行下一次赋值,c3.operator=(&c2.operator=(&c1))
{
if(this != &c)
{
_real = c._real;
_image = c._image;
}
return *this;
}
bool operator==(const Complex&c )
{
if(_real == c._real && _image == c._image )
return true;
else
return false;
}


double cab(Complex c)//求复数绝对值(模)
{
double d;
d = sqrt(c._real*c._real+c._image*c._image);
return d;
}
    Complex sqr(Complex c)//求复数平方根
{
_real=sqrt( (cab(c)+c._real)/2 );
_image=sqrt( (cab(c)-c._real)/2 );
return *this;
}
friend istream& operator>>(istream& _cin, Complex& c);

private:
double _real;
double _image;
};


ostream& operator<<(ostream& _cout, const Complex& c)
{
if(c._image == 0)
_cout< else
_cout< return _cout;
}


istream& operator>>(istream& _cin, Complex& c)
{
_cin>>c._real>>c._image;
return _cin;
}


void FunTest()
{
//Complex c1(1.0, 2.0);
//Complex c2(1.0, 2.0);
//Complex c3 = c2 = c1;
//Complex c3 = c2/c1;
//bool r = c1.operator==(c2);
Complex c1;
cin>>c1;
cout<

}


int main()
{
FunTest();
system("pause");
return 0;
}

复数类Complex_第1张图片

你可能感兴趣的:(c/c++)