1
我的乘法邏輯在某處不正確。似乎我沒有考慮何時必須添加兩個多項式相乘的結果相同程度的項。多項式乘法度數學與鏈接列表不正確
public Polynomial multiply(Polynomial p) {
if (p.poly == null || this.poly == null) {
Polynomial zero = new Polynomial();
zero.poly = new Node (0, 0, null);
return zero;
} else {
Polynomial retPol = new Polynomial();
retPol.poly = new Node(0, 0, null);
Node front = retPol.poly;
Node entered = p.poly;
Node thisPol = this.poly;
int high = Integer.MIN_VALUE;
int low = Integer.MAX_VALUE;
while (entered != null) {
thisPol = this.poly;
while (thisPol != null) {
if (thisPol.term.degree + entered.term.degree > high)
high = thisPol.term.degree + entered.term.degree;
if (thisPol.term.degree + entered.term.degree < low)
low = thisPol.term.degree + entered.term.degree;
thisPol = thisPol.next;
}
entered = entered.next;
}
entered = p.poly;
Node create = front;
for (int i = low; i <= high; i++) {
create.term.degree = i;
create.term.coeff = 0;
create.next = new Node (0, 0, null);
create = create.next;
}
entered = p.poly;
while (entered != null) {
thisPol = this.poly;
while (thisPol != null) {
int degree = entered.term.degree + thisPol.term.degree;
create = front;
while (create != null) {
if (create.term.degree == degree) {
create.term.coeff = entered.term.coeff * thisPol.term.coeff;
}
create = create.next;
}
thisPol = thisPol.next;
}
entered = entered.next;
}
create = front;
while (create != null) {
if (create.term.degree == high) {
create.next = null;
create = create.next;
}
else
create = create.next;
}
retPol.poly = front;
return retPol;
}
}
我應該得到的答案是:
32.0x^9 + 16.0x^8 + -16.0x^7 + -20.0x^6 + 52.0x^5 + 38.0x^4 + -6.0x^3 + -6.0x^2 + 9.0x + 27.0
,但我真的開始:
32.0x^9 + 16.0x^8 + -16.0x^7 + -8.0x^6 + 16.0x^5 + 24.0x^4 + 12.0x^3 + -6.0x^2 + -9.0x + 27.0
看來,3〜6度邏輯不正確。這是一個邏輯錯誤,我知道。我只是不知道如何解決。我也知道應該爲那些不正確的學位添加條款,但是它看起來好像繞過了那個,只顯示了一個。
任何提示將不勝感激。謝謝。
我試過實現這個,但仍然沒有運氣 –
你的意思是沒有運氣?錯誤的數字? – rsutormin
你想要乘以多少?您只顯示了沒有輸入的預期結果多項式。 – rsutormin