2

I'm working on a polynomial class that basically does +,-,*,/, and evaluates polynomials. I keep running into bugs (specifically output is incorrect) and I think it's because of one of my operation methods (maybe addition??).

EDIT: Narrowed down problem to +() operator. It cannot add polynomials and a double.

Any help would be appreciated, please!

Polynomial Class CPP:

#include <iostream>
#include "polynomial.h"

using namespace std;

/*
   =======================
       Constructors
   =======================
*/

Polynomial::Polynomial() //default constructor
   {
      for ( int i = 0; i < 20; i++ )
      {
         coefs[i] = 0;
      }
   }


Polynomial::~Polynomial() {}
void Polynomial::set(int coef, int pwr){
    coefs[pwr] = coef;
    pwrs = degree();
}

int Polynomial::degree()
   {
      int d = 0;
      for ( int i = 0; i < 20; i++ )
         if ( coefs[i] != 0 ) d = i;
      return d;
   }

/* 
   =======================
        operator=
   =======================
*/

Polynomial& Polynomial::operator= ( const Polynomial& poly )
{
      if ( this == &poly ) return ( *this ) ;
      else{
          for (int i = 0; i < 20; i++)
              coefs[i] = poly.coefs[i];
      }
      return ( *this );
}

/* 
   =======================
        operator+
   =======================
*/

Polynomial operator+(const Polynomial& a, const Polynomial& b )
{
    Polynomial c;
    for ( int i = 0; i <= a.pwrs; i++ ) c.coefs[i] += a.coefs[i];
    for ( int i = 0; i <= b.pwrs; i++ ) c.coefs[i] += b.coefs[i];
    c.pwrs = c.degree();
    return c;
}

Polynomial operator+(const Polynomial& a, const double& d)
{
    Polynomial c;
    for ( int i = 0; i <= a.pwrs; i++ ){
        if(i == a.pwrs) {
            i=i+1;
            c.coefs[i] = d;
        }
        c.coefs[i] += a.coefs[i];
    }
    c.pwrs = c.degree();
    return c;
}

/*
   =======================
       Operator-
   =======================
*/

Polynomial operator- (const Polynomial& a,const Polynomial& b )
{
    //Polynomial a = *this; //a is the poly on the L.H.S
    Polynomial c;
    for ( int i = 0; i <= a.pwrs; i++ ) c.coefs[i] += a.coefs[i];
    for ( int i = 0; i <= b.pwrs; i++ ) c.coefs[i] -= b.coefs[i];
    c.pwrs = c.degree();
    return c;
}

/*
   =======================
       Operator*
   =======================
*/

Polynomial operator* (const Polynomial& a, const Polynomial& b)
{
    //Polynomial a = *this; //a is the poly on the L.H.S
    Polynomial c;
    for ( int i = 0; i <= a.pwrs; i++ )
        for ( int j = 0; j <= b.pwrs; j++ )
            c.coefs[i+j] += ( a.coefs[i] * b.coefs[j] );
    c.pwrs = c.degree();
    return c;
   }

Polynomial operator*(const Polynomial& poly1, const double& d)
{
    Polynomial poly;
    for(int i = 0; i < 20; i++)
        poly.coefs[i] = poly1.coefs[i] * d;
    poly.pwrs = poly1.pwrs;
    return poly;
}

Polynomial operator*(const double& d, const Polynomial& poly1)
{
    Polynomial poly;
    for(int i = 0; i < 20; i++)
        poly.coefs[i] = d * poly1.coefs[i];
    poly.pwrs = poly1.pwrs;
    return poly;
}

/*
   =======================
       Operator/
   =======================
*/

Polynomial operator/ (const Polynomial& a, const Polynomial& b)
{
    Polynomial c;
    for ( int i = 0; i <= a.pwrs; i++ )
        for ( int j = 0; j <= b.pwrs; j++ )
            c.coefs[i+j] += ( a.coefs[i] / b.coefs[j] );
    c.pwrs = c.degree();
    return c;
   }


ostream& operator<<(ostream& out, const Polynomial& p) {
    for ( int i = 19; i >= 0; i-- ) {
        if(p.pwrs > 1){
            if ( p.coefs[i] != 0 ) {
                cout << p.coefs[i] << "x^" << i << " ";
                if(p.coefs[i-1] > 0)
                    cout << "+";
                else if(p.coefs[i-1] < 0)
                    cout << "";
            }
        }
        else if (p.pwrs == 1)
            cout << p.coefs[i] << "x ";
        else if(p.pwrs == 0)
            cout << p.coefs[i];
    }
    cout << endl;
}

Main:

#include <iostream>
#include "polynomial.h"
#include "monomial.h"

using namespace std;

int main(){
Polynomial a, b, c, d, e,result;
    a.set(1,3); //x^3
    b.set(1,2); //x^2
    c.set(6,1); //6x
    d.set(0.01,10); //(1/100)x^10
    e.set(2,5); //2x^5 
    result = 4*(a+b)*(c+1)*(d-e); // 4 * (x^3+x^2) * (6x+1) * ((1/100)x^10 - 2x^5)
}
4

3 回答 3

1

您可能需要设置pwrs = degree();.operator =

另外,正如@6502 指出的那样,您的说法operator +不正确。你可以这样改变它:

Polynomial operator+(const Polynomial& a, const double& d)
{
    Polynomial c;
    c.coefs[0] = d;
    for ( int i = 0; i <= a.pwrs; i++ ){
        c.coefs[i] += a.coefs[i];
    }
    c.pwrs = c.degree();
    return c;
}

仍然低效,但至少它应该给出正确的结果。

于 2013-11-11T08:16:28.840 回答
0

除了 Henrink 指出的问题之外,将 0.01 分配给 coef 是没有意义的。因为这里只接受 int 。

void Polynomial::set(int coef, int pwr){
    coefs[pwr] = coef;
    pwrs = degree();
}
于 2013-11-11T09:18:04.393 回答
0

添加双精度存在逻辑问题。在这种情况下,您只想添加到coefs[0],而不是全部。

为什么要定义赋值运算符?这个类的默认值不是正确的吗?

于 2013-11-11T08:16:15.577 回答