complex.h尽可能注释

本文详细探讨了C++标准库中的`<complex>`头文件,它提供了复数类模板,用于数学计算。内容包括复数的定义、基本操作、以及如何在实际项目中有效使用复数类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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__


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值