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