C++面向对象编程<四>:类comlpex的完整代码
来源:互联网 发布:软件版本升级说明 编辑:程序博客网 时间:2024/05/22 04:31
这里给出完整代码
#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 doubleimag (const complex& x){ return x.imag ();}inline doublereal (const complex& x){ return x.real ();}inline complexoperator + (const complex& x, const complex& y){ return complex (real (x) + real (y), imag (x) + imag (y));}inline complexoperator + (const complex& x, double y){ return complex (real (x) + y, imag (x));}inline complexoperator + (double x, const complex& y){ return complex (x + real (y), imag (y));}inline complexoperator - (const complex& x, const complex& y){ return complex (real (x) - real (y), imag (x) - imag (y));}inline complexoperator - (const complex& x, double y){ return complex (real (x) - y, imag (x));}inline complexoperator - (double x, const complex& y){ return complex (x - real (y), - imag (y));}inline complexoperator * (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 complexoperator * (const complex& x, double y){ return complex (real (x) * y, imag (x) * y);}inline complexoperator * (double x, const complex& y){ return complex (x * real (y), x * imag (y));}complexoperator / (const complex& x, double y){ return complex (real (x) / y, imag (x) / y);}inline complexoperator + (const complex& x){ return x;}inline complexoperator - (const complex& x){ return complex (-real (x), -imag (x));}inline booloperator == (const complex& x, const complex& y){ return real (x) == real (y) && imag (x) == imag (y);}inline booloperator == (const complex& x, double y){ return real (x) == y && imag (x) == 0;}inline booloperator == (double x, const complex& y){ return x == real (y) && imag (y) == 0;}inline booloperator != (const complex& x, const complex& y){ return real (x) != real (y) || imag (x) != imag (y);}inline booloperator != (const complex& x, double y){ return real (x) != y || imag (x) != 0;}inline booloperator != (double x, const complex& y){ return x != real (y) || imag (y) != 0;}#include <cmath>inline complexpolar (double r, double t){ return complex (r * cos (t), r * sin (t));}inline complexconj (const complex& x) { return complex (real (x), -imag (x));}inline doublenorm (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;}
1 0
- C++面向对象编程<四>:类comlpex的完整代码
- C语言面向对象编程(四):面向接口编程
- C语言面向对象编程(四):面向接口编程
- C语言面向对象编程(四):面向接口编程
- C语言面向对象编程之四:面向接口编程
- C的面向对象编程
- 【c#】 面向对象的编程
- js面向对象编程,定义类的四种方法
- js面向对象编程,一个完整的继承例子
- js面向对象编程,一个完整原型的继承例子
- js面向对象编程,一个具有各种方法,字段 完整的类
- PHP面向对象编程(四)
- (四)面向对象编程
- 面向对象的C语言编程
- 基于Objective-C的面向对象编程
- C语言的面向对象编程
- Objective-C 面向对象的编程语言
- 基于C语言的面向对象编程
- 成员方法
- 用canvas 实现《Matrix》(黑客帝国)中 电脑屏幕效果
- xampp新建数据库+数据库的导入导出
- 一道常被人轻视的前端JS面试题
- 《Android源码设计模式》读书笔记 (17) 第17章 中介者模式
- C++面向对象编程<四>:类comlpex的完整代码
- 有一个14位数。由 2个1 2个2 2个3 2个4 2个5 2个6 2个7组成!其中,两个1中有一个数字,2个2中有2个数字……2个7中有7个数字入:73625324765141
- POJ235 The Embarrassed Cryptographer
- Linux驱动开发错误:module license 'unspecified' taints kernel.
- Java Notes-14(DateAtHost Client, TinyHttpd Server, Socket Options)
- HDU 1056 HangOver
- swift基本语法(总结提炼版)之002逻辑分支
- 51nod 1405 树的距离之和 (两次dfs,树形dp)
- MapReduce的运行原理