📄 complex
字号:
_Val[_RE] = _Right;
_Val[_IM] = 0;
return (*this);
}
_Myt& operator+=(const _Ty& _Right)
{ // add real
_Val[_RE] = _Val[_RE] + _Right;
return (*this);
}
_Myt& operator-=(const _Ty& _Right)
{ // subtract real
_Val[_RE] = _Val[_RE] - _Right;
return (*this);
}
_Myt& operator*=(const _Ty& _Right)
{ // multiply by real
_Val[_RE] = _Val[_RE] * _Right;
_Val[_IM] = _Val[_IM] * _Right;
return (*this);
}
_Myt& operator/=(const _Ty& _Right)
{ // divide by real
_Val[_RE] = _Val[_RE] / _Right;
_Val[_IM] = _Val[_IM] / _Right;
return (*this);
}
_Myt& operator+=(const _Myt& _Right)
{ // add other complex
this->_Add(_Right);
return (*this);
}
_Myt& operator-=(const _Myt& _Right)
{ // subtract other complex
this->_Sub(_Right);
return (*this);
}
_Myt& operator*=(const _Myt& _Right)
{ // multiply by other complex
this->_Mul(_Right);
return (*this);
}
_Myt& operator/=(const _Myt& _Right)
{ // divide by other complex
this->_Div(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator=(const complex<_Other>& _Right)
{ // assign another complex
_Val[_RE] = (_Ty)_Right._Val[_RE];
_Val[_IM] = (_Ty)_Right._Val[_IM];
return (*this);
}
template<class _Other> inline
_Myt& operator+=(const complex<_Other>& _Right)
{ // add other complex
this->_Add(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator-=(const complex<_Other>& _Right)
{ // subtract other complex
this->_Sub(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator*=(const complex<_Other>& _Right)
{ // multiply by other complex
this->_Mul(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator/=(const complex<_Other>& _Right)
{ // divide by other complex
this->_Div(_Right);
return (*this);
}
};
// CLASS complex<double>
template<> class complex<double>
: public _Complex_base<double, _Dcomplex_value>
{ // complex with double components
public:
typedef double _Ty;
typedef complex<_Ty> _Myt;
complex(
const complex<float>&); // defined below
explicit complex(
const complex<long double>&); // defined below
complex(const _Ty& _Realval = 0,
const _Ty& _Imagval = 0)
: _Complex_base<double, _Dcomplex_value>(_Realval, _Imagval)
{ // construct from double components
}
complex(const _Dcomplex_value& _Right)
: _Complex_base<double, _Dcomplex_value>(_Right._Val[_RE],
_Right._Val[_IM])
{ // construct from double complex value
}
complex(const _Lcomplex_value& _Right)
: _Complex_base<double, _Dcomplex_value>(_Right._Val[_RE],
_Right._Val[_IM])
{ // construct from long double complex value
}
complex<_Ty>& operator=(const _Ty& _Right)
{ // assign real
_Val[_RE] = _Right;
_Val[_IM] = 0;
return (*this);
}
_Myt& operator+=(const _Ty& _Right)
{ // add real
_Val[_RE] = _Val[_RE] + _Right;
return (*this);
}
_Myt& operator-=(const _Ty& _Right)
{ // subtract real
_Val[_RE] = _Val[_RE] - _Right;
return (*this);
}
_Myt& operator*=(const _Ty& _Right)
{ // multiply by real
_Val[_RE] = _Val[_RE] * _Right;
_Val[_IM] = _Val[_IM] * _Right;
return (*this);
}
_Myt& operator/=(const _Ty& _Right)
{ // divide by real
_Val[_RE] = _Val[_RE] / _Right;
_Val[_IM] = _Val[_IM] / _Right;
return (*this);
}
_Myt& operator+=(const _Myt& _Right)
{ // add other complex
this->_Add(_Right);
return (*this);
}
_Myt& operator-=(const _Myt& _Right)
{ // subtract other complex
this->_Sub(_Right);
return (*this);
}
_Myt& operator*=(const _Myt& _Right)
{ // multiply by other complex
this->_Mul(_Right);
return (*this);
}
_Myt& operator/=(const _Myt& _Right)
{ // divide by other complex
this->_Div(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator=(const complex<_Other>& _Right)
{ // assign another complex
_Val[_RE] = (_Ty)_Right._Val[_RE];
_Val[_IM] = (_Ty)_Right._Val[_IM];
return (*this);
}
template<class _Other> inline
_Myt& operator+=(const complex<_Other>& _Right)
{ // add other complex
this->_Add(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator-=(const complex<_Other>& _Right)
{ // subtract other complex
this->_Sub(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator*=(const complex<_Other>& _Right)
{ // multiply by other complex
this->_Mul(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator/=(const complex<_Other>& _Right)
{ // divide by other complex
this->_Div(_Right);
return (*this);
}
};
// CLASS complex<long double>
template<> class complex<long double>
: public _Complex_base<long double, _Lcomplex_value>
{ // complex with long double components
public:
typedef long double _Ty;
typedef complex<_Ty> _Myt;
complex(
const complex<float>&); // defined below
complex(
const complex<double>&); // defined below
complex(const _Ty& _Realval = 0,
const _Ty& _Imagval = 0)
: _Complex_base<long double, _Lcomplex_value>(_Realval, _Imagval)
{ // construct from long double components
}
complex(const _Lcomplex_value& _Right)
: _Complex_base<long double, _Lcomplex_value>(_Right._Val[_RE],
_Right._Val[_IM])
{ // construct from long double complex value
}
complex<_Ty>& operator=(const _Ty& _Right)
{ // assign real
_Val[_RE] = _Right;
_Val[_IM] = 0;
return (*this);
}
_Myt& operator+=(const _Ty& _Right)
{ // add real
_Val[_RE] = _Val[_RE] + _Right;
return (*this);
}
_Myt& operator-=(const _Ty& _Right)
{ // subtract real
_Val[_RE] = _Val[_RE] - _Right;
return (*this);
}
_Myt& operator*=(const _Ty& _Right)
{ // multiply by real
_Val[_RE] = _Val[_RE] * _Right;
_Val[_IM] = _Val[_IM] * _Right;
return (*this);
}
_Myt& operator/=(const _Ty& _Right)
{ // divide by real
_Val[_RE] = _Val[_RE] / _Right;
_Val[_IM] = _Val[_IM] / _Right;
return (*this);
}
_Myt& operator+=(const _Myt& _Right)
{ // add other complex
this->_Add(_Right);
return (*this);
}
_Myt& operator-=(const _Myt& _Right)
{ // subtract other complex
this->_Sub(_Right);
return (*this);
}
_Myt& operator*=(const _Myt& _Right)
{ // multiply by other complex
this->_Mul(_Right);
return (*this);
}
_Myt& operator/=(const _Myt& _Right)
{ // divide by other complex
this->_Div(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator=(const complex<_Other>& _Right)
{ // assign another complex
_Val[_RE] = (_Ty)_Right._Val[_RE];
_Val[_IM] = (_Ty)_Right._Val[_IM];
return (*this);
}
template<class _Other> inline
_Myt& operator+=(const complex<_Other>& _Right)
{ // add other complex
this->_Add(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator-=(const complex<_Other>& _Right)
{ // subtract other complex
this->_Sub(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator*=(const complex<_Other>& _Right)
{ // multiply by other complex
this->_Mul(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator/=(const complex<_Other>& _Right)
{ // divide by other complex
this->_Div(_Right);
return (*this);
}
};
// CONSTRUCTORS FOR complex SPECIALIZATIONS
inline complex<float>::complex(
const complex<double>& _Right)
: _Complex_base<float, _Fcomplex_value>(
(_Ty)_Right.real(), (_Ty)_Right.imag())
{ // construct complex<float> from complex<double>
}
inline complex<float>::complex(
const complex<long double>& _Right)
: _Complex_base<float, _Fcomplex_value>(
(_Ty)_Right.real(), (_Ty)_Right.imag())
{ // construct complex<float> from complex<long double>
}
inline complex<double>::complex(
const complex<float>& _Right)
: _Complex_base<double, _Dcomplex_value>(
(_Ty)_Right.real(), (_Ty)_Right.imag())
{ // construct complex<double> from complex<float>
}
inline complex<double>::complex(
const complex<long double>& _Right)
: _Complex_base<double, _Dcomplex_value>(
(_Ty)_Right.real(), (_Ty)_Right.imag())
{ // construct complex<double> from complex<long double>
}
inline complex<long double>::complex(
const complex<float>& _Right)
: _Complex_base<long double, _Lcomplex_value>(
(_Ty)_Right.real(), (_Ty)_Right.imag())
{ // construct complex<long double> from complex<float>
}
inline complex<long double>::complex(
const complex<double>& _Right)
: _Complex_base<long double, _Lcomplex_value>(
(_Ty)_Right.real(), (_Ty)_Right.imag())
{ // construct complex<long double> from complex<double>
}
// TEMPLATE CLASS complex
template<class _Ty>
class complex
: public _Complex_base<_Ty, _Complex_value<_Ty> >
{ // complex with _Ty components
public:
typedef complex<_Ty> _Myt;
typedef _Complex_base<_Ty, _Complex_value<_Ty> > _Mybase;
complex(const _Ty& _Realval = _Ty(), const _Ty& _Imagval = _Ty())
: _Mybase(_Realval, _Imagval)
{ // construct from components of same type
}
_Myt& operator=(const _Ty& _Right)
{ // assign real
this->_Val[_RE] = _Right;
this->_Val[_IM] = _Ty();
return (*this);
}
template<class _Other>
complex(const complex<_Other>& _Right)
: _Mybase((_Ty)_Right.real(), (_Ty)_Right.imag())
{ // construct from other complex type
}
template<class _Other>
_Myt& operator=(const complex<_Other>& _Right)
{ // assign other complex type
this->_Val[_RE] = (_Ty)_Right.real();
this->_Val[_IM] = (_Ty)_Right.imag();
return (*this);
}
_Myt& operator+=(const _Ty& _Right)
{ // add real
this->_Val[_RE] = this->_Val[_RE] + _Right;
return (*this);
}
_Myt& operator-=(const _Ty& _Right)
{ // subtract real
this->_Val[_RE] = this->_Val[_RE] - _Right;
return (*this);
}
_Myt& operator*=(const _Ty& _Right)
{ // multiply by real
this->_Val[_RE] = this->_Val[_RE] * _Right;
this->_Val[_IM] = this->_Val[_IM] * _Right;
return (*this);
}
_Myt& operator/=(const _Ty& _Right)
{ // divide by real
this->_Val[_RE] = this->_Val[_RE] / _Right;
this->_Val[_IM] = this->_Val[_IM] / _Right;
return (*this);
}
_Myt& operator+=(const _Myt& _Right)
{ // add other complex
this->_Add(_Right);
return (*this);
}
_Myt& operator-=(const _Myt& _Right)
{ // subtract other complex
this->_Sub(_Right);
return (*this);
}
_Myt& operator*=(const _Myt& _Right)
{ // multiply by other complex
this->_Mul(_Right);
return (*this);
}
_Myt& operator/=(const _Myt& _Right)
{ // divide by other complex
this->_Div(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator+=(const complex<_Other>& _Right)
{ // add other complex
this->_Add(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator-=(const complex<_Other>& _Right)
{ // subtract other complex
this->_Sub(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator*=(const complex<_Other>& _Right)
{ // multiply by other complex
this->_Mul(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator/=(const complex<_Other>& _Right)
{ // divide by other complex
this->_Div(_Right);
return (*this);
}
};
#define _CMPLX(T) complex<T >
#define _CTR(T) _Ctraits<T >
#define _TMPLT(T) template<class T >
#include <xcomplex> /* define all complex template functions */
// TEMPLATE FUNCTION operator>>
template<class _Ty,
class _Elem,
class _Tr> inline
basic_istream<_Elem, _Tr>& operator>>(
basic_istream<_Elem, _Tr>& _Istr, complex<_Ty>& _Right)
{ // extract a complex<_Ty>
typedef complex<_Ty> _Myt;
const ctype<_Elem>& _Ctype_fac = _USE(_Istr.getloc(), ctype<_Elem>);
_Elem _Ch = 0;
long double _Real = 0;
long double _Imag = 0;
if (_Istr >> _Ch && _Ch != _Ctype_fac.widen('('))
{ // no leading '(', treat as real only
_Istr.putback(_Ch);
_Istr >> _Real;
_Imag = 0;
}
else if (_Istr >> _Real >> _Ch && _Ch != _Ctype_fac.widen(','))
if (_Ch == _Ctype_fac.widen(')'))
_Imag = 0; // (real)
else
{ // no trailing ')' after real, treat as bad field
_Istr.putback(_Ch);
_Istr.setstate(ios_base::failbit);
}
else if (_Istr >> _Imag >> _Ch && _Ch != _Ctype_fac.widen(')'))
{ // no imag or trailing ')', treat as bad field
_Istr.putback(_Ch);
_Istr.setstate(ios_base::failbit);
}
if (!_Istr.fail())
{ // store valid result
_Ty _Tyreal((_Ty)_Real), _Tyimag((_Ty)_Imag);
_Right = _Myt(_Tyreal, _Tyimag);
}
return (_Istr);
}
// TEMPLATE FUNCTION operator<<
template<class _Ty,
class _Elem,
class _Tr> inline
basic_ostream<_Elem, _Tr>& operator<<(
basic_ostream<_Elem, _Tr>& _Ostr, const complex<_Ty>& _Right)
{ // insert a complex<_Ty>
const ctype<_Elem>& _Ctype_fac = _USE(_Ostr.getloc(), ctype<_Elem>);
basic_ostringstream<_Elem, _Tr, allocator<_Elem> > _Sstr;
_Sstr.flags(_Ostr.flags());
_Sstr.imbue(_Ostr.getloc());
_Sstr.precision(_Ostr.precision());
_Sstr << _Ctype_fac.widen('(') << real(_Right)
<< _Ctype_fac.widen(',') << imag(_Right)
<< _Ctype_fac.widen(')');
basic_string<_Elem, _Tr, allocator<_Elem> > _Str = _Sstr.str();
return (_Ostr << _Str.c_str());
}
_STD_END
#pragma warning(pop)
#pragma pack(pop)
#endif /* RC_INVOKED */
#endif /* _COMPLEX_ */
/*
* Copyright (c) 1992-2009 by P.J. Plauger. ALL RIGHTS RESERVED.
* Consult your license regarding permissions and restrictions.
V5.20:0009 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -