complex.h尽可能注释

complex.h尽可能注释#ifndef__MYCOMPLEX__ //安全宏#define__MYCOMPLEX__classcomplex; //声明类inlinecomplex& //自己加上inline,可行__doapl(complex*ths,constcomplex&r);/* comples& //声明一个函数,返回一个compl

#ifndef __MYCOMPLEX__							//安全宏
#define __MYCOMPLEX__

class complex;									//声明类
<span style="color:#ff0000;">inline complex&									//自己加上inline,可行
__doapl (complex* ths, const complex& r);
/*
	comples&	//声明一个函数,返回一个 complex 引用
	__doapl (complex* ths, const complex& r);	//传入参数为(complex 指针,complex 常量引用)
	但不知道为什么要换行,难道是为了显示条理?
*/</span>
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;
 <span style="color:#ff0000;">inline friend complex& __doapl (complex *, const complex&);			//声明友元函数
  friend complex& __doami (complex *, const complex&);			//complex& __doami(。。。)可以直接访问类中的私有数据
  friend complex& __doaml (complex *, const complex&);			//不知道为什么要将其私有
};
//在类中的友元函数声明,与全局函数声明,哪一个才是 complex& __doapl (complex *, const complex&);的真正声明?
</span>
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));			//临时对象,不可用引用返回,只能return by value
}

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__

今天的文章complex.h尽可能注释分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/26581.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注