多项式LinkedList格式

时间:2017-04-23 17:28:27

标签: java formatting linkedin polynomial-math polynomials

我被要求使用LinkedList创建一个程序,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 + 4x ^ 5 + 9x ^ 6

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

1 个答案:

答案 0 :(得分: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(); 
    }
}