我正在处理以下多项式类:
#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();
}
现在,我重载了“-”运算符,它工作正常:
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();
// 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;
}
我只是稍微修改了我的“-”运算符方法以覆盖“a”中的值并返回“a”,并且只使用“c”多项式作为临时值。
我输入了一些调试打印语句,并在计算时确认:
c = a - b;
和
a -= b;
被计算为相同的值。
但是,当我去打印它们时,它们的结果是不同的:
Polynomial 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";
a.print(); cout << "\n";
结果:
7x^4 -6x^3 4x^2
7x^4 1x^2
为什么我的 c = a - b
和 a -= b
在打印它们时给我不同的结果?
最佳答案
Polynomial::operator -=
没有修改 this
,它正在修改 this
的拷贝.如果你改变 Polynomial a= *this
至 Polynomial &a= *this
,即做一个引用而不是一个拷贝,它会像你现在正在修改的那样工作 *this
通过a
.此外,operator <op>=
的返回值通常是引用,而不是值。
关于c++ - 为什么我的运算符重载不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2640261/