C++ 输入算子多项式

C++ 输入算子多项式,c++,input,io,stream,C++,Input,Io,Stream,我试图为多项式类创建一个输入操作符。然而,我的输入操作符并没有读取我的第一个系数以及第一个指数之后的所有内容。它基本上是读取没有系数的第一项。这是我的密码: std::istream & operator>> (std::istream & in, Polynomial & aPoly) { double tempCoefficient; // temporary storage for co

我试图为多项式类创建一个输入操作符。然而,我的输入操作符并没有读取我的第一个系数以及第一个指数之后的所有内容。它基本上是读取没有系数的第一项。这是我的密码:

std::istream & operator>> (std::istream & in,
                           Polynomial & aPoly)
{
    double tempCoefficient;    // temporary storage for coefficient
    char ch;               // variable that contains results from peek()
    char dum;            // removes useless symbols like '+' and 'x'
    int tempExponent;          // contains current exponent for coefficient
    bool moreTerms = true; // variable that tells when the terms run out
    bool negativeCoefficient = false;  // tells when coefficient is negative
    aPoly.coefficients.resize(0); // Clears aPoly before inserting user input

    in >> ch ;


    if (ch == '-')
    {
        negativeCoefficient = true;
        ch = in.peek();
    }

    else
    {
        in.putback(ch);
    }

    if (ch >= '0' && ch <= '9')
    {
        in >> tempCoefficient;
        if (negativeCoefficient)
        {
            tempCoefficient *= -1;
            negativeCoefficient = false;
        }
        ch = in.peek();
    }



    if (ch == 'x')
    {
        tempCoefficient = 1.0;
        in >> dum;

        if (in.peek() == '^')
        {
            in >> dum;
            in >> tempExponent;
        }

        else
        {
            tempExponent = 1;
        }
    }

    else
    {
        tempExponent = 0;
        moreTerms = false;
    }

    aPoly.coefficients.resize(tempExponent + 1);
    aPoly.coefficients[tempExponent] = tempCoefficient;

    if ((in.peek() != '+') || (in.peek() != '-'))
    {
        moreTerms = false;
    }

    while (moreTerms)
    {
        ch = in.peek();

        if (ch == '+')
        {
            in >> dum; // '+'
        }

        else
        {
            negativeCoefficient = true;
            in >> dum; // '-'
        }

        ch = in.peek();

        if ( (ch >= '0') && (ch <= '9') )
        {
            in >> tempCoefficient;
            if (negativeCoefficient)
            {
                tempCoefficient *= -1;
                negativeCoefficient = false;
            }
            ch = in.peek();
        }

        else
        {
            if (negativeCoefficient)
            {
                tempCoefficient = -1.0;
                negativeCoefficient = false;
            }

            else
            {
                tempCoefficient = 1.0;
            }
        }

        if (ch == 'x')
        {
            in >> dum;

            if (in.peek() == '^')
            {
                in >> dum;
                in >> tempExponent;
            }

            else
            {
               tempExponent = 1;
            }
        }

        else
        {
            tempExponent = 0;
            moreTerms = false;
        }



        if ((in.peek() != '+') && (in.peek() != '-'))
        {
            moreTerms = false;
        }

        aPoly.coefficients[tempExponent] = tempCoefficient;

    }
    return in;

}
std::istream&operator>>(std::istream&in,
多项式(aPoly)
{
double tempcefficient;//系数的临时存储
char ch;//包含来自peek()的结果的变量
char dum;//删除无用的符号,如“+”和“x”
int tempExponent;//包含系数的当前指数
bool moreTerms=true;//告知术语何时用完的变量
bool negativeCoefficient=false;//表示系数为负数的时间
aPoly.coverties.resize(0);//在插入用户输入之前清除aPoly
在>>ch;
如果(ch='-')
{
负效率=真;
ch=in.peek();
}
其他的
{
in.putback(ch);
}
如果(ch>='0'&&ch>温度系数;
if(负效率)
{
温度系数*=-1;
否定效率=假;
}
ch=in.peek();
}
如果(ch='x')
{
温度系数=1.0;
在>>dum中;
如果(在.peek()中='^')
{
在>>dum中;
在>>中;
}
其他的
{
指数=1;
}
}
其他的
{
指数=0;
moreTerms=假;
}
aPoly.系数.调整大小(指数+1);
aPoly.系数[温度指数]=温度系数;
if((in.peek()!='+')| |(in.peek()!='-'))
{
moreTerms=假;
}
while(更多术语)
{
ch=in.peek();
如果(ch=='+')
{
在>>dum;/'+'中
}
其他的
{
负效率=真;
在>>dum;/'-'中
}
ch=in.peek();
如果((ch>='0')&(ch>温度系数;
if(负效率)
{
温度系数*=-1;
否定效率=假;
}
ch=in.peek();
}
其他的
{
if(负效率)
{
温度系数=-1.0;
否定效率=假;
}
其他的
{
温度系数=1.0;
}
}
如果(ch='x')
{
在>>dum中;
如果(在.peek()中='^')
{
在>>dum中;
在>>中;
}
其他的
{
指数=1;
}
}
其他的
{
指数=0;
moreTerms=假;
}
如果((in.peek()!='+')和&(in.peek()!='-'))
{
moreTerms=假;
}
aPoly.系数[温度指数]=温度系数;
}
返回;
}

您遇到了一个问题

if ((in.peek() != '+') || (in.peek() != '-'))
条件始终为真


在下面的代码中,您使用的
&&
效果更好。

请提供一个答案。请说明您的问题。谢谢,但它仍然没有考虑第一个系数:(