Timonse
Timonse

Reputation: 97

Issues with polynomial class c++

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)
}

Upvotes: 2

Views: 4269

Answers (3)

TerrenceSun
TerrenceSun

Reputation: 473

Besides the problem that Henrink pointed out, assigning 0.01 to coef is meaningless. As only int is accepted here.

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

Upvotes: 0

Henrik
Henrik

Reputation: 23324

You probably need to set pwrs = degree(); in operator =.

Also, as @6502 pointed out, your operator + is incorrect. You could change it like this:

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;
}

Still inefficient, but at least it should give the correct result.

Upvotes: 1

6502
6502

Reputation: 114579

There is a logic problem with addition of a double. In that case you only want to add to coefs[0], not to all of them.

Why are you defining an assignment operator? Isn't the default one correct for this class?

Upvotes: 0

Related Questions