c++ 之 复数类的实现

//实现复数类的基本成员函数
//实现复数之间比较大小
//实现复数的四则运算

/* 复数加法:
复数z = a + bi(a, b为实数)
当b = 0时, z为实数, 可以比较大小;
当b不为零时, z为虚数, (a = 0时为纯虚数), 不能比较大小.*/

/* 复数减法:
设z1=a+bi,z2=c+di是任意两个复数,
则它们的差是 (a+bi)-(c+di)=(a-c)+(b-d)i.
两个复数的差依然是复数,它的实部是原来两个复数实部的差,它的虚部是原来两个虚部的差
*/

//复数乘法:设z1 = a + bi,z2 = c + di(a、b、c、d∈R)是任意两个复数,
//那么它们的积(a + bi)(c + di) = (ac - bd) + (bc + ad)i.

//复数除法:
//(a + bi) / (c + di) = (ac + bd) / (c ^ 2 + d ^ 2) + (bc - ad) / (c ^ 2 + d ^ 2)i





#include<iostream>
using  namespace  std;

class Complex
{
public:
    //四个默认成员函数 
    //构造函数 
    Complex(double real = 0.0, double image = 0.0)
        :_real(real), _image( image)
    {

    }
    //拷贝构造函数 
    Complex(const Complex& c)
    {
        _real = c._real;
        _image = c._image;
    }
    //析构函数 
    ~Complex()
    {

    }
    //赋值操作符的重载 
    Complex& operator=(Complex& c)
    {
        if (this!=&c)
        {
            this->_real = c._real;
            this->_image = c._image;
        }
        return *this;
    }
    //复数比较大小
    bool operator==(const Complex& c)
    {
        if (this->_image != 0 || c._image != 0)
        {
            cout << "虚数不能比较大小" << endl;
            return false;
        }
        return (this->_image == c._image) && (this->_real==c._real);
    }
    bool operator!=(const Complex& c)
    {
        if (this->_image != 0 || c._image != 0)
        {
            cout << "虚数不能比较大小" << endl;
            return false;
        }
        return !(this->_image == c._image) && (this->_real == c._real);
    }

    bool operator>(const Complex& c)
    {
        if (this->_image != 0 || c._image != 0)
        {
            cout << "虚数不能比较大小" << endl;
            return false;
        }
        else
        {
            if (_real > c._real)
                return true;
            else
                return false;
        }
    }
    bool operator>=(const Complex& c)
    {
        if (this->_image != 0 || c._image != 0)
        {
            cout << "虚数不能比较大小" << endl;
            return false;
        }
        return (*this == c || *this > c);
    }
    bool operator<(const Complex& c)
    {
        if (this->_image != 0 || c._image != 0)
        {
            cout << "虚数不能比较大小" << endl;
            return false;
        }
        return !(*this >= c);
    }
    bool operator<=(const Complex& c)
    {
        if (this->_image != 0 || c._image != 0)
        {
            cout << "虚数不能比较大小" << endl;
            return false;
        }
        return !(*this>c);
    }

    //复数的四则运算
    Complex operator+(const Complex& c)  
    {
        Complex sum;
        sum._real = _real + c._real;
        sum._image = _image + c._image;
        return sum;
    }
    Complex& operator+=(const Complex& c)
    {
        _real = this->_real + c._real;
        _image = this->_image + c._image;
        return *this;
    }

    Complex operator-(const Complex& c)
    {
        Complex  sub;
        sub._real = _real - c._real;
        sub._image = _image - c._image;
        return sub;
    }
    Complex& operator-=(const Complex& c)
    {
        _real = this->_real - c._real;
        _image = this->_image - c._image;
        return *this;
    }
    Complex operator*(const Complex& c)
    {
        Complex mul;
        mul._real = (this->_real * c._real) - (this->_image *c._image);
        mul._image = (this->_real * c._real) +(this->_image *c._image);
        return mul;
    }
    Complex operator/(const Complex& c)
    {
        Complex div;
        div._real = ((this->_real*c._real) + (this->_image*c._image)) / (c._real*c._real + c._image*c._image);
        div._image = ((this->_image*c._real) - (this->_real*c._image)) / (c._real*c._real + c._image*c._image);
        return div;
    }
    //前置++ 
    Complex& operator++()
    {
        this->_real++;
        this->_image++;
        return *this;
    }
    //后置++ 
    Complex operator++(int)
    {
        Complex tmp(*this);
        this->_real++;
        this->_image++;

        return tmp;
    }
    void Display()
    {
        cout << _real << "+" << _image << "i" << endl;
    }


private:
    double _real;
    double _image;
};

void  test()
{
    Complex c1(2, 1);
    c1.Display();
    Complex c2(3,1);
    c2.Display();
    bool ret = (c1 <c2);
    cout << ret << endl;
    Complex c3 = c1 - c2;
    c3.Display();
    Complex c4 = c1 + c2;
    c4.Display();
    c1-=c2;
    c1.Display();
    Complex c5 = c1 /c2;
    c5.Display();
    c1++;
    c1.Display();
    ++c1;
    c1.Display();
}



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

结果如下:
这里写图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值