输入运算符多项式

时间:2015-10-11 19:32:52

标签: c++ input io stream

我正在尝试为Polynomial类创建输入运算符。但是,我的输入操作符不是在第一个指数之后读取我的第一个系数和所有内容。它基本上是在没有系数的情况下读取第一项。这是我的代码:

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;

}

1 个答案:

答案 0 :(得分:0)

你这里有问题

if ((in.peek() != '+') || (in.peek() != '-'))

条件始终为真。

在代码中进一步向下使用&&,效果更好。