多项式链表格式

Polynomial LinkedList formatting

有人要求我使用 LinkedList 创建一个程序,该 LinkedList 表示由一个或多个不同项组成的多项式。大多数一切似乎都在工作,但是我在将生成的多项式格式化为我想要的打印方式时遇到了一些问题。

我的多项式应该按降序排列,但正在按升序打印。此外,当打印多项式时,我需要以某种方式摆脱整个多项式前面的第一个“+”号,而不会对每个项之间的其余“+”号造成问题。

学期Class

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

多项式Class

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 + 4x^5 +9x^6

期望的输出: 9x^6 + 4x^5 + 5x^2

由于您的列表是单链接的,因此您不能向后遍历它。 但是您可以在条款前面加上条款而不是附加条款。要去掉第一个 '+',return 如果第一个项是正数,一个子串:

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