2013-03-19 18 views
0

我需要編寫一個方法,它通過修改當前多項式來導出微分。我可以寫一個適用於返回類型的函數。下面的代碼:多項式的導數(void版本)

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; 
} 

我如何使用代碼的這個上部變成無效:

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; 
     } 
    } 
} 

回答

1

假設你叫區分說myPolyList.differentiate();,那麼你可以嘗試換return res;this.poly = res.poly; 。唯一的問題是,現在,這不會起作用,因爲polyprivate,並且您無法從polyList中獲取第一個poly。

編輯:多一點關於如何設置你的程序的信息將幫助我給出更具體的答案。