繁体   English   中英

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

[英]How to convert Strings into polynomials and adding or subtracting them?

我必须将多项式表示为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);}


    }

多项式加法如何工作?

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

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

}

EDITED for multiple Polynomials.Multiple Polynomials在List中添加,然后sum通过迭代List计算

输出: -

产量

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

基于多项式的定义:

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

因此,您可以从将问题减少到一个术语开始:

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和其他元素。

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

{2, 4, -1, 1}

将翻译为:

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

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

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

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的索引来计算值,并使用一些魔法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM