我有以下的多項式類我的工作:爲什麼我的運營商超載工作不正常?
#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
給我不同的結果當我去打印他們?
你應該申報的二進制數學運算符(包括賦值操作符,如' - =')取一個const參考輸入。另外,你應該聲明你的賦值操作符返回一個引用,並使用'return * this;'。所以,你的減-和賦值運算符應該是這樣的:'多項式與多項式::運算符 - =(常量多項式&B)'。 – 2010-04-14 19:13:14
另一個錯誤:你的構造函數沒有設置「deg」。你可能要考慮使用'vector'來表示你的係數,除非你之前沒有使用它,在這種情況下它可能會有點多。 –
2010-04-14 19:18:33