我需要编写一个通过修改当前多项式求导的方法。我可以编写一个适用于返回类型的程序。代码如下:
public PolynomialSortedList differentiate() {
PolynomialSortedList res = new PolynomialSortedList();
for(PolyNode tmp = poly; tmp != null; tmp = tmp.next)
{
if(tmp.exp != 0)
res.addTerm(tmp.coef * tmp.exp, tmp.exp - 1 );
}
return res;
}
如何使用代码的上部部分将其变为 void:
public class PolynomialSortedList implements Polynomial {
private PolyNode poly;
private double TOLERANCE = 0.00000001;
public PolynomialSortedList() {
poly = null;
}
private static class PolyNode {
int coef;
int exp;
PolyNode next;
PolyNode(int coef, int exp,PolyNode next) {
this.coef = coef;
this.exp = exp;
this.next = next;
}
}
}
最佳答案
假设您通过 myPolyList. Differentiate();
调用 Differentiate,那么您可以尝试将 return res;
与 this.poly = res.poly 交换;
。唯一的问题是,现在这还不太有效,因为 poly
是 private
并且您无法从 polyList 中获取第一个 Poly。
编辑:有关您的程序如何设置的更多信息将帮助我给出更具体的答案。
关于java - 求多项式的导数(无效版本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15491617/