Java 如何将字符串转换为多项式并进行加法或减法运算?
我必须将多项式表示为arrayslist。如何从如下所示的txt文件中获取输入Java 如何将字符串转换为多项式并进行加法或减法运算?,java,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;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;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;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);}
}
我能想到的最简单的解决方案是将系数存储在一个数组中,让数组的索引对应于
x
项的幂。因此,一系列:
{2,4,-1,1}
将转化为:
x^3-x^2+4x+2
然后,加法和减法只需在两个数组之间添加相应的索引,并将结果存储在一个新数组中。您还必须跟踪多项式的最高幂项,以便知道表示它的数组有多大。因此,一个顺序为
n
的多项式将有一个大小为n+1
的数组来表示它。以下是关于如何实现多项式的一个想法:
根据以下定义:
在数学中,多项式是由变量(或不定数)和系数组成的表达式,只涉及加法、减法、乘法和非负整数指数运算
因此,您可以从将问题简化为一个术语开始:
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
}
现在,创建一个多项式
类作为术语的列表
,并定义添加和删除术语的必要方法:
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...
*/
}
}
类多项式{
最后一个字符串变量;
列出术语;
公共多项式(字符串变量){
//这将允许您仅接受“X”或“Y”或仅包含此变量的术语
this.variable=变量;
terms=newarraylist();
}
公共无效添加(期限){
/*
实施这个。。。
*/
}
}
使用此基本模型,您可以提出更多的想法来增强设计。例如,
术语
可以实现可比
,以支持术语之间的比较,类似于多项式
和其他元素。多项式加法是如何工作的?
答案:-将相同幂的系数相加
因此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
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
SUM=6X^5+5X^3-2X^2+3X^1+4X^0
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
您可以将幂作为键和系数作为值存储在映射中,然后迭代映射并将系数添加到其值中
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;
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.Scanner;
多项式类{
/**
*@param args
*@抛出FileNotFoundException
*/
公共静态void main(字符串[]args)引发FileNotFoundException{
列表多项式=新的ArrayList();
File polyfile=新文件(“polyinput.txt”);
扫描仪读取=新扫描仪(polyfile);
while(read.hasNextLine()){
字符串行=read.nextLine();
String[]lineSpillted=LINE.split(“;”);
Map poynomial=new HashMap();
对于(int i=1;i2){
对于(int i=2;i很抱歉,变量名没有接近数学标准,也没有经过测试,但这应该给您留下一些想法
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;
}
}
import java.util.ArrayList;
公共级保利{
私有字符串[]个数字;
私有ArrayList func;
公共多边形(字符串多边形,双值){
数字=多边形拆分(“;”);
func=新的ArrayList();
对于(int i=1;i
这样,您就可以通过获取arraylist的索引来获取所需的变量和计算值,并发挥一些神奇的作用。这并不像您想象的那么容易。如果这是家庭作业,那么请创建自己的类来解析字符串并创建多项式的抽象表示。否则,请寻找一个处理该字符串的库s、 我试着把它们转换成字符串,但之后我就不知所措了。我把它们读入了,但在给它们定界后却不知道如何使用它们。我怎么能用一个未定义的变量来做数学运算呢?然后我可能要加上或减去所有单独的数字?我只把它们读入了arraylist中,然后把它们放在arraylist中。我如何做表示呢?Can给定一个参数,立即替换“x”?我不知道。=你的意思是什么,你做的数组如何转化为它?你将数组中的数字乘以数组中该数字的索引幂。例如,数组中的-1
的索引为2
,因此它变成-1*x^2
,或者简单地说-x^2
@Dave他的第一个数组元素是最后一个系数(2x^0
)。我希望他选择了这个