注册 登录
编程论坛 C语言论坛

如何将“>>”重载为复数输入形式?

持剑的战士 发布于 2020-06-08 21:39, 1572 次点击
重载“>>”运算符,输入格式为:a+bi,其中a, b分别为实部和虚部。
3 回复
#2
rjsp2020-06-09 08:32
没办法,比如 "+123-456x",仅当读取到'x'时,才知道前面“-456”也不应该读取。而标准规定最多只能塞回一个字符到输入流中。

你参考一下 operator>>( ……,std::complex ) 的实现,它允许 -123、(-123),(-123, 456) 等复数形式,若有符号,符号首先出现。
程序代码:
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;
}

#3
rjsp2020-06-09 08:35
假如说你的复数形式只允许 +123-456i 这种必须带虚部的形式,不允许 +123 这种纯实部形式,那倒是可以实现
#4
rjsp2020-06-09 09:02
写了个demo,没验证
程序代码:
#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;
}
输入
123-456i
输出
123-456i


1