为什么我的运算符重载不能正常工作?

Mit*_*rax 3 c++

我有以下多项式课程,我正在研究:

#include <iostream>

using namespace std;

class Polynomial
{
//define private member functions
private:
   int coef[100];  // array of coefficients
   // coef[0] would hold all coefficients of x^0
   // coef[1] would hold all x^1
   // coef[n] = x^n ...

   int deg;        // degree of polynomial (0 for the zero polynomial)

//define public member functions
public:
   Polynomial::Polynomial() //default constructor
   {
      for ( int i = 0; i < 100; i++ )
      {
         coef[i] = 0;
      }
   }
   void set ( int a , int b ) //setter function
   {
      //coef = new Polynomial[b+1];
      coef[b] = a;
      deg = degree();
   }

   int degree()
   {
      int d = 0;
      for ( int i = 0; i < 100; i++ )
         if ( coef[i] != 0 ) d = i;
      return d;
   }

   void print()
   {
      for ( int i = 99; i >= 0; i-- ) {
         if ( coef[i] != 0 ) {
            cout << coef[i] << "x^" << i << " ";
         }
      }
   }

   // use Horner's method to compute and return the polynomial evaluated at x
   int evaluate ( int x )
   {
      int p = 0;
      for ( int i = deg; i >= 0; i-- )
         p = coef[i] + ( x * p );
      return p;
   }

   // differentiate this polynomial and return it
   Polynomial differentiate()
   {
      if ( deg == 0 )  {
         Polynomial t;
         t.set ( 0, 0 );
         return t;
      }
      Polynomial deriv;// = new Polynomial ( 0, deg - 1 );
      deriv.deg = deg - 1;
      for ( int i = 0; i < deg; i++ )
         deriv.coef[i] = ( i + 1 ) * coef[i + 1];
      return deriv;
   }

   Polynomial Polynomial::operator + ( Polynomial b )
   {
      Polynomial a = *this; //a is the poly on the L.H.S
      Polynomial c;

      for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i];
      for ( int i = 0; i <= b.deg; i++ ) c.coef[i] += b.coef[i];
      c.deg = c.degree();

      return c;
   }

   Polynomial Polynomial::operator += ( Polynomial b )
   {
      Polynomial a = *this; //a is the poly on the L.H.S
      Polynomial c;

      for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i];
      for ( int i = 0; i <= b.deg; i++ ) c.coef[i] += b.coef[i];
      c.deg = c.degree();

      for ( int i = 0; i < 100; i++) a.coef[i] = c.coef[i];
      a.deg = a.degree();

      return a;
   }

   Polynomial Polynomial::operator -= ( Polynomial b )
   {
      Polynomial a = *this; //a is the poly on the L.H.S

      Polynomial c;

      for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i];
      for ( int i = 0; i <= b.deg; i++ ) c.coef[i] -= b.coef[i];
      c.deg = c.degree();


      for ( int i = 0; i < 100; i++) a.coef[i] = c.coef[i];
      a.deg = a.degree();

      return a;
   }

   Polynomial Polynomial::operator *= ( Polynomial b )
   {
      Polynomial a = *this; //a is the poly on the L.H.S
      Polynomial c;

      for ( int i = 0; i <= a.deg; i++ )
         for ( int j = 0; j <= b.deg; j++ )
            c.coef[i+j] += ( a.coef[i] * b.coef[j] );
      c.deg = c.degree();

      for ( int i = 0; i < 100; i++) a.coef[i] = c.coef[i];
      a.deg = a.degree();

      return a;
   }

   Polynomial Polynomial::operator - ( Polynomial b )
   {
      Polynomial a = *this; //a is the poly on the L.H.S
      Polynomial c;

      for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i];
      for ( int i = 0; i <= b.deg; i++ ) c.coef[i] -= b.coef[i];
      c.deg = c.degree();


      return c;
   }

   Polynomial Polynomial::operator * ( Polynomial b )
   {
      Polynomial a = *this; //a is the poly on the L.H.S
      Polynomial c;

      for ( int i = 0; i <= a.deg; i++ )
         for ( int j = 0; j <= b.deg; j++ )
            c.coef[i+j] += ( a.coef[i] * b.coef[j] );
      c.deg = c.degree();
      return c;
   }
};

int main()
{
   Polynomial a, b, c, d;
   a.set ( 7, 4 ); //7x^4
   a.set ( 1, 2 ); //x^2

   b.set ( 6, 3 ); //6x^3
   b.set ( -3, 2 ); //-3x^2

   c = a - b; // (7x^4 + x^2) - (6x^3 - 3x^2)
   a -= b;

   c.print();
   cout << "\n";


   a.print();
   cout << "\n";


   c = a * b; // (7x^4 + x^2) * (6x^3 - 3x^2)
   c.print();

   cout << "\n";

   d = c.differentiate().differentiate();
   d.print();

   cout << "\n";

   cout << c.evaluate ( 2 ); //substitue x with 2

   cin.get();
}
Run Code Online (Sandbox Code Playgroud)

现在,我有" - "运算符重载,它工作正常:

Polynomial Polynomial::operator - ( Polynomial b )
   {
      Polynomial a = *this; //a is the poly on the L.H.S
      Polynomial c;

      for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i];
      for ( int i = 0; i <= b.deg; i++ ) c.coef[i] -= b.coef[i];
      c.deg = c.degree();

      return c;
   }
Run Code Online (Sandbox Code Playgroud)

但是,我对" - ="运算符有困难:

Polynomial Polynomial::operator -= ( Polynomial b )
   {
      Polynomial a = *this; //a is the poly on the L.H.S

      Polynomial c;

      for ( int i = 0; i <= a.deg; i++ ) c.coef[i] += a.coef[i];
      for ( int i = 0; i <= b.deg; i++ ) c.coef[i] -= b.coef[i];
      c.deg = c.degree();

      // overwrite value of 'a' with the newly computed 'c' before returning 'a'
      for ( int i = 0; i < 100; i++) a.coef[i] = c.coef[i];
      a.deg = a.degree();

      return a;
   }
Run Code Online (Sandbox Code Playgroud)

我只是稍微修改了我的" - "运算符方法来覆盖'a'中的值并返回'a',并且只使用'c'多项式作为临时值.

我已经输入了一些调试打印语句,我确认在计算时,两者:

c = a - b;

a - = b;

被计算为相同的值.

但是,当我打印它们时,它们的结果是不同的:

多项式a,b; a.set(7,4); // 7x ^ 4 a.set(1,2); // X ^ 2

b.set(6,3); // 6x ^ 3 b.set(-3,2); // - 3X ^ 2

c = a - b; //(7x ^ 4 + x ^ 2) - (6x ^ 3 - 3x ^ 2)a - = b;

c.print(); cout <<"\n";

打印(); cout <<"\n";

结果:

7x ^ 4 -6x ^ 3 4x ^ 2

7x ^ 4 1x ^ 2

为什么我c = a - ba -= b给我不同的结果,当我去打印出来?

MSN*_*MSN 8

Polynomial::operator -=没有修改this,它正在修改一个副本this.如果您更改Polynomial a= *thisPolynomial &a= *this,即创建引用而不是副本,它将按照您现在正在修改的*this方式工作a.此外,返回值operator <op>=通常是引用,而不是值.

  • 此外,所有参数都应该是`Polynomial const&`. (2认同)