C++面向对象编程<四>:类comlpex的完整代码
2016-02-21 23:31
537 查看
这里给出完整代码
下面是测试文件
#ifndef __MYCOMPLEX__ #define __MYCOMPLEX__ class complex; complex& __doapl (complex* ths, const complex& r); complex& __doami (complex* ths, const complex& r); complex& __doaml (complex* ths, const complex& r); class complex { public: complex (double r = 0, double i = 0): re (r), im (i) { } complex& operator += (const complex&); complex& operator -= (const complex&); complex& operator *= (const complex&); complex& operator /= (const complex&); double real () const { return re; } double imag () const { return im; } private: double re, im; friend complex& __doapl (complex *, const complex&); friend complex& __doami (complex *, const complex&); friend complex& __doaml (complex *, const complex&); }; inline complex& __doapl (complex* ths, const complex& r) { ths->re += r.re; ths->im += r.im; return *ths; } inline complex& complex::operator += (const complex& r) { return __doapl (this, r); } inline complex& __doami (complex* ths, const complex& r) { ths->re -= r.re; ths->im -= r.im; return *ths; } inline complex& complex::operator -= (const complex& r) { return __doami (this, r); } inline complex& __doaml (complex* ths, const complex& r) { double f = ths->re * r.re - ths->im * r.im; ths->im = ths->re * r.im + ths->im * r.re; ths->re = f; return *ths; } inline complex& complex::operator *= (const complex& r) { return __doaml (this, r); } inline double imag (const complex& x) { return x.imag (); } inline double real (const complex& x) { return x.real (); } inline complex operator + (const complex& x, const complex& y) { return complex (real (x) + real (y), imag (x) + imag (y)); } inline complex operator + (const complex& x, double y) { return complex (real (x) + y, imag (x)); } inline complex operator + (double x, const complex& y) { return complex (x + real (y), imag (y)); } inline complex operator - (const complex& x, const complex& y) { return complex (real (x) - real (y), imag (x) - imag (y)); } inline complex operator - (const complex& x, double y) { return complex (real (x) - y, imag (x)); } inline complex operator - (double x, const complex& y) { return complex (x - real (y), - imag (y)); } inline complex operator * (const complex& x, const complex& y) { return complex (real (x) * real (y) - imag (x) * imag (y), real (x) * imag (y) + imag (x) * real (y)); } inline complex operator * (const complex& x, double y) { return complex (real (x) * y, imag (x) * y); } inline complex operator * (double x, const complex& y) { return complex (x * real (y), x * imag (y)); } complex operator / (const complex& x, double y) { return complex (real (x) / y, imag (x) / y); } inline complex operator + (const complex& x) { return x; } inline complex operator - (const complex& x) { return complex (-real (x), -imag (x)); } inline bool operator == (const complex& x, const complex& y) { return real (x) == real (y) && imag (x) == imag (y); } inline bool operator == (const complex& x, double y) { return real (x) == y && imag (x) == 0; } inline bool operator == (double x, const complex& y) { return x == real (y) && imag (y) == 0; } inline bool operator != (const complex& x, const complex& y) { return real (x) != real (y) || imag (x) != imag (y); } inline bool operator != (const complex& x, double y) { return real (x) != y || imag (x) != 0; } inline bool operator != (double x, const complex& y) { return x != real (y) || imag (y) != 0; } #include <cmath> inline complex polar (double r, double t) { return complex (r * cos (t), r * sin (t)); } inline complex conj (const complex& x) { return complex (real (x), -imag (x)); } inline double norm (const complex& x) { return real (x) * real (x) + imag (x) * imag (x); } #endif //__MYCOMPLEX__
下面是测试文件
#include <iostream> #include "complex.h" using namespace std; ostream& operator << (ostream& os, const complex& x) { return os << '(' << real (x) << ',' << imag (x) << ')'; } int main() { complex c1(2, 1); complex c2(4, 0); cout << c1 << endl; cout << c2 << endl; cout << c1+c2 << endl; cout << c1-c2 << endl; cout << c1*c2 << endl; cout << c1 / 2 << endl; cout << conj(c1) << endl; cout << norm(c1) << endl; cout << polar(10,4) << endl; cout << (c1 += c2) << endl; cout << (c1 == c2) << endl; cout << (c1 != c2) << endl; cout << +c2 << endl; cout << -c2 << endl; cout << (c2 - 2) << endl; cout << (5 + c2) << endl; return 0; }
相关文章推荐
- neuq oj 1052: 谭浩强C语言(第三版)习题6.8 C语言
- C语言运算符优先级详细列表
- C++面向对象编程<三>:操作符重载
- C++面向对象编程<二>:构造函数
- c中的数据类型
- C语言中柔性数组解析
- c新手入门
- 冒泡排序(C++版)
- 快排算法(C++版)
- c语言入门之项目3.0——求和的和
- 内存分配学习(一)-- 实现一个malloc
- 二叉树的遍历(C++)版
- c++编程里面endl与\n的区别,告诉你什么时候用二者最适合
- C++设计模式——单例模式
- C++中关于指针的理解(一点一点完善中)
- 希尔排序(C++版)
- 简单选择排序算法(C++版)
- 简单插入排序(C++版)
- 适配器模式
- 《C++Primer》 学习——第三章