复数类 class complex

精华版

#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;//数据应尽量private

  friend 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;//数据应尽量private

  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);//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__




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赤城封雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值