c++ - 多项式类 C++ 的问题

标签 c++ operator-overloading polynomial-math

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

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

如有任何帮助,我们将不胜感激!

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

最佳答案

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

另外,正如@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;
}

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

关于c++ - 多项式类 C++ 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19901158/

相关文章:

c++ - 数值包装器

math - 云的算法

python - 识别图形上升趋势或下降趋势

c# - 在 OpenCV 中将图像转换为 BYTE 指针的 MatchTemplate 图像

c++ - 在 R 中快速重复构建包

c++ - 不同类别的 2 个对象之间的信号/插槽

c++ - 将多维数组作为 void * 传递给 extern "C"函数

c++ - 从 C++ 中的 vector 继承类调用 super 运算符==

c++ - 奇怪的 c++ 运算符(运算符 unsigned short())

c++ - 根据度数对多项式项进行排序