首页 > 其他 > 详细

复数类

时间:2015-12-03 17:13:07      阅读:321      评论:0      收藏:0      [点我收藏+]

#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;

}


本文出自 “水仙花” 博客,请务必保留此出处http://10704527.blog.51cto.com/10694527/1719280

复数类

原文:http://10704527.blog.51cto.com/10694527/1719280

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!