2017-04-23 32 views
0

我被要求使用LinkedList來創建一個程序,該LinkedList表示由一個或多個不同的術語組成的多項式。大多數一切都似乎工作,但是我有一些問題得到的結果多項式來打印時,我想要的格式。Polynomial LinkedList格式化

我的多項式應該按照降序格式化,但是按升序打印。另外當打印多項式時,我需要以某種方式擺脫整個多項式前面的第一個「+」符號,而不會導致每個項之間其餘的「+」符號出現問題。

期限類

public class Term { 

private DecimalFormat formatHelper = new DecimalFormat("#.####"); 
int coeff; 
private int exp; 
private Term next; 

public Term(int exp, int coeff, Term next) { 
    this.setExp(exp); 
    this.coeff = coeff; 
    this.setNext(next); 
} 

public String toString() { 
    String format = formatHelper.format(Math.abs(coeff)); 

    if (getExp() == 0) 
     return format; 
    else 

    if (getExp() == 1) 
     return format + "x"; 
    else 

     return format + "x^" + getExp(); 
} 

public int getExp() { 
    return exp; 
} 

public void setExp(int exp) { 
    this.exp = exp; 
} 

public Term getNext() { 
    return next; 
} 

public void setNext(Term next) { 
    this.next = next; 
} 

多項式類

public class Polynomial { 

private double test = 0.0005; 
private Term head; 

public Polynomial() { 
    head = null; 
} 

/** 
* Adds a term to the current polynomial with the specified coefficient and exponent 
*/ 
public void addTerm(int exp, int coeff) { 

    if (Math.abs(coeff) < test) 

     return; 

    if (head == null || exp < head.getExp()) { 

     head = new Term(exp, coeff, head); 

     return; 
    } 

    Term last = null; 
    Term current = head; 

    while (current != null && exp > current.getExp()) { 
     last = current; 
     current = current.getNext(); 
    } 

    if (current == null || exp != current.getExp()) 
     last.setNext(new Term(exp, coeff, current)); 

    else { 
     current.coeff += coeff; 

     if (Math.abs(current.coeff) < test) 

      if (last != null) 
       last.setNext(current.getNext()); 

      else 
       head = head.getNext(); 
    } 
} 

/** 
* Formats the polynomial 
*/ 
public String toString() { 

    StringBuffer buffer = new StringBuffer(); 
    for (Term term = head; term != null; term = term.getNext()) 

     if (term.coeff < 0) 
      buffer.append(" - " + term.toString()); 
     else 
      buffer.append(" + " + term.toString()); 

     return buffer.toString(); 
} 




/** 
* EXTRA CREDIT - Adds two polynomials 
*/ 
public Polynomial add(Polynomial p2) { 
    Polynomial answer = clone(); 
    for (Term term = p2.head; term != null; term = term.getNext()) 

     answer.addTerm(term.getExp(), term.coeff); 

    return answer; 
} 

/** 
* Special method used only for extra credit, aids in adding of polynomials 
*/ 
public Polynomial clone() { 
    Polynomial answer = new Polynomial(); 

    for (Term term = head; term != null; term = term.getNext()) 
     answer.addTerm(term.getExp(), term.coeff); 

    return answer; 
} 



Tester Class 

public class Prog7 { 

public static void main(String[] args) 
    { 
     Polynomial p1 = new Polynomial(); 
     Polynomial p2 = new Polynomial(); 
     Scanner keyboard = new Scanner(System.in); 
     int coeffChoice; 
     int expChoice; 
     String userInput = ""; 
     String userInput2 = ""; 



     while(!userInput.equalsIgnoreCase("no")){ 

      System.out.println("Please enter the coefficient of the current term: "); 
      coeffChoice = keyboard.nextInt(); 
      System.out.println("Please enter the exponent of the current term: "); 
      expChoice = keyboard.nextInt(); 

      p1.addTerm(expChoice, coeffChoice); 
      System.out.println("Would you like to add a term to the polynomial?"); 
      userInput = keyboard.next(); 
     } 

     System.out.println("Time to start building the second polynomial!"); 


     while(!userInput2.equalsIgnoreCase("no")){ 

      System.out.println("Please enter the coefficient of the current term: "); 
      coeffChoice = keyboard.nextInt(); 
      System.out.println("Please enter the exponent of the current term: "); 
      expChoice = keyboard.nextInt(); 

      p2.addTerm(expChoice, coeffChoice); 
      System.out.println("Would you like to add a term to the polynomial?"); 
      userInput2 = keyboard.next(); 
     } 

     System.out.println("Polynomial 1"); 
     System.out.println(p1.toString()); 

     System.out.println(); 

     System.out.println("Polynomial 2"); 
     System.out.println(p2.toString()); 

     System.out.println(); 

     System.out.println("Polynomial Addition."); 
     System.out.println(p1.add(p2)); 

    } 

示例輸出

電流輸出: + 5X^2 + 4×^ 5 + 9倍^ 6

所需的輸出: 9x^6 + 4x^5 + 5x^2

回答

0

由於您的列表是單鏈接的,因此您無法以反向方向遍歷它。 但是你可以預先附加條款而不是附加條款。爲了擺脫第一個'+',如果第一項是正數,則返回子字符串:

public String toString() { 

    StringBuffer buffer = new StringBuffer(); 
    for (Term term = head; term != null; term = term.getNext()) { 
     if (term.coeff < 0) 
      buffer.insert(0, " - " + term.toString()); 
     else 
      buffer.insert(0, " + " + term.toString()); 
    } 

    if (buffer.charAt(1) == '+') { 
     return buffer.substring(2); 
    else { 
     return buffer.toString(); 
    } 
}