C++模拟实现复数类

#include <iostream>
using namespace std;

class Complex
{
public:
    Complex(double Real = 0.0f, double Image = 0.0f)
        :_Real(Real)
        ,_Image(Image)
    {}

    Complex(const Complex& c)
        :_Real(c._Real )
        ,_Image(c._Image )
    {}
    ~Complex()
    {}
    Complex& operator=(const Complex& c)
    {
        if(this != &c)
        {
            _Real = c._Real ;
            _Image = c._Image ;
        }
        return *this;
    }
    Complex operator+(const Complex& c)
    {
        return Complex(_Real = _Real + c._Real,_Image = _Image + c._Image);
    }
    Complex operator-(const Complex& c)
    {
        return Complex(_Real = _Real - c._Real,_Image = _Image - c._Image);
    }

    Complex& operator*(const Complex& c)
    {
        return *this *= c;
    }

    Complex operator/(const Complex& c)
    {
        return *this /= c;
    }

    Complex& operator+=(const Complex& c)
    {
        _Real += c._Real ;
        _Image += c._Image ;
        return *this;
    }
    Complex& operator-=(const Complex& c)
    {
        _Real -= c._Real ;
        _Image -= c._Image ;
        return *this;
    }

    Complex& operator*=(const Complex& c)
    {
        _Real = _Real * c._Real - _Image * c._Image;
        _Image = _Real * c._Image + _Image * c._Real;
        return *this;
    }
    Complex& operator/=(const Complex& c)
    {
        _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;
    }

    bool operator >(const Complex& c)
    {
        if(*this != c)
        {
            if((_Real > c._Real) || (_Image > c._Image) )
                return 1;
        }
        return 0;
    }
    bool operator >=(const Complex& c)
    {
            if((_Real >= c._Real) || (_Image >= c._Image) )
                return 1;
    }
    bool operator <(const Complex& c)
    {
        if(*this != c)
        {
            if((_Real < c._Real) || (_Image < c._Image) )
                return 1;
        }
        return 0;
    }
    bool operator <=(const Complex& c)
    {
        if((_Real <= c._Real) || (_Image <= c._Image) )
            return 1;
    }
    bool operator ==(const Complex& c)
    {
        return ((_Real == c._Real) && (_Image == c._Image));
    }
    bool operator !=(const Complex& c)
    {
        return ((_Real != c._Real) && (_Image != c._Image));
    }
    friend ostream &operator<<(ostream& output, const Complex &c)
    {
        output << c._Real ;  
        if (c._Image  > 0) {  
            std::cout << "+";  
        }  
        std::cout << c._Image << "i";  
        return output;  
    }
private:
    double _Real;
    double _Image;
};

int main()
{
    //Complex c1(1.0,2.0);//赋值
    //Complex c2;
    //Complex c3;
    //c2 = c1;
    //c3 = c2 = c1;

    //Complex c1(1.0,2.0);//加减法
    //Complex c2(2.0,3.0);
    //Complex ret;
    //ret = c2 - c1;
    //cout<<ret;

    //Complex c1(1.0,2.0);//+=、-=
    //Complex c2(2.0,3.0);
    //c2 += c1;
    //cout<<c2;
    //c2 -= c1;
    //cout<<c2;

    Complex c1(1.0,2.0);//比大小
    Complex c2(2.0,3.0);
    //c1 == c2;
    if(c1 < c2)
    {
        cout<<"ok";
    }
    else 
    {
        cout<<"no";
    }


    //Complex c1(1.0,2.0);
    //Complex c2(2.0,3.0);
    //Complex c3;
    //c3 = c1 * c2;
    c3 = c1/c2;
    //cout<<c3;
    return 0;
}

其中加减乘除法那块还有不足。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值