单变量多项式(来自文本文件)

时间:2011-11-26 03:56:44

标签: java jscience

我想修改下面的代码来从文本文件中读取节点(与硬编码值相比)

此外,从文本文件中读取数据的格式为:

P1 = 3 5 1 -1 0 8

P2 = 5 6 2 -1 1 7 0 -4

等等......

将值P(x)命名并输入剩余数据。有什么建议吗?

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

1 个答案:

答案 0 :(得分:1)

除非您的数据已经是二进制形式,否则我不会使用DataInputStream。以下示例使用BufferedReader撰写org.jscience.mathematics.function.Polynomial

请注意,最高阶系数首先是为了方便应用Horner's scheme,如此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]

代码:

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