精华版

#ifndef __MYCOMPLEX__  // 防卫式声明
#define __MYCOMPLEX__class complex;
complex&__doapl (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&);//&operator返回地址complex& operator *= (const complex&);complex& operator /= (const complex&);double real () const { return re; }//数据接口(封装)double imag () const { return im; }//常成员函数 "()const{}"
private://私有成员double re, im;//数据应尽量privatefriend complex& __doapl (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);//this指针指向自身对象
}inline double
imag (const complex& x)//访问私有数据成员
{return x.imag ();
}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)//重载单目运算符 +
{return x;
}inline bool
operator == (const complex& x, const complex& y)//重载运算符 ==
{return real (x) == real (y) && imag (x) == imag (y);
}

完整版

#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&);//&operator返回地址complex& operator *= (const complex&);complex& operator /= (const complex&);double real () const { return re; }//数据接口(封装)double imag () const { return im; }//常成员函数 "()const{}"
private://私有成员double re, im;//数据应尽量privatefriend 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);//this指针指向自身对象
}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__

复数类 class complex相关推荐

  1. 复数类:class Complex

    #define _CRT_SECURE_NO_WARNINGS 1#include<iostream> using namespace std;class Complex { public ...

  2. 使用c++实现复数类的运算

    复数的运算 一.前言 二.复数的运算 1.头文件(Complex.h) 2.源文件(实现函数) 3.源文件(测试函数) 4.运行截图 三.总结 一.前言 为了更深入的学习c++,小编将带领大家一起使用 ...

  3. 【c++】实现一个复数类

    // 实现一个复数类(complex) //要求:1:实现不带参 / 带参数 /带默认参数的几种构造函数. // 2:实现拷贝构造 / 析构函数.#include <iostream> u ...

  4. 定义一个复数类Complex,重载运算符+

    定义一个复数类Complex,重载运算符"+".使之能用于复数的加法运算,将运算符函数重载为非成员.非友员的普通函数.编写程序求2个复数之和. #include<iostre ...

  5. C#复数类Complex的封装

    C#复数类Complex的封装 ------------------------------------------------------------------------------------ ...

  6. 定义一个复数类Complex,重载运算符“+”,

    定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算.参加运算的两个运算符可以都是类对象.也可以其中一个是整数,顺序任意.例如:c1+c2,i+c1,c1+i ...

  7. 1、定义一个复数类Complex,使得下面的代码能够工作。 Complex c1(3,5); Complex c2=4.5; c1.add(c2); c1.show();

    一.问题描述 1.定义一个复数类Complex,使得下面的代码能够工作. Complex c1(3,5); Complex c2=4.5; c1.add(c2); c1.show(); #includ ...

  8. 建立一个复数类Complex,其私有数据成员mX和mY表示复数的实部和虚部,构造函数Complex用于对复数的实部和虚部初始化

    建立一个复数类Complex,其私有数据成员mX和mY表示复数的实部和虚部,构造函数Complex用于对复数的实部和虚部初始化,友员函数Add,Sub,Mul和Div分别用于进行复数的加.减.乘和除法 ...

  9. Java练习题 类 编写一个程序,使用复数类Complex验证两个复数 1+2i 和3+4i 相加产生一个新的复数 4+6i 。

    编写一个程序,使用复数类Complex验证两个复数 1+2i 和3+4i 相加产生一个新的复数 4+6i . 复数类Complex必须满足如下要求: (1) 复数类Complex 的属性有: real ...

最新文章

  1. 计算机上配置的网卡 实质上是,()计算机上配置的网卡,实质上是_ ___。
  2. linux内核杂记(3)-进程(1)
  3. h5日期选择控件_成都h5开发工程师培训_H5入门需要知道的知识
  4. 从IC设计来看Trace32的用途
  5. A Data Access Layer to persist business objects using attributes and reflection - Part III [无常译]...
  6. UI设计中的弹窗设计素材,技巧快get起来
  7. 优雅的实现Activiti动态调整流程(自由跳转、前进、后退、分裂、前加签、后加签等),含范例代码!...
  8. miui8.2 是android 7.0,因与MIUI 8.2撞车 小米5暂缺失安卓7.0
  9. Kinect 2.0 高帧率 同分辨率采集RGB-D图像并保存,并显示人体骨架
  10. 安装虚拟机报错 This kernel requires an X86-64 CPU,but only detected an i686 CPU
  11. android dff播放器,无损音乐解码播放器
  12. VS C++项目报错warning C4199: ……use /Zc:twoPhase-
  13. 搜索引擎优化的一些注意事项
  14. 微信小程序开发报错及解决记录
  15. 羽毛球击球点与自身位置[羽球技术入门必读]
  16. 视频教程-HoloLens与ARcore第三视角开发-其他
  17. PMM使用Grafana告警
  18. 飞机下滑波束导引系统设计
  19. 天龙八部武夷千年冰魄
  20. 薛开宇caffe学习笔记的补充笔记3

热门文章

  1. java jfm入门_image Java版*图像过滤入门示例及源码模拟绿坝过滤机制 - 下载 - 搜珍网...
  2. JS 限制文本框只能输入数字
  3. 应用协议 tcp udp
  4. 建筑八大员培训湖北标准员培训工程施工现场标准员的工作导则
  5. date java 格式化 sss_java DateFormat 格式化格式参考
  6. 2019-05-22 SSS扫描器;SSS扫描器指南;
  7. 二分图(Bipartite Graph)
  8. terminatethread导致内存泄露
  9. 理论力学专题:守恒量的证明
  10. 顺序栈实现将十进制整数转换为r(2、8、16)进制数