请你用java语言编写一个一元多项式计算器,一元多项式要从键盘录入或从文件中读取系数、指数数据,要求程序能够能够正确浏览各多项式,计算任意两个一元多项式的加法、减法以及乘法,保留原多项式结果
时间: 2024-03-10 09:44:58 浏览: 56
好的,我可以为您提供一份Java代码,实现一元多项式计算器的功能。请注意,代码中包含读取文件的功能,您可以根据需要自行修改。
```java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class PolynomialCalculator {
public static void main(String[] args) throws IOException {
ArrayList<Term> p1 = readPolynomialFromFile("polynomial1.txt");
ArrayList<Term> p2 = readPolynomialFromFile("polynomial2.txt");
System.out.println("Polynomial 1: " + polynomialToString(p1));
System.out.println("Polynomial 2: " + polynomialToString(p2));
ArrayList<Term> pAdd = addPolynomials(p1, p2);
ArrayList<Term> pSub = subtractPolynomials(p1, p2);
ArrayList<Term> pMul = multiplyPolynomials(p1, p2);
System.out.println("Addition: " + polynomialToString(pAdd));
System.out.println("Subtraction: " + polynomialToString(pSub));
System.out.println("Multiplication: " + polynomialToString(pMul));
}
public static ArrayList<Term> readPolynomialFromFile(String fileName) throws IOException {
ArrayList<Term> polynomial = new ArrayList<>();
File file = new File(fileName);
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(" ");
polynomial.add(new Term(Integer.parseInt(parts[0]), Integer.parseInt(parts[1])));
}
br.close();
// sort the terms in descending order of exponents
Collections.sort(polynomial, new Comparator<Term>() {
@Override
public int compare(Term t1, Term t2) {
return t2.exponent - t1.exponent;
}
});
return polynomial;
}
public static ArrayList<Term> addPolynomials(ArrayList<Term> p1, ArrayList<Term> p2) {
ArrayList<Term> result = new ArrayList<>();
int i = 0, j = 0;
while (i < p1.size() && j < p2.size()) {
Term t1 = p1.get(i), t2 = p2.get(j);
if (t1.exponent > t2.exponent) {
result.add(new Term(t1.coefficient, t1.exponent));
i++;
} else if (t1.exponent < t2.exponent) {
result.add(new Term(t2.coefficient, t2.exponent));
j++;
} else {
result.add(new Term(t1.coefficient + t2.coefficient, t1.exponent));
i++;
j++;
}
}
while (i < p1.size()) {
result.add(new Term(p1.get(i).coefficient, p1.get(i).exponent));
i++;
}
while (j < p2.size()) {
result.add(new Term(p2.get(j).coefficient, p2.get(j).exponent));
j++;
}
return result;
}
public static ArrayList<Term> subtractPolynomials(ArrayList<Term> p1, ArrayList<Term> p2) {
ArrayList<Term> result = new ArrayList<>();
int i = 0, j = 0;
while (i < p1.size() && j < p2.size()) {
Term t1 = p1.get(i), t2 = p2.get(j);
if (t1.exponent > t2.exponent) {
result.add(new Term(t1.coefficient, t1.exponent));
i++;
} else if (t1.exponent < t2.exponent) {
result.add(new Term(-t2.coefficient, t2.exponent));
j++;
} else {
result.add(new Term(t1.coefficient - t2.coefficient, t1.exponent));
i++;
j++;
}
}
while (i < p1.size()) {
result.add(new Term(p1.get(i).coefficient, p1.get(i).exponent));
i++;
}
while (j < p2.size()) {
result.add(new Term(-p2.get(j).coefficient, p2.get(j).exponent));
j++;
}
return result;
}
public static ArrayList<Term> multiplyPolynomials(ArrayList<Term> p1, ArrayList<Term> p2) {
ArrayList<Term> result = new ArrayList<>();
for (Term t1 : p1) {
for (Term t2 : p2) {
int c = t1.coefficient * t2.coefficient;
int e = t1.exponent + t2.exponent;
insertTerm(result, new Term(c, e));
}
}
return result;
}
public static void insertTerm(ArrayList<Term> polynomial, Term term) {
int i;
for (i = 0; i < polynomial.size(); i++) {
if (polynomial.get(i).exponent < term.exponent) {
break;
}
}
polynomial.add(i, term);
}
public static String polynomialToString(ArrayList<Term> polynomial) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < polynomial.size(); i++) {
Term t = polynomial.get(i);
if (t.coefficient > 0 && i > 0) {
sb.append("+");
}
sb.append(t.coefficient);
sb.append("x^");
sb.append(t.exponent);
}
return sb.toString();
}
static class Term {
int coefficient;
int exponent;
public Term(int coefficient, int exponent) {
this.coefficient = coefficient;
this.exponent = exponent;
}
}
}
```
代码中的 `Term` 类表示一项,包含系数和指数两个属性。`readPolynomialFromFile` 方法用于从文件中读取多项式数据,并按指数从高到低排序。`addPolynomials`、`subtractPolynomials`、`multiplyPolynomials` 分别实现多项式的加、减、乘运算。`insertTerm` 方法用于将一项插入到多项式中的正确位置。`polynomialToString` 方法将多项式转换为字符串表示,便于输出。
您可以根据需要修改文件名以及多项式的具体数据。
阅读全文