-5

我有一个PolynomialRational类,它将多项式和有理函数作为向量进行操作。*我已经为、/+和定义了非成员运算符-,但我认为我不知道如何正确使用它们(参见下面的代码):

#include<iostream>
#include<algorithm>
#include<iterator>
#include<functional>
#include<vector>
#include<cmath>

using namespace std;

class Polynomial
{
public:
    Polynomial();
    Polynomial(vector<int>coeffs);
    friend class Rational;
     Accessors
    int Degree() const;
    int Coefficient(int k) const;
    void print() const;
    int get_size() const;
    vector<int> get_vect() const; // THIS RETURNS coefficient.
    void set_vect(vector<int> input); // THIS SETS coefficient TO AN INPUTTED VECTOR OF INTEGERS.
    void constantMultiply(int x);
    void Transform();
    double evaluateAt(double x);

    Polynomial& operator++();
    Polynomial operator++ (int unused);
    Polynomial& operator--();
    Polynomial operator-- (int unused);
    Polynomial& operator+=(Polynomial name1);
    Polynomial& operator-=(Polynomial name2);
    Polynomial& operator*=(Polynomial name3);

private:
    vector<int> coefficient;
};

 poly1 + poly2
Polynomial Add(const Polynomial & poly1, const Polynomial & poly2);
 poly1 - poly2
Polynomial Subtract(const Polynomial & poly1, const Polynomial & poly2);
 poly1 * poly2
Polynomial Multiply(const Polynomial & poly1, const Polynomial & poly2);

int zero_detect( vector<int> a , int j);
vector<int> flip( vector<int> input );

class Rational
{
public:
    Rational();
    Rational(Polynomial p);
    Rational(Polynomial pN, Polynomial pD);
    double evaluateAt(double x);
    void print();
    Rational& operator++();
    Rational operator++(int unused);
    Rational& operator--();
    Rational operator--(int unused);
    Rational& operator+=(Rational name1);
    Rational& operator-=(Rational name1);
    Rational& operator*=(Rational name1);
    Rational& operator/=(Rational name1);
    Polynomial get_poly_top(); // SOME SIMPLE ACCESSORS.
    Polynomial get_poly_bot();
private:
    Polynomial top;
    Polynomial bot;
};

Polynomial operator+ (Polynomial a , Polynomial b);
Polynomial operator- (Polynomial a , Polynomial b);
Polynomial operator* (Polynomial a , Polynomial b);

Rational operator+ (Rational a , Rational b);
Rational operator- (Rational a , Rational b);
Rational operator* (Rational a , Rational b);
Rational operator/ (Rational a , Rational b);

bool operator== (Polynomial a , Polynomial b);
bool operator!= (Polynomial a , Polynomial b);
bool operator< (Polynomial a , Polynomial b);
bool operator> (Polynomial a , Polynomial b);
Polynomial sign_swap(Polynomial input); // THIS NEGATES HE COEFFICIENT CELLS, AND HELPS WITH DEFINING SUBTRACTION OFF OF ADDITION.

int main(void)
{
    cout << "Welcome! Please input the coefficients of the first polynomial, p." << endl;
    cout << "When you are finished, enter -1234." << endl;

    vector<int> user_vect1;
    vector<int> user_vect2;

    int input1;
    do
    {
        cin >> input1;
        if(input1 == -1234)
        {
            continue;
        }
        else
        {
            user_vect1.push_back(input1);
        }
    }while(input1 != -1234); // THIS IS THE USER INPUT GADGET.

    cout << endl << endl;

    cout << "Your first polynomial is ";

    Polynomial user_poly1(user_vect1);
    Polynomial temp_poly1(user_vect1);
    user_poly1.print();

    cout << "." << endl;

    cout << "Its transform is ";

    user_poly1.Transform();
    user_poly1.print();

    cout << "." << endl << endl;

    cout << "Please input the coefficients of the second polynomial, q." << endl;

    int input2;
    do
    {
        cin >> input2;
        if(input2 == -1234)
        {
            continue;
        }
        else
        {
            user_vect2.push_back(input2);
        }
    }while(input2 != -1234);

    cout << endl << endl;

    cout << "Your second polynomial is ";

    Polynomial user_poly2(user_vect2);
    Polynomial temp_poly2(user_vect2); // I WANTED A TEMPORARY POLYNOMIAL BECAUSE user_poly(i) GETS TRANSFORMED LATER.
    user_poly2.print();

    cout << "." << endl;

    cout << "Its transform is ";

    user_poly2.Transform();
    user_poly2.print();

    cout << "." << endl << endl;

    cout << "p(x)+q(x) = ";
    /*FUNCTION CALL*/
    (temp_poly1+temp_poly2).print();
    cout << endl << endl;

    cout << "p(x)-q(x) = ";
    /* FUNCTION CALL */
    (temp_poly1-temp_poly2).print();
    cout << endl << endl;

    cout << "p(x)*q(x) = ";
    /*FUNCTION CALL */
    (temp_poly1*temp_poly2).print();
    cout << endl << endl;

    cout << "p(x)/q(x) = ";
    /*FUNCTION CALL*/
    Rational user_rat1(temp_poly1,temp_poly2);
    user_rat1.print();
    cout << endl << endl;

    cout << "p(x)/q(x) + p(x)*q(x) = ";
    /*FUNCTION CALL*/
    Rational user_rat2(temp_poly1+(temp_poly1)*(temp_poly2*temp_poly2),temp_poly2);
    (user_rat2).print();
    cout << endl << endl;

    cout << "p(x)+1 = ";
    /*FUNCTION CALL*/
    (++temp_poly1).print();
    cout << endl << endl;

    cout << "p(x)+2 = ";
    /*FUNCTION CALL*/
    (++temp_poly1).print();
    cout << endl << endl;
    --(--temp_poly1);

    cout << "(p(x)/q(x))*(1+x^2-3x^4) = ";
    /*FUNCTION CALL*/
    vector<int> temp_insert;
    temp_insert.push_back(1);
    temp_insert.push_back(0);
    temp_insert.push_back(1);
    temp_insert.push_back(0);
    temp_insert.push_back(-3); // HERE I'M PUSHING THE VALUES OF THE DESIRED POLYNOMIAL INTO temp_insert.
    Polynomial temporary(temp_insert);
    Rational temp_rat1(temporary);
    (user_rat1*temp_rat1).print(); // <-- RIGHT HERE IS THE BIGGEST ISSUE I'M HAVING. IT'S NOT .print() I DON'T THINK.
    cout << endl << endl;

    cout << "Does p(x) equal q(x)? ";
    /*FUNCTION CALL*/ //<--- Should be a function that is a void, but outputs "Yes." or "No."
    if( temp_poly1 == temp_poly2 )
    {
        cout << "Yes.";
    }
    else
    {
        cout << "No.";
    }
    cout << endl << endl;

    cout << "Is p(x) < q(x)? ";
    /*FUNCTION CALL*/ //<--- Should be a function that is a void, but outputs "Yes." or "No."
    if( temp_poly1 < temp_poly2 )
    {
        cout << "Yes.";
    }
    else
    {
        cout << "No.";
    }
    cout << endl << endl;

    cout << "p(2) = ";
    /*FUNCTION CALL*/
    temp_poly1.evaluateAt(2);
    cout << endl << endl;

    cout << "p(3)/q(3)= ";
    /*FUNCTION CALL*/
    double fun1 = temp_poly1.evaluateAt(3);
    double fun2 = temp_poly2.evaluateAt(3);
    cout << fun1/fun2;
    cout << endl << endl;
};

Polynomial::Polynomial()
{
    coefficient.push_back(0);
}

Polynomial::Polynomial(vector<int> coeffs)
{
    coefficient = coeffs;
}

int Polynomial::Degree() const
{
    int deg = 0;
    for (size_t i = 0; i < coefficient.size(); ++i)
    {
        if (coefficient[i] != 0)
        {
            deg = i;
        }
    }
    return deg;
}

int Polynomial::Coefficient(int k) const
{
    return coefficient[k + 1];
}

void Polynomial::print() const
{
    for( size_t i = 0 ; i < coefficient.size() ; ++i )
    {
        if( coefficient[i] == 0 )
        {
            continue;
        }
        else
        {
            if( i == 0 )
            {
                cout << coefficient[i];
            }
            else
            {
                if(zero_detect(coefficient, i) == i)
                {
                    if( coefficient[i] == 1 || coefficient[i] == -1 )
                    {
                        if( coefficient[i] == 1)
                        {
                            cout << "";
                        }
                        else
                        {
                            cout << "-";
                        }
                    }
                    else
                    {
                        cout << coefficient[i];
                    }
                    if( i == 1 )
                    {
                        cout << "x";
                    }
                    else
                    {
                        cout << "x^" << i;
                    }
                }
                else
                {
                    cout << "+";
                    if( coefficient[i] == 1 || coefficient[i] == -1 )
                    {
                        if( coefficient[i] == 1)
                        {
                            cout << "";
                        }
                        else
                        {
                            cout << "-";
                        }
                    }
                    else
                    {
                        cout << coefficient[i];
                    }
                    if( i == 1 )
                    {
                        cout << "x";
                    }
                    else
                    {
                        cout << "x^" << i;
                    }
                }
            }
        }   
    }
}

int Polynomial::get_size() const
{
    return coefficient.size();
}

vector<int> Polynomial::get_vect() const
{
    return coefficient;
}

void Polynomial::constantMultiply(int x)
{
    for( size_t i = 0 ; i < coefficient.size() ; ++i )
    {
        coefficient[i] *= x;
    }
}

void Polynomial::Transform()
{
    vector<int> transform;
    for( size_t i = 1 ; i < coefficient.size() ; ++i )
    {
        transform.push_back(coefficient[i]*i);
    }
    coefficient = transform;
}

double Polynomial::evaluateAt(double x)
{
    double product=0;
    for(size_t i = 0 ; i < coefficient.size() ; ++i )
    {
        product += (double)coefficient[i]*pow(x,(double)i);
    }
    return product;
}

Polynomial Add(const Polynomial& poly1, const Polynomial& poly2)
{
    vector<int> Poly1 = poly1.get_vect();
    vector<int> Poly2 = poly2.get_vect();
    vector<int> Poly3;

    if( Poly1.size() < Poly2.size() )
    {
        for(size_t i = Poly1.size() ; i< Poly2.size() ; ++i )
        {
            Poly1.push_back(0);
        }
    }
    else if( Poly1.size() > Poly2.size() )
    {
        for(size_t i = Poly2.size() ; i< Poly1.size() ; ++i )
        {
            Poly2.push_back(0);
        }
    }
    for( size_t i = 0 ; i < max(Poly1.size(),Poly2.size()) ; ++i )
    {
        Poly3.push_back(Poly1[i]+Poly2[i]);
    }
    return Poly3;
}

Polynomial Subtract(const Polynomial & poly1, const Polynomial & poly2)
{
    vector<int> Poly1 = poly1.get_vect();
    vector<int> Poly2 = poly2.get_vect();
    vector<int> Poly3;

    if( Poly1.size() < Poly2.size() )
    {
        for(size_t i = Poly1.size() ; i< Poly2.size() ; ++i )
        {
            Poly1.push_back(0);
        }
    }
    else if( Poly1.size() > Poly2.size() )
    {
        for(size_t i = Poly2.size() ; i< Poly1.size() ; ++i )
        {
            Poly2.push_back(0);
        }
    }
    for( size_t i = 0 ; i < max(Poly1.size(),Poly2.size()) ; ++i )
    {
        Poly3.push_back(Poly1[i]-Poly2[i]);
    }
    return Poly3;
}

Polynomial Multiply(const Polynomial & poly1, const Polynomial & poly2)
{
    if( poly2.get_size()==1 && poly2.Coefficient(0)==1)
    {
        return poly1;
    }
    else
    {
        vector<int> Poly1 = poly1.get_vect();
        vector<int> Poly2 = poly2.get_vect();
        vector<int> Poly3;

        Poly3.resize(Poly1.size() + Poly2.size() - 1, 0);

        for (size_t i = 0; i != Poly1.size(); i++)
            for (size_t j = 0; j != Poly2.size(); j++)
                Poly3[i+j] += Poly1[i] * Poly2[j];

        return Poly3;
    }
}

int zero_detect( vector<int> a , int j)
{
    int count=0;
    for( size_t i = 0 ; i < j ; ++i )
    {
        if( a[i] == 0 )
        {
            ++count;
        }
    }
    return count;
}

Rational::Rational()
{
    vector<int> temp;
    temp.push_back(0);
    top.set_vect(temp);
    ++bot;
}

Rational::Rational(Polynomial p)
{
    top = p;
    ++bot;
}

Rational::Rational(Polynomial pN, Polynomial pD)
{
    top = pN;
    bot = pD;
}

double Rational::evaluateAt(double x)
{
    return top.evaluateAt(x)/bot.evaluateAt(x);
}

void Rational::print()
{
    top.print();
    cout << " / ";
    bot.print();
}

Polynomial operator+ (Polynomial a , Polynomial b)
{
    return Add(a,b);
}
Polynomial operator- (Polynomial a , Polynomial b)
{
    return Subtract(a,b);
}
Polynomial operator* (Polynomial a , Polynomial b)
{
    return Multiply(a,b);
}

Rational operator+ (Rational a , Rational b)
{
    Rational c((a.get_poly_top()*b.get_poly_bot())+(b.get_poly_top()*a.get_poly_bot()), a.get_poly_bot()*b.get_poly_bot());
    return c;
}
Rational operator- (Rational a , Rational b)
{
    Rational c((a.get_poly_top()*b.get_poly_bot())+(sign_swap(b.get_poly_top()*a.get_poly_bot())), a.get_poly_bot()*b.get_poly_bot());
    return c;
}
Rational operator* (Rational a , Rational b)
{
    Rational c(a.get_poly_top()*b.get_poly_top(), a.get_poly_bot()*b.get_poly_bot());
    return c;
}
Rational operator/ (Rational a , Rational b)
{
    Rational c(a.get_poly_top()*b.get_poly_bot(), a.get_poly_bot()*b.get_poly_top());
    return c;
}

Rational& Rational::operator++()
{
    Rational c(bot, bot);
    return *this+c;
}

Rational Rational::operator++(int unused)
{
    Rational copy(*this);
    Rational c(bot, bot);
    *this = *this+c;
    return copy;
}
Rational& Rational::operator--()
{
    Rational c(bot, bot);
    return *this-c;
}

Rational Rational::operator--(int unused)
{
    Rational copy(*this);
    Rational c(bot, bot);
    *this = *this-c;
    return copy;
}

bool operator== (Polynomial a , Polynomial b)
{
    vector<int>temp1=a.get_vect();
    vector<int>temp2=b.get_vect();
    if( a.Degree() != b.Degree() )
    {
        return false;
    }
    else
    {
        for(size_t i = 0 ; i<a.get_size() ; ++i)
        {
            if( temp1[i]!= temp2[i])
            {
                return false;
            }
        }
        return true;
    }
}
bool operator!= (Polynomial a , Polynomial b)
{
    return !(a==b);
}
bool operator< (Polynomial a , Polynomial b)
{
    if(a.Degree() < b.Degree())
    {
        return true;
    }
    else
    {
        return false;
    }
}
bool operator> (Polynomial a , Polynomial b)
{
    if(a.Degree() > b.Degree())
    {
        return true;
    }
    else
    {
        return false;
    }
}

Polynomial Rational::get_poly_top()
{
    return top;
}

Polynomial Rational::get_poly_bot()
{
    return bot;
}

Polynomial sign_swap(Polynomial input)
{
    vector<int> temp = input.get_vect();
    for(size_t i = 0 ; i<input.get_size() ; ++i )
    {
        temp[i] *=-1;
    }
    input.set_vect(temp);
    return input;
}

void Polynomial::set_vect(vector<int> input)
{
    coefficient = input;
}

Polynomial& Polynomial::operator++()
{
    coefficient[0]++;
    return *this;
}
Polynomial Polynomial::operator++ (int unused) // &
{
    Polynomial copy(*this);
    coefficient[0]++;
    return copy;
}
Polynomial& Polynomial::operator--()
{
    coefficient[0]--;
    return *this;
}
Polynomial Polynomial::operator-- (int unused) // &
{
    Polynomial copy(*this);
    coefficient[0]--;
    return copy;
}
Polynomial& Polynomial::operator+=(Polynomial name1)
{
    return Add(*this, name1);
}
Polynomial& Polynomial::operator-=(Polynomial name2)
{
    return Subtract(*this, name2);
}
Polynomial& Polynomial::operator*=(Polynomial name3)
{
    return Multiply(*this, name3);  
}

我收到一条错误消息,提示我在代码中的某些向量进程存在超出范围的问题。


笔记:

如果您正在参加编程课程,而这恰好是您的作业,那么请不要将此代码复制并粘贴到您的源代码中并为您自己的利益使用它。相反,如果您遇到麻烦,请将其用作基础。


任何解决这个有理乘法问题的帮助,或者任何其他看起来效率低下、多余、无用等的东西都将不胜感激。谢谢你。:-)


问题:

我创建了一个属于RationalfriendPolynomial类,单参数构造函数Rational( Polynomial p)将其顶部多项式设置topp,其数据类型为Polynomial,并将其底部多项式设置bot1,其数据类型与 相同top。我在使用这个单参数构造函数中创建了一个Rational对象,该构造函数传入了我在 中手动生成的向量,但是当我尝试打印两个有理对象的乘积时——第一个,是一个带有和的双参数类型构造1(1 多项式)--我收到一个错误,指出向量的边界超出范围。我不明白为什么...mainmainuser_rat1top=pbot=

4

1 回答 1

0

资源:

Polynomial Multiply(const Polynomial & poly1, const Polynomial & poly2)
{
    if( poly2.get_size()==1 && poly2.Coefficient(0)==1)

有问题的方法:

int Polynomial::Coefficient(int k) const
{
    return coefficient[k + 1];
}

如果大小为 1,那么您只能到达coefficient[0]and k + 1here is 1。您可能的意思是:

int Polynomial::Coefficient(int k) const
{
    return coefficient[k];
}
于 2013-08-14T23:39:55.967 回答