复数类

复数类:
**复数简单的运算,复数x被定义为二元有序实数对(a,b);记为z=a+bi;i是虚数单位。
**在复数a+bi中,a=Re(z)成为实部,b=lm(z)称为虚部。当虚部为0时,这个虚数可以视为实数。当虚部部位0,实部为0时,称z为纯虚数。

头文件:

#ifndef _COMPLEX_H__
#define _COMPLEX_H__


#include<stdlib.h>
#include<iostream>
using namespace std;

//复数类的申明
class Complex
{
public:
    Complex(double real = 0.0, double image = 0.0);
    Complex(const Complex& z);
    ~Complex();
    Complex& operator=(Complex& z);
public:
    Complex operator+(Complex& z);
    Complex operator-(Complex& z);

public:
    Complex& operator++();
    Complex operator++(int);
    bool operator==(Complex&z);
    bool operator<(Complex&z);
    bool operator<=(Complex&z);
    bool operator>(Complex&z);
    bool operator>=(Complex&z);
    bool operator!=(Complex&z);
public:
    void display();
private:
    double _real;
    double _image;
};


#endif// _COMPLEX_H__

函数实现部分:

#define _CRT_SECURE_NO_WARNINGS 1
#include"complex.h"

//构造函数
Complex::Complex(double real , double image )
{
    _real = real;
    _image = image;
}

//拷贝构造函数
Complex::Complex(const Complex& z)
{
    _real = z._real;
    _image = z._image;
}

//析构函数
Complex::~Complex()
{
    //这里析构函数不用做任何操作
}

//运算符的重载
Complex& Complex:: operator=(Complex& z)
{
    if (this != &z)
    {
        _real = z._real;
        _image = z._image;
    }
    return *this;
}

//运算符重载 +
Complex Complex::operator+(Complex& z)
{
    Complex tmp(*this);
    tmp._real = _real + z._real;
    tmp._image = _image + z._image;
    return tmp;
}

Complex Complex::operator-(Complex& z)
{
    Complex tmp(*this);
    tmp._real = _real - z._real;
    tmp._image = _image - z._image;
    return tmp;
}

Complex& Complex::operator++()
{
    _real++;
    _image++;
    return *this;
}

Complex Complex:: operator++(int)
{
    Complex tmp(*this);
    _real++;
    _image++;
    return tmp;
}

bool Complex:: operator==(Complex&z)
{
    if (!z._image && !_image)
    {
        if (_real == _image)
        {
            return true;
        }
    }
    else if (_image == z._image)
    {
        if (_real == z._real)
        {
            return true;
        }
    }
    return false;
}

bool Complex::operator<(Complex&z)
{
    if ((!_image) && (!z._image))
    {
        if (_real < z._real)
        {
            return true;
        }
    }
    return false;
}


bool Complex::operator<=(Complex&z)
{
    if ((!_image) && (!z._image))
    {
        if (_real < z._real)
        {
            return true;
        }
    }
    else if (_real == z._real)
    {
        if (_image == z._image)
            return true;
    }
    return false;
}


bool Complex::operator>(Complex&z)
{
    if ((!_image) && (!z._image))
    {
        if (_real > z._real)
        {
            return true;
        }
    }
    return false;
}


bool Complex::operator>=(Complex&z)
{
    if ((!_image) && (!z._image))
    {
        if (_real > z._real)
        {
            return true;
        }
    }
    else if (_real == z._real)
    {
        if (_image == z._image)
        {
            return true;
        }
    }
    return false;
}

bool Complex::operator!=(Complex&z)
{
    if (_real != z._real)
    {
        if (_image != z._image)
        {
            return true;
        }
    }
    return false;
}

//打印函数
void Complex::display()
{
    cout << _real << "+" << _image << "i" << endl;
}

测试函数:

#define _CRT_SECURE_NO_WARNINGS 1
#include"complex.h"


void test2()
{
    Complex z1(2.0, 3.0);
    Complex z2(3.0, 4.0);
    Complex z3(z2);
    if (z1 == z2)
    {
        cout << "z1=z2" << endl;
    }
    if (z2 == z3)
    {
        cout << "z2=z3" << endl;
    }
    if (z2 > z3)
    {
        cout << "z2>z3" << endl;
    }
    if (z2 > z3)
    {
        cout << "z2>z3" << endl;
    }
    if (z2 < z3)
    {
        cout << "z2<z3" << endl;
    }
    if (z2 <= z3)
    {
        cout << "z2<=z3" << endl;
    }
    if (z2 >= z3)
    {
        cout << "z2>=z3" << endl;
    }
}

//测试用例打印函数
void test1()
{
    Complex z1(2.0, 3.0);
    Complex z2 = z1;
    Complex z3 = z2 + z1;
    Complex z4 = z3 - z2;
    ++z4;
    z1++;
    z1.display();
    z2.display();
    z3.display();
    z4.display();
}
int main()
{

    //test1();
    test2();
    system("pause");
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值