STL解析:complex

#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;
	//plus miuns mlutiply 定义为友元函数,
	friend complex& __doapl(complex *, const complex&);
	friend complex& __doami(complex *, const complex&);
	friend complex& __doaml(complex *, const complex&);
};

//定义plus为内连函数,返回类型为引用,return *this返回内存的引用。
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));
}

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);
}

ostream&
operator << (ostream& os, const complex& x)
{
	return os << '(' << real(x) << ',' << imag(x) << ')';
}

#endif   //__MYCOMPLEX__

学习知识点

  1. 使用函数访问数据。real(x)
  2. 不能返回引用,就采用临时变量。return complex(real(x), -imag(x));
  3. ostream& 我们尽量使用引用的方式传,如果不希望被修改就使用const
    返回类型,思考return by references 还是 return by value.
    例如我们返回os,os是原来就有的东西,所以我们返回引用
    如果是一个在函数里面生成的空间,那么最后返回最后是值
    例如下面重载,我们需要考虑用户可能的cout << a << b;的多重输出。
    因此需要返回ostream,每个地方多为用户想一点
  4. 输入值,尽量使用引用,不用拷贝值。节约时间。complex& x

参考博客:
https://www.cnblogs.com/henuzyx/p/9107842.html
https://www.cnblogs.com/Q1143316492/p/10385461.html

发布了46 篇原创文章 · 获赞 0 · 访问量 837

猜你喜欢

转载自blog.csdn.net/github_38148039/article/details/103819410
STL