如何将字符串转换为多项式并加上或减去它们?

时间:2014-08-30 15:40:36

标签: java

我必须将多项式表示为arrayslist。如何从类似于此的txt文件中获取输入

P1;5;3;-4;1;8;0
P2;6;5;-2;2;7;1;-4;0

并将其转换为看起来像这样的多项式

P1(X) = 5X^3 –4X +8
P2(X) = 6X^5 -2X^2 +7X -4.

我怎样才能解决这两个多项式之间的加法和减法问题?例如P1 + P2

这就是我所拥有的:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;


public class PolyProcessor {
    static int polyNum = 0;
public static void main(String[] args) throws FileNotFoundException{

    PolyCalc c = new PolyCalc();
    File polyfile = new File("polyinput.txt");
    Scanner read = new Scanner(polyfile);
    while (read.hasNextLine()){
        String j = read.nextLine();
        c.add(j);
        }


    }
    }


class PolyCalc{
    static int polyCount = 0;

    static ArrayList polynomials = new ArrayList();

     static void add(String j){

         polynomials.add(j);
         polyCount++;}

     static Object get(int i){
         return polynomials.get(i);}


    }

4 个答案:

答案 0 :(得分:3)

多项式加法如何工作?

答案: - 通过添加相同功率的系数

SO P1 = 5X ^ 3 - 4X + 8

且P2 = 6X ^ 5 -2X ^ 2 + 7X ^ 1 + -4

变为

P1 = 0X ^ 5 + 5X ^ 3 + 0X ^ 2 - 4X ^ 1 + 8X ^ 0

P2 = 6X ^ 5 + 0X ^ 3 -2X ^ 2 + 7X ^ 1 - 4X ^ 0

<强> ____________________________________

SUM = 6X ^ 5 + 5X ^ 3 -2X ^ 2 + 3X ^ 1 + 4X ^ 0

<强> ____________________________________

您可以将权力存储为Key,将系数作为值存储在Map中。然后迭代地图并在其值中添加系数

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

class SumOfPolynomials {

/**
 * @param args
 * @throws FileNotFoundException 
 */
public static void main(String[] args) throws FileNotFoundException {

     List<Map<Integer, Integer>> listOfPolynomials = new ArrayList<Map<Integer, Integer>>();
    File polyfile = new File("polyinput.txt");
    Scanner read = new Scanner(polyfile);
    while (read.hasNextLine()){
        String LINE = read.nextLine();
        String[] lineSpillted =LINE.split(";");
        Map<Integer, Integer> poynomial = new HashMap<Integer, Integer>();
        for(int i =1;i<lineSpillted.length-1;i=i+2){                //i starts from ignores P1,P2 etc

             poynomial.put(Integer.parseInt(lineSpillted[i+1]), Integer.parseInt(lineSpillted[i]));

        }
        listOfPolynomials.add(poynomial);
        }

    read.close();

    Map<Integer, Integer> result = polynomialSum(listOfPolynomials.get(0), listOfPolynomials.get(1));

    if(listOfPolynomials.size()>2){

        for(int i=2;i<listOfPolynomials.size()-1;i++){

            result = polynomialSum(result,listOfPolynomials.get(i));
        }
    }
    // print out the SUM as VALUEX^KEY
    System.out.println();
    int c = 0;
    for (Map.Entry<Integer, Integer> entry : result.entrySet()) {

        System.out.print(entry.getValue() + "X^" + entry.getKey());
        c++;
        if (c != result.size()) {
            System.out.print("+");
        }
    }

}

public static Map<Integer, Integer> polynomialSum(Map<Integer, Integer> arg1,
        Map<Integer, Integer> arg2) {

    Map<Integer, Integer> SUM = new HashMap<Integer, Integer>();

    for (Map.Entry<Integer, Integer> entry : arg1.entrySet()) {

        Integer power = entry.getKey();
        Integer coeff1 = entry.getValue();
        Integer coefficient;
        if (arg2.containsKey(power)) {
            coefficient = arg2.get(power) + coeff1;
        } else {
            coefficient = coeff1;
        }
        SUM.put(power, coefficient);
    }

    for (Map.Entry<Integer, Integer> entry : arg2.entrySet()) {

        if (SUM.containsKey(entry.getKey())) {
            continue;
        } else {
            SUM.put(entry.getKey(), entry.getValue());
        }

    }

    return SUM;
}

}

已编辑多个多项式。多个多项式在列表中添加,然后通过迭代列表计算总和

<强>输出: -

Output

答案 1 :(得分:1)

以下是关于如何实现多项式的想法:

基于polynomial的定义:

  

在数学中,多项式是由变量(或不确定)和系数组成的表达式,它只涉及加法,减法,乘法和非负整数指数的运算。

因此,您可以将问题缩减为术语:

class Term {
    //making it immutable
    final double power;
    final double coefficient;
    final String variable;
    //constructor
    public Term(double power, double coefficient, String variable) {
        //assign variables and such
        this.power = power;
        //...
    }
    //getters for your class
}

现在,创建一个Polynomial类作为List个术语,并定义添加和删除术语所需的方法:

class Polynomial {
    final String variable;
    List<Term> terms;
    public Polynomial(String variable) {
        //this will allow you to accept only "X" or "Y" or terms with this variable only
        this.variable = variable;
        terms = new ArrayList<Terms>();
    }
    public void add(Term term) {
        /*
            implement this...
        */
    }
}

通过这个基本模型,您可以提出更多想法来增强设计。例如,Term可以实施Comparable<Term>以支持术语之间的比较,类似于Polynomial和其他元素。

答案 2 :(得分:0)

我能想到的最简单的解决方案是将系数存储在数组中,并让数组的索引对应x项上的幂。所以数组:

{2, 4, -1, 1}

会转换为:

x^3 - x^2 + 4x + 2

然后添加和减去只是在两个数组之间添加相应的索引,并将结果存储在新数组中。您还必须跟踪多项式的最高幂项,以便了解如何使用代表它的数组。因此,订单n的多项式将具有大小为n + 1的数组来表示它。

答案 3 :(得分:0)

很抱歉变量名称没有接近数学标准,也没有经过测试,但这应该会让你有点想法。

import java.util.ArrayList;
public class Poly {

    private String[] numbers;
    private ArrayList<Variable> func;

    public Poly(String poly, double valueOfX) {
        numbers = poly.split(";");
        func = new ArrayList<>();
        for (int i = 1; i < numbers.length - 1; i+=2) {
            double exp = (numbers[i+1] == "0") ? 1 : Double.parseDouble(numbers[i++]); 
            double x = (numbers[i+1] == "0") ? 1 : valueOfX; 
            func.add(new Variable(Double.parseDouble(numbers[i]), exp, x));
        }
    }

    public ArrayList<Variable> getFunc() {
        return func;
    }

}
public class Variable {

    private double value;
    private double exponent;
    private double x;

    public Variable(double value, double exponent, double x) {
        this.value = value;
        this.exponent = exponent;
        this.x = x;
    }

    public double getValue() {
        return value;
    }

    public double getExponent() {
        return exponent;
    }

    public double getX() {
        return x;
    }
}

由此,您可以获得所需的变量,并通过获取arraylist的索引来计算值,并且可以起到一定的作用。