2012-01-24 40 views
1

我要代表班多項式:代表多項式鏈表

PolyNode(下一個變量):

  1. INT _電源
  2. 雙_coefficient
  3. PolyNode _next

Polynom(與下一個構造函數&法):

  1. 公共多項式() - 空構造 - 空列表
  2. 公共多項式(PolyNode P) - 從PolyNode得到PARAM並插入他作爲第一個列表
  3. 公共多項式ADDNODE上(PolyNode p) - 從PolyNode獲取參數並將其添加到Polynom。 並返回新的多項式

這是測試用例:

// Create two Polynoms 
    Polynom p1 = new Polynom(); 
    p1.addNode(new PolyNode(0,2)); 
    p1.addNode(new PolyNode(2,4)); 
    System.out.println("\nP1:"); 
    System.out.println(p1); 

    Polynom p2 = new Polynom(new PolyNode(0,2)); 
    p2.addNode(new PolyNode(2,-1)); 
    p2.addNode(new PolyNode(4,5)); 
    System.out.println("\nP2:"); 
    System.out.println(p2); 

這是有用輸出:

P1: 4.0倍^ 2 + 2.0

P2: 5.0x^4-1.0x^2 + 2.0

這是PolyNode I類寫道:

public class PolyNode 
{ 
    char _operation; 
    int _power;  
    double _coefficient;  
    PolyNode _next; 



    public PolyNode() 
{ 
    _next = null; 
    _operation = '+'; 
    _coefficient = 1; 
    _power = 1; 
} 

    public PolyNode(char oper, double coeff, int power, PolyNode next) 
{ 
    _operation = oper; 
    _coefficient = coeff; 
    _power = power; 
    _next = next; 
} 


public PolyNode(PolyNode next) 
{ 
    _next = next; 
} 

public PolyNode(int power, int coeff) 
{ 
_power = power; 
    _coefficient = coeff; 
} 

public void setSign(char oper) 
{ 
    _operation = oper; 
} 


public void setCoef(double coeff) 
{ 
    _coefficient = coeff; 
} 


public void setPower(int power) 
{ 
    _power = power; 
} 


public void setNext(PolyNode next) 
{ 
    _next = next; 
} 


public char getSign() 
{ 
    return _operation; 
} 


public double getCoeff() 
{ 
    return _coefficient; 
} 

    public int getPower() 
{ 
    return _power; 
} 

public PolyNode getNext() 
{ 
    return _next; 
} 

public boolean isEnd() 
{ 
    return (_next == null); 
} 

} 

這是我寫的多項式類:

public class Polynom 
{ 
    private PolyNode _head; 


    public Polynom() 
    { 
     _head = null; 
    } 

     public Polynom (Polynom poly) 
    { 
     Polynom r = new Polynom (poly); 
    } 

    public Polynom (PolyNode p) 
    { 
     _head = p; 
    } 

    public Polynom addNode (PolyNode p) 
    { 
     Polynom r = new Polynom (p); 
     PolyNode current; 

     if (_head == null) 
       _head = p; 

     else 
     { 
      current = _head; 
      while (current._next !=null) 
       current = current._next; 
       current._next = p; 

      } 
       return r; 
     } 

     public String toString() 
     { 
      String s = ""; 

      while (_head != null) 
      { 
      s += _head.getCoeff() + "x^" + _head.getPower(); 
      _head = _head._next; 
     } 

      return s; 

     } 
    } 

這是我的錯誤輸出:

P1: 2.0x^04.0x^2

P2: 2.0倍^^0-1.0x^25.0x 4

我不明白鏈表的想法!

toString()方法需要這樣的示例輸出:

R = 3.8x10 - 5.9x3 + 5.5x2 - 11.0

將顯示像上的toString():

3.8 x^10 - 5.9 x^3 + 5.5 x^2 - 11。0

+0

希望Java有一個[LinkedList](http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html)類,它將爲您完成存儲工作。 – Riduidel

+1

這可能無法解決所有問題,但在Polynom.toString方法中,您正在重新分配Polynom的_head,這可能不是您想要的。您應該聲明一個局部變量來迭代所有PolyNodes。 – ARRG

回答

1

我認爲如果你有一個用於PolyNode的toString()方法,Polynom只是在列表中有一個節點時調用該方法會更容易。

要處理的唯一「特殊」情況是列表的頭部,如果該術語是肯定的,則不需要打印「+」號。

除此之外,也有你的toString()實現,尤其是在這樣幾個問題,你遍歷列表:

public String toString(){ 
    StringBuffer s = new StringBuffer(); 
    // WARNING: you don't want to use the list head for iteration, 
    // otherwise you lose the reference to it, and basically to the whole list! 
    PolyNode current = _head; // so we use a cursor node reference 
    while (current.next!=null){ // while current is not the last node 
     // you want to have the sign first, for every node, 
     // except the first, if it's positive 
     if(current!=_head || current.getSign()=='-') 
      s.append(current.getSign() + " "); 
     // then you append the coefficient 
     s.append(current.getCoeff()); 
     // and then the exponent 
     s.append(" x^" + current.getPower() + " "); 
     // you keep going to the next node 
     current = current._next; 
    } 
    return s.toString(); 
} 

我建議你看一下鏈表的一些實施不懂,然後進行練習。

+0

謝謝...我們不允許使用StringBuffer或「append」.. = \。謝謝 – Oshrib

+0

嗯,好的:好吧,只需使用像你那樣的字符串。 :) –

1

正如Riduidel所說,有一個Java LinkedList類可以處理您的存儲和導航。因此,您可以使用LinkedList類來存儲PolyNode對象。
要從toString()方法解決輸出錯誤的具體問題:

  • 創建輸出String時,這樣你就可以打印一個「+」,或者你需要檢查你_operationPolyNode屬性「 - 」 (有額外的檢查沒有在第一項的前面顯示一個「+」如果是正數)

  • 你需要遍歷降序排列爲指數(即power)在PolynomPolyNode項目,最簡單的方法要做到這一點是迭代過以相反的順序(或一個更容易,可以使用LinkedList並使用Iterator其開始於List的末尾)

  • 此外,StringBufferStringBuilder類的使用是優選的,以只使用String,特別是在建築物可能很長的很多步驟中的字符串。

+0

嗨,謝謝。我們非常受限於類的使用 - 我們不允許使用StringBuffer ...或者簡單地說 - 我們不允許使用比我的代碼中顯示的問題更多的問題 – Oshrib

+1

好吧,確保您的'PolyNode'將按正確順序打印的最簡單方法是實現'public Polynom addNode(PolyNode p)'這樣一種方式,使得具有最高「power」的'PolyNode'總是存儲在'PolyNome'的第一個位置。這實際上相當於插入排序(按降序排列)。爲了促進這一點並改進重用,你可以添加一些額外的比較邏輯(例如,使'PolyNode'實現'Comparable'接口,或者如果你不能使用它,只需在'PolyNode'中寫一個'compare'方法)實現這種行爲。祝你好運! – fredo

1

馬曼17 ....我在同樣的業務:-))

我看見你已經添加的屬性 - 焦_operation; 從我在OpenU論壇看到這是不允許的...

+0

它也不需要。只要檢查係數是否在正確的位置就足夠了。 – fredo

1

在addNode方法我認爲你犯了一個錯誤。 他們說你需要將新節點添加到他的正確位置(關於他的電源號碼)

你需要檢查新的PolyNode電源並在polynom中找到他的位置並輸入它。