为什么我的运营商超载工作不正常?
我有以下的多项式类我的工作:为什么我的运营商超载工作不正常?
#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;
和
一个 - = 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”;
a.print(); cout < <“\ n”;
结果:
7X^4 -6x^3^4倍2
7X^4 1X^2
为什么我c = a - b
和a -= b
给我不同的结果当我去打印他们?
Polynomial::operator -=
未修改this
,它修改了this
的副本。如果您将Polynomial a= *this
更改为Polynomial &a= *this
,即作为参考而不是副本,则它将像您现在正在修改*this
到a
一样工作。此外,operator <op>=
的返回值通常是参考值,而不是值。
另外,所有参数都应该是'Polynomial const&'。 – Potatoswatter 2010-04-14 19:19:20
因此,首先,您可能希望将const Polynomial&
而不是Polynomial
传递给您的函数,因为后者创建了一个副本,而前者通过常量引用传递。
其次,我觉得很奇怪,你正在写:
Polynomial b = *this;
而不是写b.coeff[i]
的,你可以简单地写coef[i]
,因为它解析为this->coef[i]
。如果你绝对必须使用一些其他的变量b
,虽然,那么我建议您阅读当您使用以下:
const Polynomial& b = *this;
,并使用以下书写时:
Polynomial& b = *this;
请注意,如果您使用Polynomial
代替的Polynomial&
,那么你的b变量提供是副本,是不相同的*this
;因此,您所做的更改不会像预期的那样影响*this
。
也就是说,写deg = //...
比a.deg = //...
,其中a
代表*this
清晰。我强烈建议你摆脱创建(试图)参考*this
变量的习惯。最后
一个说明,正如在评论已经指出,赋值运算符应该返回类型的引用。所以,你的operator=
,operator+=
,operator-=
等应返回Polynomial&
。这是因为在赋值语句中允许高效的链接。例如:a = b = c
。如果您要退回void
,那么这根本不起作用。随着返回Polynomial
的副本,它会工作,但它会neeedlessly构建副本。在这种情况下使用引用(即Polynomial&
)会阻止复制。
运营商-=
应该修改左侧值(并返回对*this
的引用以允许链接)。
这也常见于其他方面来实现这些功能:
//random example
X& operator+= (const X& other)
{
this->sth += other.sth;
return *this;
}
//free function in terms of the previous
//more verbose than needed for exposition
X operator+ (const X& lhv, const X& rhv)
{
X result(lhv);
result += rhv;
return result;
}
事实上,大多数运营商可以(也应该)在其他方面来实现,甚至还有Boost.Operators合成从相关运营商现有的。
当然,减法只是第二个操作数的负数的加法。 – JohnMcG 2010-04-14 19:18:56
如果你的教授让你使用Boost ...^_- – 2010-04-14 19:19:28
你应该申报的二进制数学运算符(包括赋值操作符,如' - =')取一个const参考输入。另外,你应该声明你的赋值操作符返回一个引用,并使用'return * this;'。所以,你的减-和赋值运算符应该是这样的:'多项式与多项式::运算符 - =(常量多项式&B)'。 – 2010-04-14 19:13:14
另一个错误:你的构造函数没有设置“deg”。你可能要考虑使用'vector'来表示你的系数,除非你之前没有使用它,在这种情况下它可能会有点多。 –
2010-04-14 19:18:33