博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
复数类
阅读量:5820 次
发布时间:2019-06-18

本文共 3096 字,大约阅读时间需要 10 分钟。

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>

using namespace std;

class Complex

{

public:

void display( )

{

/*cout << "real:" << this->_real << endl;

cout << "image:" << this->_image << endl;*/

cout << this->_real << "+" << this->_image << "i" << endl;

}

Complex operator+(const Complex& a)

{

Complex tem;

tem._real = this->_real + a._real;

tem._image = this->_image + a._image;

return tem;

}

Complex operator-(const Complex& a)

{

Complex tem;

tem._real = this->_real - a._real;

tem._image = this->_image - a._image;

return tem;

}

Complex operator*(const Complex& a)

{

Complex tem;

tem._real = this->_real * a._real;

tem._image = this->_image * a._image;

return tem;

}

Complex operator/(const Complex& a)

{

Complex tem;

tem._real = this->_real / a._real;

tem._image = this->_image / a._image;

return tem;

}

void operator+=(const Complex& a)

{

this->_real = this->_real + a._real;

this->_image = this->_image + a._image;

}

void operator-=(const Complex& a)

{

this->_real = this->_real - a._real;

this->_image = this->_image - a._image;

}

void operator*=(const Complex& a)

{

this->_real = this->_real * a._real;

this->_image = this->_image * a._image;

}

void operator/=(const Complex& a)

{

this->_real = this->_real / a._real;

this->_image = this->_image / a._image;

}

Complex operator++(int)//huozhi

{

Complex tem(*this);

this->_real += 1;

this->_image += 1;

return tem;

}

Complex& operator++()

{

this->_real += 1;

this->_image += 1;

return *this;

}

Complex operator--(int)//huozhi

{

Complex tem(*this);

this->_real -= 1;

this->_image -= 1;

return tem;

}

Complex& operator--()

{

this->_real -= 1;

this->_image -= 1;

return *this;

}

bool operator>(const Complex& a)

{

double m = pow(this->_real, 2) + pow(this->_image, 2);

double n = pow(a._real, 2) + pow(a._image, 2);

return (m > n);

}

bool operator<(const Complex& a)

{

double m = pow(this->_real, 2) + pow(this->_image, 2);

double n = pow(a._real, 2) + pow(a._image, 2);

return (m < n);

}

bool operator>=(const Complex& a)

{

double m = pow(this->_real, 2) + pow(this->_image, 2);

double n = pow(a._real, 2) + pow(a._image, 2);

return (m > n || m == n);

}

bool operator<=(const Complex& a)

{

double m = pow(this->_real, 2) + pow(this->_image, 2);

double n = pow(a._real, 2) + pow(a._image, 2);

return (m < n || m == n);

}

bool operator==(const Complex& a)

{

double m = pow(this->_real, 2) + pow(this->_image, 2);

double n = pow(a._real, 2) + pow(a._image, 2);

return (m == n);

}

public:

Complex(double real = 0.0, double image = 0.0)

{

this->_real = real;

this->_image = image;

}

Complex(Complex& a)

{

this->_real = a._real;

this->_image = a._image;

}

~Complex()

{

;

}

Complex* operator=(const Complex& a)

{

this->_real = a._real;

this->_image = a._image;

return this;

}

private:

double _real;

double _image;

};

int main()

{

Complex first(1.0, 2.0);

first.display();

Complex second(first);

second.display();

Complex three;

//three = second = first;

//second.display();

//three = first + second;

first.operator++(1);

first.display();

system("pause");

return 0;

}

本文转自 ye小灰灰  51CTO博客,原文链接:http://blog.51cto.com/10704527/1719280,如需转载请自行联系原作者
你可能感兴趣的文章
Gradle之module间依赖版本同步
查看>>
java springcloud版b2b2c社交电商spring cloud分布式微服务(十五)Springboot整合RabbitMQ...
查看>>
d3 v4实现饼状图,折线标注
查看>>
微软的云策略
查看>>
Valid Parentheses
查看>>
AIX 配置vncserver
查看>>
windows下Python 3.x图形图像处理库PIL的安装
查看>>
【IL】IL生成exe的方法
查看>>
SettingsNotePad++
查看>>
没有JS的前端:体积更小、速度更快!
查看>>
数据指标/表现度量系统(Performance Measurement System)综述
查看>>
GitHub宣布推出Electron 1.0和Devtron,并将提供无限制的私有代码库
查看>>
论模式在领域驱动设计中的重要性
查看>>
有关GitHub仓库分支的几个问题
查看>>
云原生的浪潮下,为什么运维人员适合学习Go语言?
查看>>
EAServer 6.1 .NET Client Support
查看>>
锐捷交换机密码恢复(1)
查看>>
Method Swizzling对Method的要求
查看>>
佛祖保佑,永不宕机
查看>>
四、配置开机自动启动Nginx + PHP【LNMP安装 】
查看>>