I want to modify the below code to read the nodes from a text file (vs. hard-coded values)
Furthermore, read data from text file in the format:
P1 = 3 5 1 -1 0 8
P2 = 5 6 2 -1 1 7 0 -4
etc...
Name the values P(x) and input the remaining data. Any advice?
import java.io.DataInputStream;
import java.util.LinkedList;
import java.util.ListIterator;
// A term contains the coefficient and the exponent of x.
class Term {
public Term(int c, int d) {
coeff = c;
degree = d;
}
public Term multiply(Term other) {
return new Term(coeff * other.coeff, degree + other.degree);
}
public void add(int c) {
coeff += c;
}
public void sub(int c) {
coeff -= c;
}
public int getCoeff() {
return coeff;
}
public int getDegree() {
return degree;
}
private int coeff;
private int degree;
}
// A polynomial made up of terms.
class Polynomial {
// Construct a Polynomial object.
public Polynomial() {
terms = new LinkedList();
}
public Polynomial add(Polynomial p) {
Polynomial r = new Polynomial();
ListIterator iterator = terms.listIterator();
while (iterator.hasNext()) {
r.add((Term) iterator.next());
}
ListIterator pIterator = p.terms.listIterator();
while (pIterator.hasNext()) {
r.add((Term) pIterator.next());
}
return r;
}
public Polynomial sub(Polynomial p) {
Polynomial r = new Polynomial();
ListIterator iterator = terms.listIterator();
while (iterator.hasNext()) {
r.sub((Term) iterator.next());
}
ListIterator pIterator = p.terms.listIterator();
while (pIterator.hasNext()) {
r.sub((Term) pIterator.next());
}
return r;
}
public Polynomial multiply(Polynomial p) {
Polynomial r = new Polynomial();
ListIterator iterator = terms.listIterator();
while (iterator.hasNext()) {
ListIterator pIterator = p.terms.listIterator();
Term t1 = (Term) iterator.next();
while (pIterator.hasNext()) {
Term t2 = (Term) pIterator.next();
r.add(t1.multiply(t2));
}
}
return r;
}
// Adds a coefficient and degree as a new Term.
public void addTerm(int c, int d) {
add(new Term(c, d));
}
// Adds a term
public void add(Term t) {
int c = t.getCoeff();
int d = t.getDegree();
ListIterator iterator = terms.listIterator();
while (iterator.hasNext()) {
Term current = (Term) iterator.next();
if (d == current.getDegree()) {
if (c == -current.getCoeff())
iterator.remove();
else
current.add(c);
return;
} else if (d < current.getDegree()) {
iterator.previous();
iterator.add(t);
return;
}
}
iterator.add(t);
}
public void sub(Term t) {
int c = t.getCoeff();
int d = t.getDegree();
ListIterator iterator = terms.listIterator();
while (iterator.hasNext()) {
Term current = (Term) iterator.next();
if (d == current.getDegree()) {
if (c == current.getCoeff())
iterator.remove();
else
current.add(-c);
return;
} else if (d < current.getDegree()) {
iterator.previous();
iterator.add(t);
current.add(-c);
return;
}
}
iterator.add(t);
}
// Prints the polynomial.
public void print() {
ListIterator iterator = terms.listIterator();
while (iterator.hasNext()) {
Term current = (Term) iterator.next();
if (current.getCoeff() != 0) {
if ((current.getCoeff() < 0) || (!(current.getDegree() != 0))) {
System.out.print(" ");
}
else {
System.out.print(" + ");
}
System.out.print(current.getCoeff());
if (current.getDegree() > 0) {
System.out.print("x");
if (current.getDegree() > 1)
System.out.print("^" + current.getDegree());
}
}
}
System.out.println();
}
@SuppressWarnings("deprecation")
public char getChar() {
char val = ' ';
String buffer = "";
DataInputStream ds = new DataInputStream(System.in);
try {
buffer = ds.readLine();
val = buffer.charAt(0);
} catch (Exception e) {
}
return (val);
}
public char enterNext() {
System.out.println("Enter next Polynomial? ");
char ans = this.getChar();
switch (ans) {
case 'n':
case 'N':
System.out.println("All Done.");
System.exit(0);
break;
case 'y':
case 'Y':
System.out.println("Enter next one, please!");
}
return ans;
}
private LinkedList terms;
}
// A test program for Polynomial and Term classes.
public class RPC {
private static char ans = 'y';
public static void main(String[] args) {
Polynomial p = new Polynomial();
do
{
p.addTerm(10, 0);
p.addTerm(-1, 1);
p.addTerm(9, 7);
p.addTerm(5, 10);
System.out.print("f(x) = ");
p.print();
System.out.println();
Polynomial q = new Polynomial();
do
{
q.addTerm(1, 0);
q.addTerm(-11, 1);
q.addTerm(19, 7);
q.addTerm(15, 10);
System.out.print("q(x) = ");
q.print();
p.enterNext();
}
while (!(ans != 'y') | !(ans != 'Y'));
System.out.println();
Polynomial a = p.add(q);
System.out.print("f(x) + q(x) = ");
a.print();
Polynomial s = p.sub(p);
s.print();
Polynomial m = p.multiply(q);
System.out.print("f(x) * q(x) = ");
m.print();
System.exit(0);
} while (ans != 1000);
}
}
Unless your data is already in binary form, I wouldn't use DataInputStream
. The example below uses BufferedReader
to compose org.jscience.mathematics.function.Polynomial
.
Note that the highest order coefficient is first for convenience in applying Horner's scheme, as shown in this example.
1 2 3 4 4 3 2 1
Coefficient: 1 2 3 4 Polynomial: [1]x³ + [2]x² + [3]x + [4] Coefficient: 4 3 2 1 Polynomial: [4]x³ + [3]x² + [2]x + [1]
Code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.jscience.mathematics.function.Polynomial;
import org.jscience.mathematics.function.Term;
import org.jscience.mathematics.function.Variable;
import org.jscience.mathematics.number.Integer64;
/** @see http://stackoverflow.com/questions/8276150 */
public class ReadPoly {
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new FileReader("test.txt"));
String s;
while ((s = r.readLine()) != null) {
System.out.println("Coefficient: " + s);
Polynomial<Integer64> p = create(s.split(" "));
System.out.println("Polynomial: " + p);
}
}
public static Polynomial<Integer64> create(String... a) {
Variable<Integer64> x = new Variable.Local<Integer64>("x");
Polynomial<Integer64> px = Polynomial.valueOf(Integer64.ZERO, x);
for (int i = 0, e = a.length - 1; i < a.length; i++, e--) {
px = px.plus(Polynomial.valueOf(
Integer64.valueOf(a[i]), Term.valueOf(x, e)));
}
return px;
}
}
来源:https://stackoverflow.com/questions/8276150/single-variable-polynomial-from-text-file