#include<iostream> using namespace std; class Complex { public: Complex(double real = 0.0, double p_w_picpath = 0.0) { this->_real = real; _p_w_picpath = p_w_picpath; } Complex(const Complex &c) { _real = c._real; _p_w_picpath = c._p_w_picpath; } Complex& operator=(const Complex &c) { _real = c._real; _p_w_picpath = c._p_w_picpath; return *this; } bool operator>(const Complex &c) { double tmp1,tmp2; tmp1 = sqrt(pow(_real, 2) + pow(_p_w_picpath, 2)); tmp2 = sqrt(pow(c._real, 2) + pow(c._p_w_picpath, 2)); return tmp1 > tmp2; } bool operator==(const Complex &c) { return sqrt(pow(_real, 2) + pow(_p_w_picpath, 2)) == sqrt(pow(c._real, 2) + pow(c._p_w_picpath, 2)); } bool operator>=(const Complex &c) { return ((*this > c) || (*this == c)); } bool operator<(const Complex &c) { return sqrt(pow(_real, 2) + pow(_p_w_picpath, 2)) < sqrt(pow(c._real, 2) + pow(c._p_w_picpath, 2)); } bool operator<=(const Complex &c) { return (*this < c) || (*this == c); } Complex operator+(const Complex &c) { Complex add; add._real = _real + c._real; add._p_w_picpath = _p_w_picpath + c._p_w_picpath; return add; } Complex operator-(const Complex &c) { Complex sub; sub._real = _real - c._real; sub._p_w_picpath = _p_w_picpath - c._p_w_picpath; return sub; } Complex operator*(const Complex &c)const//****** { Complex mul; mul._real = _real*c._real - _p_w_picpath*c._p_w_picpath; mul._p_w_picpath = _real*c._p_w_picpath + _p_w_picpath*c._real; return mul; } Complex operator/(const Complex &c)****** { Complex div; Complex c1; c1._real = c._real; c1._p_w_picpath = (-1)*c._p_w_picpath; const Complex c2 = c*c1;//********************易错 div._real = ((*this*c1)._real) / (c2._real); div._p_w_picpath = ((*this*c1)._p_w_picpath) / (c2._real); return div; } Complex operator+=(const Complex &c) { _real = _real + c._real; _p_w_picpath = _p_w_picpath + c._p_w_picpath; return *this; } Complex operator-=(const Complex &c) { _real = _real - c._real; _p_w_picpath = _p_w_picpath - c._p_w_picpath; return *this; } Complex operator++(int)//后置 { Complex tmp(*this); _real++; _p_w_picpath++; return tmp; } Complex operator++()//前置 { _real++; _p_w_picpath++; return *this; } Complex operator--(int)//后置 { Complex tmp(*this); _real--; _p_w_picpath--; return tmp; } Complex operator--()//前置 { _real--; _p_w_picpath--; return *this; } void Display() { if (_real != 0) { if (_p_w_picpath>0) cout << "Complex=" << _real << "+i" << _p_w_picpath << endl; else if (_p_w_picpath < 0) cout << "Complex=" << _real << "-i" << (_p_w_picpath)*(-1) << endl; else cout << "Complex=" << _real << endl; } else { if (_p_w_picpath>0) cout << "Complex=" << "i" << _p_w_picpath << endl; else if (_p_w_picpath < 0) cout << "Complex=" << "-i" << (_p_w_picpath)*(-1) << endl; else cout << "Complex=" << _real << endl; } } ~Complex() { } private: double _real; double _p_w_picpath; }; void Test() { Complex c1(2,1); c1.Display(); //Complex c2(c1); Complex c2=c1; c2.Display(); Complex c3,c4; c4=c3 = c2; c3.Display(); c4.Display(); Complex c5(1,2); c5.Display(); bool ret1 = c1 > c5; cout << "> "<<ret1 << endl; bool ret2 = c1 == c5; cout << "== " << ret2 << endl; bool ret3 = c1 >= c5; cout << ">= " << ret3 << endl; bool ret4 = c1 < c5; cout << "< " << ret4 << endl; bool ret5 = c1 <= c5; cout << "<= " << ret5 << endl; Complex add = c1 + c5; add.Display(); Complex sub = c1 - c5; sub.Display(); Complex mul = c1 * c5; mul.Display(); Complex div = c1 / c5; div.Display(); (c1 += c5).Display(); (c1 -= c5).Display(); (c1++).Display(); (++c5).Display(); (c1--).Display(); (--c5).Display(); } int main() { Test(); system("pause"); return 0; }
实现一个复数类
原创红玲chehlling 博主文章分类:C/C++/数据结构 ©著作权
©著作权归作者所有:来自51CTO博客作者红玲chehlling的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:调整数组使奇数全部都位于偶数前面
下一篇:实现一个日期类
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
设计复数类java 设计一个复数类
问题描述:设计一个复数类,要求 (1)在复数内部用双精度浮点数定义其实部和虚部 (2)实现3个构造函数;第1个构造函数没有参数;  
设计复数类java 成员变量 构造函数 构造方法