关于打印多项式

时间:2016-09-28 23:10:45

标签: java

这是存储和评估输入数字多项式的类的一部分 这是方法

public class Polynomial{
int coef,power;

public Polynomial(int maxPower){
}

public void setTerm(int coefficient, int power) {
    this.coef = coefficient;
    this.power = power;
}

主方法中的输入参数

public static void main(String[] args){
    Polynomial q = new Polynomial(2);
    q.setTerm(2, 0);
    q.setTerm(0, 1);
    q.setTerm(3, 2);
    System.out.println(q);
}

并且有一个toString方法来打印多项式,如下所示

public String toString(){
    return 
}

我一直遇到setTerm方法只读取最后输入参数q.setTerm(3,2)的问题,我认为前两个被替换了。我该怎么办?

3 个答案:

答案 0 :(得分:2)

因此,在您的示例中,我们必须记住之前看到的术语。

我将通过地图如下进行。

import java.util.HashMap;
import java.util.Map;

/**
 * Created by arunavs on 9/28/16.
 */
public class Polynomial {

    // Key is power, value is coeff.
    private Map<Integer, Integer> powerToCoeffMap = new HashMap<>();

    // Maximal power allowed for polynomial.
    private int maxPower;


    public Polynomial(int maxPower){
        this.maxPower = maxPower;
    }

    public void setTerm(int coefficient, int power) {
        if (power > maxPower) {
           // throw some exception here. 
        }
        powerToCoeffMap.put(power, coefficient);
    }

    // terms may be rearranged in output. 
    public String toString() {
        StringBuilder builder = new StringBuilder();
        powerToCoeffMap.forEach((power, coeff) -> {builder.append(coeff + "x^" + power+ " ");});
        return builder.toString();
     }

        public static void main(String args[]) {
            Polynomial q = new Polynomial(2);
            q.setTerm(2, 0);
            q.setTerm(0, 1);
            q.setTerm(3, 2);
        System.out.println(q);
    }
}

输出:2x ^ 0 0x ^ 1 3x ^ 2

我希望这会有所帮助。

答案 1 :(得分:1)

您只存储了一个系数和一个电源。 最简单的是有一系列由幂索引的系数。

public class Polynomial {

    final int[] coefficients;

    public Polynomial(int maxPower) {
        coefficients = new int[maxPower + 1]; // All zero.
    }

    public void setTerm(int coefficient, int power) {
        coefficients[power] = coefficient;
    }

    @Override
    public String toString() {
        StringBuilder sb = newStringBuilder();
        for (int power = 0; power < coefficients.length; ++power) {
            if (coefficients[power] != 0) {
                if (coefficients[power] > 0 && sb.length() > 0) {
                    sb.append('+');
                }
                sb.append(coefficients[power]);
                if (power > 0) {
                    sb.append('x');
                    if (power > 1) {
                        sb.append('^').append(power);
                    }
                }
            }
        }
        return sb.toString();
    }
}

答案 2 :(得分:0)

另一种可能同时添加多个术语的实现可能如下所示:

package de;

import java.util.TreeMap;

public class Polynomial {
    //Private Fields
    private TreeMap<Integer, Integer> polys = new TreeMap<>();

    public Polynomial() {

    }
    public Polynomial(int... args){
        if(args.length < 2) polys.put(0, args[0]);

        for(int i= 0; i < args.length && i+1 < args.length; i = i+2){
            //                        coeff                      power
        polys.put(Integer.valueOf(args[i+1]), Integer.valueOf(args[i]));
        }
        if((args.length % 2) != 0){
            polys.put(0, args[args.length-1]);
        }
    }
    public void setTerm(int coeff, int power){
        polys.put(Integer.valueOf(power),Integer.valueOf(coeff));
    }

    public void setTerms(int... args){
        if(args.length < 2) polys.put(0, args[0]);

        for(int i= 0; i < args.length && i+1 < args.length; i = i+2){
            //                        coeff                      power
        polys.put(Integer.valueOf(args[i+1]), Integer.valueOf(args[i]));
        }
        if((args.length % 2) != 0){
            polys.put(0, args[args.length-1]);
        }
    }
    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        for(Integer key : polys.descendingKeySet()){
            Integer value = polys.get(key);
            if(value == 0) continue;
            if(value > 0 && sb.length() != 0) sb.append("+");

            sb.append(value.toString());
            if(key == 1) sb.append("x");
            if(key > 1) sb.append("x^" + key);

            }
            return sb.toString();
        }


    public static void main(String[] args) {
        Polynomial p = new Polynomial(1, 1, -2, 2, -3, 3, 7, 0);
        p.setTerm(17, 5);
        p.setTerms(23, 4, 0, 6);
        System.out.println(p.toString());
    }
}

输出:17x ^ 5 + 23x ^ 4-3x ^ 3-2x ^ 2 + 1x + 7

它从最高功率到最低功率排序,打印x而不是x ^ 1并且不打印x ^ 0.