多项式类c ++的问题

时间:2013-11-11 07:40:44

标签: c++ operator-overloading polynomial-math

我正在研究一个基本上用+, - ,*,/的多项式类,并计算多项式。我一直遇到错误(特别是输出不正确),我认为这是因为我的一种操作方法(可能是添加??)。

编辑 :将问题缩小到+()运算符。它不能添加多项式和双精度。

任何帮助将不胜感激,拜托!

多项式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;
}

主:

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

3 个答案:

答案 0 :(得分: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;
}

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

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