Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 多项式链接列表格式_Java_Formatting_Linkedin_Polynomial Math_Polynomials - Fatal编程技术网

Java 多项式链接列表格式

Java 多项式链接列表格式,java,formatting,linkedin,polynomial-math,polynomials,Java,Formatting,Linkedin,Polynomial Math,Polynomials,我被要求使用LinkedList创建一个程序,它表示由一个或多个不同术语组成的多项式。大多数事情似乎都在工作,但我有一些问题,如何得到多项式的格式,我希望打印时 我的多项式应该是按降序排列的,但它们是按升序排列的。另外,当多项式被打印出来时,我需要以某种方式去除整个多项式前面的第一个“+”符号,而不会引起每个项之间剩余“+”符号的问题 学期班 public class Term { private DecimalFormat formatHelper = new DecimalFormat("

我被要求使用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));

   }
公共类多项式{
私人双重测试=0.0005;
私人任期负责人;
公共多项式(){
head=null;
}
/**
*使用指定的系数和指数向当前多项式添加一个项
*/
公共void addTerm(int exp,int coeff){
if(数学绝对值(系数)<测试)
返回;
if(head==null | | expcurrent.getExp()){
last=当前值;
current=current.getNext();
}
if(current==null | | exp!=current.getExp())
last.setNext(新术语(exp、coeff、current));
否则{
current.coeff+=系数;
if(数学绝对值(电流系数)<测试)
if(last!=null)
last.setNext(current.getNext());
其他的
head=head.getNext();
}
}
/**
*设置多项式的格式
*/
公共字符串toString(){
StringBuffer=新的StringBuffer();
for(Term=head;Term!=null;Term=Term.getNext())
如果(项系数<0)
append(“-”+term.toString());
其他的
append(“+”+term.toString());
返回buffer.toString();
}
/**
*额外积分-添加两个多项式
*/
公共多项式加法(多项式p2){
多项式答案=克隆();
for(Term=p2.head;Term!=null;Term=Term.getNext())
addTerm(term.getExp(),term.coeff);
返回答案;
}
/**
*仅用于额外积分的特殊方法,有助于添加多项式
*/
公共多项式克隆(){
多项式答案=新多项式();
for(Term=head;Term!=null;Term=Term.getNext())
addTerm(term.getExp(),term.coeff);
返回答案;
}
测试级
公共课程计划7{
公共静态void main(字符串[]args)
{
多项式p1=新多项式();
多项式p2=新多项式();
扫描仪键盘=新扫描仪(System.in);
内系数选择;
整数选择;
字符串userInput=“”;
字符串userInput2=“”;
而(!userInput.equalsIgnoreCase(“否”)){
System.out.println(“请输入当前项的系数:”);
coeffChoice=keyboard.nextInt();
System.out.println(“请输入当前术语的指数:”);
expChoice=keyboard.nextInt();
p1.添加术语(expChoice,coefffchoice);
System.out.println(“您想在多项式中添加一个项吗?”);
userInput=keyboard.next();
}
System.out.println(“开始构建第二个多项式的时间!”);
而(!userInput2.equalsIgnoreCase(“否”)){
System.out.println(“请输入当前项的系数:”);
coeffChoice=keyboard.nextInt();
System.out.println(“请输入当前术语的指数:”);
expChoice=keyboard.nextInt();
p2.添加术语(expChoice,coefffchoice);
System.out.println(“您想在多项式中添加一个项吗?”);
userInput2=keyboard.next();
}
系统输出println(“多项式1”);
System.out.println(p1.toString());
System.out.println();
System.out.println(“多项式2”);
System.out.println(p2.toString());
System.out.println();
System.out.println(“多项式加法”);
系统输出打印LN(p1.添加(p2));
}
示例输出

电流输出: +5x^2+4x^5+9x^6

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

由于列表是单独链接的,因此不能向后遍历。 但您可以在这些项之前加上前缀,而不是附加它们。若要删除第一个“+”,请在第一个项为正时返回一个子字符串:

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(); 
    }
}
公共字符串toString(){
StringBuffer=新的StringBuffer();
for(Term=head;Term!=null;Term=Term.getNext()){
如果(项系数<0)
insert(0,“-”+term.toString());
其他的
insert(0,“+”+term.toString());
}
if(buffer.charAt(1)='+'){
返回缓冲区。子字符串(2);
否则{
返回buffer.toString();
}
}