2017-04-23 11 views
0

1つまたは複数の異なる用語で構成される多項式を表す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)); 

    } 

出力例

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

所望の出力: 9x^6 + 4x^5 + 5x^2

答えて

0

リストは単独でリンクされているため、逆方向にトラバースすることはできません。 しかし、それらを追加するのではなく、その前に追加することができます。最初の '+'を取り除くには、第1項が正の場合は部分文字列を返します。

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