如何将“>>”重载为复数输入形式?
重载“>>”运算符,输入格式为:a+bi,其中a, b分别为实部和虚部。
2020-06-08 21:39
程序代码:template <class _Ty, class _Elem, class _Tr>
basic_istream<_Elem, _Tr>& operator>>(basic_istream<_Elem, _Tr>& _Istr, complex<_Ty>& _Right)
{
const ctype<_Elem>& _Ctype_fac = _STD use_facet<ctype<_Elem>>(_Istr.getloc());
_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(static_cast<_Ty>(_Real)), _Tyimag(static_cast<_Ty>(_Imag));
_Right = complex<_Ty>(_Tyreal, _Tyimag);
}
return _Istr;
}
2020-06-09 08:32
2020-06-09 08:35
程序代码:#include <iostream>
#include <sstream>
template<typename T> struct DemoComplex
{
T real = T();
T imag = T();
};
template<class T, class E, class Tr>
std::basic_istream<E,Tr>& operator>>( std::basic_istream<E,Tr>& is, DemoComplex<T>& dc )
{
const std::ctype<E>& ctype_fac = std::use_facet<std::ctype<E>>(is.getloc());
T real, imag;
E ch;
if( is >> real >> imag )
{
if( is>>ch && ch!=ctype_fac.widen('i') )
{
is.putback( ch );
is.setstate( std::ios_base::failbit );
}
}
if( !is.fail() )
{
dc.real = std::move(real);
dc.imag = std::move(imag);
}
return is;
}
template<class T, class E, class Tr>
std::basic_ostream<E,Tr>& operator<<( std::basic_ostream<E,Tr>& os, const DemoComplex<T>& dc )
{
const std::ctype<E>& ctype_fac = std::use_facet<std::ctype<E>>(os.getloc());
std::basic_ostringstream<E,Tr,std::allocator<E>> sstr;
sstr.flags( os.flags() );
sstr.imbue( os.getloc() );
sstr.precision( os.precision() );
sstr << dc.real << std::showpos << dc.imag << ctype_fac.widen('i');
return os << sstr.str();
}
int main( void )
{
DemoComplex<int> a;
std::cin >> a;
std::cout << a;
}输入
2020-06-09 09:02