2014-02-06 45 views
1

我很感激幫助。除了撰寫方法之外,我能夠將此類中的所有內容都修改爲BigInteger格式。誰能幫我解釋爲什麼它不能正常工作?我非常感謝,謝謝。我如何使用BigInteger修復Polynomial類中的「加號」方法

import java.math.BigInteger; 

public class Polynomial { 
private BigInteger[] coef; // coefficients 
private int deg;  // degree of polynomial (0 for the zero polynomial) 



/** Creates the constant polynomial P(x) = 1. 
    */ 
public Polynomial(){ 
    coef = new BigInteger[1]; 
    coef[0] = new BigInteger("1"); 
    deg = 0; 
} 



/** Creates the linear polynomial of the form P(x) = x + a. 
    */ 
public Polynomial(int a){ 
    coef = new BigInteger[2]; 
    coef[1] = new BigInteger("1"); 
    coef[0] = new BigInteger(Integer.toString(a)); 
    deg = 1; 
} 

/** Creates the polynomial P(x) = a * x^b. 
    */ 
public Polynomial(int a, int b) { 
    coef = new BigInteger[b+1]; 
    for(int i = 0; i < b; i++){ 
     if(coef[i] == null) 
      coef[i] = new BigInteger("0"); 

    } 
    coef[b] = new BigInteger(Integer.toString(a)); 
    deg = degree(); 
} 


/** Return the degree of this polynomial (0 for the constant polynomial). 
    */ 
public int degree() { 
    int d = 0; 
    for (int i = 0; i < coef.length; i++) 
     if (coef[i] != null) d = i; // check to make sure this works 
    return d; 
} 

/** Return the composite of this polynomial and b, i.e., return this(b(x)) - compute using Horner's method. 
    */ 
public Polynomial compose(Polynomial b) { 
    Polynomial a = this; 
    Polynomial c = new Polynomial(0, 0); 
    for (int i = a.deg; i >= 0; i--) { 
     Polynomial term = new Polynomial(a.coef[i].intValue(), 0); 
     c = term.plus(b.times(c)); 
    } 
    return c; 
} 

    public Polynomial times(Polynomial b) { 
    Polynomial a = this; 
    Polynomial c = new Polynomial(0, a.deg + b.deg); 
    for (int i = 0; i <= a.deg; i++) 
     for (int j = 0; j <= b.deg; j++) 
      c.coef[i+j] = c.coef[i+j].add((a.coef[i].multiply(b.coef[j]))); 
    c.deg = c.degree(); 
    return c; 
} 

/** Return a textual representation of this polynomial. 
    */ 
public String toString() { 
    if (deg == 0) return "" + coef[0]; 
    if (deg == 1) return coef[1] + "x + " + coef[0]; 
    String s = coef[deg] + "x^" + deg; 
    for (int i = deg-1; i >= 0; i--) { 
     if  (coef[i] == null) continue; 
     else if (coef[i].intValue() > 0) s = s + " + " + (coef[i]); 
     else if (coef[i].intValue() < 0) s = s + " - " + (coef[i].negate()); 
     if(coef[i].intValue() != 0) 
      if  (i == 1) s = s + "x"; 
     else if (i > 1) s = s + "x^" + i; 
    } 
    return s; 
} 

public static void main(String[] args) { 
    Polynomial p = new Polynomial(1,2); 
    Polynomial q = new Polynomial(2,3); 
    Polynomial t = p.compose(q); // incorrect 
    System.out.println("p(q(x))  = " + t); // incorrect 


    } 

} 
+2

嘗試更新與後你重寫'的toString() '也打印多項式 – PopoFibo

+1

您是否使用過調試器來查看問題的根源?你知道這是'plus'而不是'toString'有bug嗎? –

+0

哎呀對不起@PopoFibo,我編輯了上面的代碼以包含toString()方法。它可以是有bug的toString()方法或plus()方法。在調試器中使用它看起來好像是toString(),但無法找出問題所在。 – user3268401

回答

2

我認爲這個問題是您toString()本身,因爲它不對齊的違約機制。意思是,你指定「0'的默認值,但不要在以下行檢查0值:

if  (i == 1) s = s + "x"; 
else if (i > 1) s = s + "x^" + i; 

它被堆積甚至0係數值。添加檢查非零係數的條件只:

if (coef[i].intValue() != 0) 
if  (i == 1) s = s + "x"; 
else if (i > 1) s = s + "x^" + i; 

這應該工作,我沒有測試過,但你可以嘗試測試併發布結果。

編輯:

好吧,我只是想你的代碼,似乎給上述條件的正確信息的地方:

6x^7 + 2x^3 
相關問題