代码如下:Polynomial.java:
/**
* Univariate polynomials with integer coefficients represented
* as circular linked lists with head nodes. Nodes that
* represent terms with higher powers of the variable appear
* earlier in the list than do nodes that represent terms with
* lower powers of the variable.
*/import java.io.*;
import java.util.*; // for the use of Stack class
//import java.lang.Object;
import dataStructures.PolyNode;public class CircularPolynomial{
// data members of CircularPolynomial
protected PolyNode headNode;
protected PolyNode lastNode; // constructor
/** create the zero polynomial */
public CircularPolynomial(){
headNode = new PolyNode();
lastNode = new PolyNode();
} /**
* degree
* @return polynomial degree */
public int degree(){
return (this.headNode.next().getExp());
} /**
* clone
* make a clone; a separate but identical object */
public Object clone(){
CircularPolynomial p = new CircularPolynomial();
// Use tmpNode as the pioneer to test if attain at the end of the polynomial;
PolyNode tmpNode = new PolyNode();
tmpNode.setCoeff(this.headNode.getCoeff());
tmpNode.setExp(this.headNode.getExp());
tmpNode.setNext(this.headNode.next());
while(tmpNode.next().getExp() != -1){ // Have not attain the endNode yet,
tmpNode.setCoeff(tmpNode.next().getCoeff());
tmpNode.setExp(tmpNode.next().getExp());
tmpNode.setNext(tmpNode.next().next());
p.append(tmpNode.getCoeff(), tmpNode.getExp());
}
// this lastNode should be the copy of remnant tmpNode,
// the 'next' domain of it would be the headNode of it.
p.lastNode.setCoeff(tmpNode.getCoeff());
p.lastNode.setExp(tmpNode.getExp());
p.lastNode.setNext(p.headNode);
return p;
} /**
* append
* add a new term to the end of the list
* (this method is complete)*/
private void append(double theCoeff, int theExp){
lastNode.setNext(new PolyNode(theCoeff, theExp, headNode));
lastNode = lastNode.next();
} /**
* input
* input the polynomial from the standard input stream
* (this method is complete) */
public void input() throws Exception {
BufferedReader keyboard = new BufferedReader(
new InputStreamReader(System.in),1);
// input number of terms
System.out.print("Enter the number of non-zero terms: ");
int terms = Integer.parseInt(keyboard.readLine());
if (terms < 0){
System.out.println("The number of terms must be >= 0; " +
"you entered " + terms);
System.exit(-1);
}
if (terms > 0){
// at least one nonzero term, input the nonzero terms
// in decreasing order of exponents
System.out.println("\nEnter the nonzero terms "
+ "in decreasing order of exponents.\n"
+ "For example, for the polynomial 2x^7 - 3x^4 + ... "+
"+ x + 2,\n"
+ "give the input as a sequence 2, 7, -3, 4, ..., "+
"1, 1, 2, 0.\n"
);
// get first term
System.out.print("Coefficient: ");
double coefficient = Double.valueOf(keyboard.readLine()).doubleValue();
System.out.print("Exponent: ");
int exponent = Integer.parseInt(keyboard.readLine());
// exponent must be >= 0 and coefficient must be nonzero
if (exponent < 0 || coefficient == 0){
System.out.println("Exponent must be >= 0 and "+
"coefficient must not equal 0. "+
"They are exponent = " + exponent +
", coefficient = " + coefficient);
System.exit(-1);
}
this.append(coefficient, exponent);
int lastExponent = exponent;//to check for input errors
// get the remaining terms
for (int i = 2; i <= terms; i++){
// get next term
System.out.print("Coefficient: ");
coefficient = Double.valueOf(keyboard.readLine()).doubleValue();
System.out.print("Exponent: ");
exponent = Integer.parseInt(keyboard.readLine());
// exponent must be < lastExponent and
//coefficient must be nonzero
if (exponent >= lastExponent || coefficient == 0){
System.out.println("Enter exponents in descending"+
" order and coefficients must not equal 0.");
System.exit(-1);
}
this.append(coefficient, exponent);
lastExponent = exponent;
}
}
}
/**
* toString
* a String representation of the polynomial */
public String toString(){
PolyNode tmpNode = new PolyNode();
tmpNode.setNext(this.headNode.next()); String str = new String();
while( tmpNode.next().getExp() != -1){
// I don't know the function to control the format of a double or an integer;
if(tmpNode.next().getExp() == 1){
str += tmpNode.next().getCoeff() + "X +" ;
}
else if(tmpNode.next().getExp() == 0){
str += tmpNode.next().getCoeff();
}
else {
str += tmpNode.next().getCoeff() + "X^" + tmpNode.next().getExp() + "+";
}
tmpNode.setNext(tmpNode.next().next());
}
str = "f(x) = "+ str;
return str;
}
/**
* add
* @return this + b */
public CircularPolynomial add(CircularPolynomial b){
CircularPolynomial sum = new CircularPolynomial();
PolyNode na = new PolyNode(this.headNode.getCoeff(),this.headNode.getExp(),this.headNode.next());
PolyNode nb = new PolyNode(b.headNode.getCoeff(),b.headNode.getExp(),b.headNode.next()); //If all the two CircularPolynomial have not been added out.(HAI MEI YOU JIA WAN)
while (na.next().getExp()!=-1 && nb.next().getExp()!=-1){
if ( na.next().getExp() > nb.next().getExp() ){
sum.append(na.next().getCoeff(), na.next().getExp());
na.setNext(na.next().next());
}
else if ( na.next().getExp()== nb.next().getExp()){
sum.append((na.next().getCoeff() + nb.next().getCoeff()), na.next().getExp());
na.setNext(na.next().next());
nb.setNext(nb.next().next());
}
else if ( na.next().getExp() < nb.next().getExp() ){
sum.append(nb.next().getCoeff(), nb.next().getExp());
nb.setNext(nb.next().next());
}
} // If there would be some Polynomial which has not been added,
// append them directly to the end of the sum;
if(na.next().getExp() != -1){ // nb has added out
while (na.next().getExp() != -1){
sum.append(na.next().getCoeff(), na.next().getExp());
na.setNext(na.next().next());
}
sum.lastNode.setCoeff(na.getCoeff());
sum.lastNode.setExp(na.getExp());
sum.lastNode.setNext(this.headNode);
} if(nb.next().getExp()!=-1){
while (nb.next().getExp()!=-1){
sum.append(nb.next().getCoeff(), nb.next().getExp());
nb.setNext(nb.next().next());
}
sum.lastNode.setCoeff(nb.getCoeff());
sum.lastNode.setExp(nb.getExp());
sum.lastNode.setNext(b.headNode);
}
return sum;
}
/**
* subtract
* The code for this method would be essentially
* identical to that for the add method.
* A different approach will be used here (for variety);
* this method will compute this + (-b) by calling the
* add method. This implementation, then, has a greater
* time complexity than it otherwise would. The code,
* however, is brief.
* @return this - b
*/
public CircularPolynomial subtract(CircularPolynomial b){
CircularPolynomial bb = new CircularPolynomial();
PolyNode nb = new PolyNode(b.headNode.getCoeff(),b.headNode.getExp(),b.headNode.next());
//According to the requirement that the coeffs of b be changed into negative
while(nb.next().getExp() != -1){
bb.append(-nb.next().getCoeff(), nb.next().getExp());
nb.setNext(nb.next().next());
}
return this.add(bb);
}
/**
* multiply
* @return w = this * b */
public CircularPolynomial multiply(CircularPolynomial b){
// Initialize two Polynomials: one as the final result, The other as the worker;
CircularPolynomial result = (CircularPolynomial)this.clone();
CircularPolynomial worker = (CircularPolynomial)this.clone();
// Initialize PolyNode 'n' to trace Polynomial b:
PolyNode n = new PolyNode(b.headNode.getCoeff(), b.headNode.getExp(), b.headNode.next());
while(n.next().getExp() != -1){
worker = this.simpleMultiply(n);
result = result.add(worker);
n.setCoeff(n.next().getCoeff());
n.setExp(n.next().getExp());
n.setNext(n.next().next());
}
return result;
} /**
* simple multiply
* This auxiliary multiplication method multiply a polynomial with a PolyNode,
* and return the resulting polynomial.
* @return w = this * n */
public CircularPolynomial simpleMultiply(PolyNode n){
CircularPolynomial w = new CircularPolynomial();
PolyNode na = new PolyNode(this.headNode.getCoeff(),this.headNode.getExp(),this.headNode.next());
// initialize coeff and exp so as to save time;
double coeff = n.getCoeff();
int exp = n.getExp();
while (na.next().getExp() != -1){
na.setCoeff(na.next().getCoeff() * coeff);
na.setExp(na.next().getExp() + exp);
w.append(na.getCoeff(), na.getExp());
na.setNext(na.next().next());
}
w.lastNode.setCoeff(na.getCoeff());
w.lastNode.setExp(na.getExp());
w.lastNode.setNext(w.headNode);
return w;
} /**
* valueAt
* @return polynomial value at x, rounded to 2 decimal places */
public double valueAt(double x){
// We can use two stacks to save the coeffs of 'this' Polynomial
Stack CoeffStack = new Stack();
Stack ExpStack = new Stack(); PolyNode na = new PolyNode(this.headNode.getCoeff(),this.headNode.getExp(),this.headNode.next()); while (na.next().getExp() != -1){
CoeffStack.push(new Double(na.next().getCoeff()));
ExpStack.push(new Integer(na.next().getExp()));
na.setNext(na.next().next());
}
// 'worker' is the temp value and 'result' would get the final values
double worker = 1, result = 0;
int count, currentExp, prevExp = ExpStack.pop();
double prevCoeff = CoeffStack.pop(); // Initialize the manipulations:
if(prevExp == 0){
result = prevCoeff;
}else{
for (count = 0; count < prevExp; count ++){
worker *= x;
}
result = prevCoeff*worker;
}
// Go on processing
currentExp = ExpStack.pop();
while( !ExpStack.empty() ){
for (count = 0; count < currentExp - prevExp; count ++ ){
worker *= x;
}
result += CoeffStack.pop() * worker;
prevExp = currentExp;
currentExp = ExpStack.pop();
}
return result; }
/** test program */
public static void main(String [] args) throws Exception { CircularPolynomial p = new CircularPolynomial();
CircularPolynomial q = new CircularPolynomial(); p.input();
System.out.print("Polynomial p(x) = ");
System.out.println(p.toString() + "\n");
System.out.println("p(2) = " +p.valueAt(2) + "\n");
q.input();
System.out.print("Polynomial q(x) = ");
System.out.println(q.toString() + "\n");
System.out.println("q(-1) = " +q.valueAt(-1) + "\n"); System.out.print("(p + q)(x) = ");
System.out.println(p.add(q).toString() + "\n");
System.out.println("(p + q)(3) = " +
(p.add(q)).valueAt(3) + "\n");
System.out.print("(p - q)(x) = ");
System.out.println(p.subtract(q).toString() + "\n");
System.out.print("(p * q)(x) = ");
System.out.println(p.multiply(q).toString() + "\n"); CircularPolynomial zero = new CircularPolynomial();
System.out.print("- q(x) = ");
System.out.println(zero.subtract(q).toString() + "\n"); System.out.print("((p-q)*p)(x) = ");
System.out.println((p.subtract(q)).multiply(p).toString() + "\n");
System.out.println("((p-q)*p)(1) = " +
(p.subtract(q)).multiply(p).valueAt(1) + "\n");
System.out.println("((p-q)*p)(5.5) = " +
(p.subtract(q)).multiply(p).valueAt(5.5) + "\n"); p = new CircularPolynomial();
q = new CircularPolynomial(); p.input();
System.out.print("Polynomial p(x) = ");
System.out.println(p.toString() + "\n");
System.out.println("p(0.0001) = " +p.valueAt(.0001) + "\n");
q.input();
System.out.print("Polynomial q(x) = ");
System.out.println(q.toString() + "\n");
System.out.println("q(-1.001) = " +q.valueAt(-1.001) + "\n"); System.out.println("(p*p*q)(x) = " +
p.multiply(p).multiply(q).toString() + "\n");
System.out.println("(p*p*q)(2.5) = " +
p.multiply(p).multiply(q).valueAt(2.5) + "\n");
}}
////////////PolyNode.java代码如下:
/**
* PolyNode.java
*
*/
package dataStructures;public class PolyNode {
// data members
protected double coeff; // coefficient
protected int exp; // exponent
protected PolyNode next; // pointer to next node
// constructors
public PolyNode(double theCoeff, int theExp, PolyNode theNext){
coeff = theCoeff;
exp = theExp;
next = theNext;
}
public PolyNode(double theCoeff, int theExp){
this(theCoeff, theExp, null);
}
public PolyNode() {
this(0, -1, null);
} // accessors
public double getCoeff(){
return coeff;
}
public int getExp(){
return exp;
}
public PolyNode next(){
return next;
} // mutators
public void setCoeff(double coeff){
this.coeff = coeff;
}
public void setExp(int exp){
this.exp = exp;
}
public void setNext(PolyNode next){
this.next = next;
}} // PolyNode
/**
* Univariate polynomials with integer coefficients represented
* as circular linked lists with head nodes. Nodes that
* represent terms with higher powers of the variable appear
* earlier in the list than do nodes that represent terms with
* lower powers of the variable.
*/import java.io.*;
import java.util.*; // for the use of Stack class
//import java.lang.Object;
import dataStructures.PolyNode;public class CircularPolynomial{
// data members of CircularPolynomial
protected PolyNode headNode;
protected PolyNode lastNode; // constructor
/** create the zero polynomial */
public CircularPolynomial(){
headNode = new PolyNode();
lastNode = new PolyNode();
} /**
* degree
* @return polynomial degree */
public int degree(){
return (this.headNode.next().getExp());
} /**
* clone
* make a clone; a separate but identical object */
public Object clone(){
CircularPolynomial p = new CircularPolynomial();
// Use tmpNode as the pioneer to test if attain at the end of the polynomial;
PolyNode tmpNode = new PolyNode();
tmpNode.setCoeff(this.headNode.getCoeff());
tmpNode.setExp(this.headNode.getExp());
tmpNode.setNext(this.headNode.next());
while(tmpNode.next().getExp() != -1){ // Have not attain the endNode yet,
tmpNode.setCoeff(tmpNode.next().getCoeff());
tmpNode.setExp(tmpNode.next().getExp());
tmpNode.setNext(tmpNode.next().next());
p.append(tmpNode.getCoeff(), tmpNode.getExp());
}
// this lastNode should be the copy of remnant tmpNode,
// the 'next' domain of it would be the headNode of it.
p.lastNode.setCoeff(tmpNode.getCoeff());
p.lastNode.setExp(tmpNode.getExp());
p.lastNode.setNext(p.headNode);
return p;
} /**
* append
* add a new term to the end of the list
* (this method is complete)*/
private void append(double theCoeff, int theExp){
lastNode.setNext(new PolyNode(theCoeff, theExp, headNode));
lastNode = lastNode.next();
} /**
* input
* input the polynomial from the standard input stream
* (this method is complete) */
public void input() throws Exception {
BufferedReader keyboard = new BufferedReader(
new InputStreamReader(System.in),1);
// input number of terms
System.out.print("Enter the number of non-zero terms: ");
int terms = Integer.parseInt(keyboard.readLine());
if (terms < 0){
System.out.println("The number of terms must be >= 0; " +
"you entered " + terms);
System.exit(-1);
}
if (terms > 0){
// at least one nonzero term, input the nonzero terms
// in decreasing order of exponents
System.out.println("\nEnter the nonzero terms "
+ "in decreasing order of exponents.\n"
+ "For example, for the polynomial 2x^7 - 3x^4 + ... "+
"+ x + 2,\n"
+ "give the input as a sequence 2, 7, -3, 4, ..., "+
"1, 1, 2, 0.\n"
);
// get first term
System.out.print("Coefficient: ");
double coefficient = Double.valueOf(keyboard.readLine()).doubleValue();
System.out.print("Exponent: ");
int exponent = Integer.parseInt(keyboard.readLine());
// exponent must be >= 0 and coefficient must be nonzero
if (exponent < 0 || coefficient == 0){
System.out.println("Exponent must be >= 0 and "+
"coefficient must not equal 0. "+
"They are exponent = " + exponent +
", coefficient = " + coefficient);
System.exit(-1);
}
this.append(coefficient, exponent);
int lastExponent = exponent;//to check for input errors
// get the remaining terms
for (int i = 2; i <= terms; i++){
// get next term
System.out.print("Coefficient: ");
coefficient = Double.valueOf(keyboard.readLine()).doubleValue();
System.out.print("Exponent: ");
exponent = Integer.parseInt(keyboard.readLine());
// exponent must be < lastExponent and
//coefficient must be nonzero
if (exponent >= lastExponent || coefficient == 0){
System.out.println("Enter exponents in descending"+
" order and coefficients must not equal 0.");
System.exit(-1);
}
this.append(coefficient, exponent);
lastExponent = exponent;
}
}
}
/**
* toString
* a String representation of the polynomial */
public String toString(){
PolyNode tmpNode = new PolyNode();
tmpNode.setNext(this.headNode.next()); String str = new String();
while( tmpNode.next().getExp() != -1){
// I don't know the function to control the format of a double or an integer;
if(tmpNode.next().getExp() == 1){
str += tmpNode.next().getCoeff() + "X +" ;
}
else if(tmpNode.next().getExp() == 0){
str += tmpNode.next().getCoeff();
}
else {
str += tmpNode.next().getCoeff() + "X^" + tmpNode.next().getExp() + "+";
}
tmpNode.setNext(tmpNode.next().next());
}
str = "f(x) = "+ str;
return str;
}
/**
* add
* @return this + b */
public CircularPolynomial add(CircularPolynomial b){
CircularPolynomial sum = new CircularPolynomial();
PolyNode na = new PolyNode(this.headNode.getCoeff(),this.headNode.getExp(),this.headNode.next());
PolyNode nb = new PolyNode(b.headNode.getCoeff(),b.headNode.getExp(),b.headNode.next()); //If all the two CircularPolynomial have not been added out.(HAI MEI YOU JIA WAN)
while (na.next().getExp()!=-1 && nb.next().getExp()!=-1){
if ( na.next().getExp() > nb.next().getExp() ){
sum.append(na.next().getCoeff(), na.next().getExp());
na.setNext(na.next().next());
}
else if ( na.next().getExp()== nb.next().getExp()){
sum.append((na.next().getCoeff() + nb.next().getCoeff()), na.next().getExp());
na.setNext(na.next().next());
nb.setNext(nb.next().next());
}
else if ( na.next().getExp() < nb.next().getExp() ){
sum.append(nb.next().getCoeff(), nb.next().getExp());
nb.setNext(nb.next().next());
}
} // If there would be some Polynomial which has not been added,
// append them directly to the end of the sum;
if(na.next().getExp() != -1){ // nb has added out
while (na.next().getExp() != -1){
sum.append(na.next().getCoeff(), na.next().getExp());
na.setNext(na.next().next());
}
sum.lastNode.setCoeff(na.getCoeff());
sum.lastNode.setExp(na.getExp());
sum.lastNode.setNext(this.headNode);
} if(nb.next().getExp()!=-1){
while (nb.next().getExp()!=-1){
sum.append(nb.next().getCoeff(), nb.next().getExp());
nb.setNext(nb.next().next());
}
sum.lastNode.setCoeff(nb.getCoeff());
sum.lastNode.setExp(nb.getExp());
sum.lastNode.setNext(b.headNode);
}
return sum;
}
/**
* subtract
* The code for this method would be essentially
* identical to that for the add method.
* A different approach will be used here (for variety);
* this method will compute this + (-b) by calling the
* add method. This implementation, then, has a greater
* time complexity than it otherwise would. The code,
* however, is brief.
* @return this - b
*/
public CircularPolynomial subtract(CircularPolynomial b){
CircularPolynomial bb = new CircularPolynomial();
PolyNode nb = new PolyNode(b.headNode.getCoeff(),b.headNode.getExp(),b.headNode.next());
//According to the requirement that the coeffs of b be changed into negative
while(nb.next().getExp() != -1){
bb.append(-nb.next().getCoeff(), nb.next().getExp());
nb.setNext(nb.next().next());
}
return this.add(bb);
}
/**
* multiply
* @return w = this * b */
public CircularPolynomial multiply(CircularPolynomial b){
// Initialize two Polynomials: one as the final result, The other as the worker;
CircularPolynomial result = (CircularPolynomial)this.clone();
CircularPolynomial worker = (CircularPolynomial)this.clone();
// Initialize PolyNode 'n' to trace Polynomial b:
PolyNode n = new PolyNode(b.headNode.getCoeff(), b.headNode.getExp(), b.headNode.next());
while(n.next().getExp() != -1){
worker = this.simpleMultiply(n);
result = result.add(worker);
n.setCoeff(n.next().getCoeff());
n.setExp(n.next().getExp());
n.setNext(n.next().next());
}
return result;
} /**
* simple multiply
* This auxiliary multiplication method multiply a polynomial with a PolyNode,
* and return the resulting polynomial.
* @return w = this * n */
public CircularPolynomial simpleMultiply(PolyNode n){
CircularPolynomial w = new CircularPolynomial();
PolyNode na = new PolyNode(this.headNode.getCoeff(),this.headNode.getExp(),this.headNode.next());
// initialize coeff and exp so as to save time;
double coeff = n.getCoeff();
int exp = n.getExp();
while (na.next().getExp() != -1){
na.setCoeff(na.next().getCoeff() * coeff);
na.setExp(na.next().getExp() + exp);
w.append(na.getCoeff(), na.getExp());
na.setNext(na.next().next());
}
w.lastNode.setCoeff(na.getCoeff());
w.lastNode.setExp(na.getExp());
w.lastNode.setNext(w.headNode);
return w;
} /**
* valueAt
* @return polynomial value at x, rounded to 2 decimal places */
public double valueAt(double x){
// We can use two stacks to save the coeffs of 'this' Polynomial
Stack CoeffStack = new Stack();
Stack ExpStack = new Stack(); PolyNode na = new PolyNode(this.headNode.getCoeff(),this.headNode.getExp(),this.headNode.next()); while (na.next().getExp() != -1){
CoeffStack.push(new Double(na.next().getCoeff()));
ExpStack.push(new Integer(na.next().getExp()));
na.setNext(na.next().next());
}
// 'worker' is the temp value and 'result' would get the final values
double worker = 1, result = 0;
int count, currentExp, prevExp = ExpStack.pop();
double prevCoeff = CoeffStack.pop(); // Initialize the manipulations:
if(prevExp == 0){
result = prevCoeff;
}else{
for (count = 0; count < prevExp; count ++){
worker *= x;
}
result = prevCoeff*worker;
}
// Go on processing
currentExp = ExpStack.pop();
while( !ExpStack.empty() ){
for (count = 0; count < currentExp - prevExp; count ++ ){
worker *= x;
}
result += CoeffStack.pop() * worker;
prevExp = currentExp;
currentExp = ExpStack.pop();
}
return result; }
/** test program */
public static void main(String [] args) throws Exception { CircularPolynomial p = new CircularPolynomial();
CircularPolynomial q = new CircularPolynomial(); p.input();
System.out.print("Polynomial p(x) = ");
System.out.println(p.toString() + "\n");
System.out.println("p(2) = " +p.valueAt(2) + "\n");
q.input();
System.out.print("Polynomial q(x) = ");
System.out.println(q.toString() + "\n");
System.out.println("q(-1) = " +q.valueAt(-1) + "\n"); System.out.print("(p + q)(x) = ");
System.out.println(p.add(q).toString() + "\n");
System.out.println("(p + q)(3) = " +
(p.add(q)).valueAt(3) + "\n");
System.out.print("(p - q)(x) = ");
System.out.println(p.subtract(q).toString() + "\n");
System.out.print("(p * q)(x) = ");
System.out.println(p.multiply(q).toString() + "\n"); CircularPolynomial zero = new CircularPolynomial();
System.out.print("- q(x) = ");
System.out.println(zero.subtract(q).toString() + "\n"); System.out.print("((p-q)*p)(x) = ");
System.out.println((p.subtract(q)).multiply(p).toString() + "\n");
System.out.println("((p-q)*p)(1) = " +
(p.subtract(q)).multiply(p).valueAt(1) + "\n");
System.out.println("((p-q)*p)(5.5) = " +
(p.subtract(q)).multiply(p).valueAt(5.5) + "\n"); p = new CircularPolynomial();
q = new CircularPolynomial(); p.input();
System.out.print("Polynomial p(x) = ");
System.out.println(p.toString() + "\n");
System.out.println("p(0.0001) = " +p.valueAt(.0001) + "\n");
q.input();
System.out.print("Polynomial q(x) = ");
System.out.println(q.toString() + "\n");
System.out.println("q(-1.001) = " +q.valueAt(-1.001) + "\n"); System.out.println("(p*p*q)(x) = " +
p.multiply(p).multiply(q).toString() + "\n");
System.out.println("(p*p*q)(2.5) = " +
p.multiply(p).multiply(q).valueAt(2.5) + "\n");
}}
////////////PolyNode.java代码如下:
/**
* PolyNode.java
*
*/
package dataStructures;public class PolyNode {
// data members
protected double coeff; // coefficient
protected int exp; // exponent
protected PolyNode next; // pointer to next node
// constructors
public PolyNode(double theCoeff, int theExp, PolyNode theNext){
coeff = theCoeff;
exp = theExp;
next = theNext;
}
public PolyNode(double theCoeff, int theExp){
this(theCoeff, theExp, null);
}
public PolyNode() {
this(0, -1, null);
} // accessors
public double getCoeff(){
return coeff;
}
public int getExp(){
return exp;
}
public PolyNode next(){
return next;
} // mutators
public void setCoeff(double coeff){
this.coeff = coeff;
}
public void setExp(int exp){
this.exp = exp;
}
public void setNext(PolyNode next){
this.next = next;
}} // PolyNode
String a="hello",
Object b=a;
String c=(String)b;
b.getClass()可以得到class,然后可以得到实例类的相关信息。Good luck.:)
你的程序风格也太差了
/**
* Univariate polynomials with integer coefficients represented
* as circular linked lists with head nodes. Nodes that
* represent terms with higher powers of the variable appear
* earlier in the list than do nodes that represent terms with
* lower powers of the variable.
*/
package untitled4;
import java.io.*;
import java.util.*; // for the use of Stack class
//import java.lang.Object;
import untitled4.PolyNode;public class CircularPolynomial{ // data members of CircularPolynomial
protected PolyNode headNode;
protected PolyNode lastNode; // constructor
/** create the zero polynomial */
public CircularPolynomial(){
headNode = new PolyNode();
lastNode = new PolyNode();
} /**
* degree
* @return polynomial degree */
public int degree(){
return (this.headNode.next().getExp());
} /**
* clone
* make a clone; a separate but identical object */
public Object clone(){
CircularPolynomial p = new CircularPolynomial(); // Use tmpNode as the pioneer to test if attain at the end of the polynomial;
PolyNode tmpNode = new PolyNode();
tmpNode.setCoeff(this.headNode.getCoeff());
tmpNode.setExp(this.headNode.getExp());
tmpNode.setNext(this.headNode.next()); while(tmpNode.next().getExp() != -1){ // Have not attain the endNode yet,
tmpNode.setCoeff(tmpNode.next().getCoeff());
tmpNode.setExp(tmpNode.next().getExp());
tmpNode.setNext(tmpNode.next().next());
p.append(tmpNode.getCoeff(), tmpNode.getExp());
}
// this lastNode should be the copy of remnant tmpNode,
// the 'next' domain of it would be the headNode of it.
p.lastNode.setCoeff(tmpNode.getCoeff());
p.lastNode.setExp(tmpNode.getExp());
p.lastNode.setNext(p.headNode);return p;
} /**
* append
* add a new term to the end of the list
* (this method is complete)*/
private void append(double theCoeff, int theExp){
lastNode.setNext(new PolyNode(theCoeff, theExp, headNode));
lastNode = lastNode.next();
} /**
* input
* input the polynomial from the standard input stream
* (this method is complete) */
public void input() throws Exception {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in),1); // input number of terms
System.out.print("Enter the number of non-zero terms: "); int terms = Integer.parseInt(keyboard.readLine());
if (terms < 0){
System.out.println("The number of terms must be >= 0; " +
"you entered " + terms);
System.exit(-1);
}
if (terms > 0){
// at least one nonzero term, input the nonzero terms
// in decreasing order of exponents
System.out.println("\nEnter the nonzero terms "
+ "in decreasing order of exponents.\n"
+ "For example, for the polynomial 2x^7 - 3x^4 + ... "+
"+ x + 2,\n"
+ "give the input as a sequence 2, 7, -3, 4, ..., "+
"1, 1, 2, 0.\n"
); // get first term
System.out.print("Coefficient: ");
double coefficient = Double.valueOf(keyboard.readLine()).doubleValue();
System.out.print("Exponent: ");
int exponent = Integer.parseInt(keyboard.readLine());
// exponent must be >= 0 and coefficient must be nonzero
if (exponent < 0 || coefficient == 0){
System.out.println("Exponent must be >= 0 and "+
"coefficient must not equal 0. "+
"They are exponent = " + exponent +
", coefficient = " + coefficient);
System.exit(-1);
}
this.append(coefficient, exponent);
int lastExponent = exponent;//to check for input errors // get the remaining terms
for (int i = 2; i <= terms; i++){
// get next term
System.out.print("Coefficient: ");
coefficient = Double.valueOf(keyboard.readLine()).doubleValue();
System.out.print("Exponent: ");
exponent = Integer.parseInt(keyboard.readLine());
// exponent must be < lastExponent and
//coefficient must be nonzero
if (exponent >= lastExponent || coefficient == 0){
System.out.println("Enter exponents in descending"+
" order and coefficients must not equal 0.");
System.exit(-1);
}
this.append(coefficient, exponent);
lastExponent = exponent;
}
}
} /**
* toString
* a String representation of the polynomial */
public String toString(){
PolyNode tmpNode = new PolyNode();
tmpNode.setNext(this.headNode.next());String str = new String();
while( tmpNode.next().getExp() != -1){
// I don't know the function to control the format of a double or an integer;
if(tmpNode.next().getExp() == 1){
str += tmpNode.next().getCoeff() + "X +" ;
}
else if(tmpNode.next().getExp() == 0){
str += tmpNode.next().getCoeff();
}
else {
str += tmpNode.next().getCoeff() + "X^" + tmpNode.next().getExp() + "+";
}
tmpNode.setNext(tmpNode.next().next());
}
str = "f(x) = "+ str;
return str;
} /**
* add
* @return this + b */
public CircularPolynomial add(CircularPolynomial b){
CircularPolynomial sum = new CircularPolynomial();
PolyNode na = new PolyNode(this.headNode.getCoeff(),this.headNode.getExp(),this.headNode.next());
PolyNode nb = new PolyNode(b.headNode.getCoeff(),b.headNode.getExp(),b.headNode.next()); //If all the two CircularPolynomial have not been added out.(HAI MEI YOU JIA WAN)
while (na.next().getExp()!=-1 && nb.next().getExp()!=-1){
if ( na.next().getExp() > nb.next().getExp() ){
sum.append(na.next().getCoeff(), na.next().getExp());
na.setNext(na.next().next());
}
else if ( na.next().getExp()== nb.next().getExp()){
sum.append((na.next().getCoeff() + nb.next().getCoeff()), na.next().getExp());
na.setNext(na.next().next());
nb.setNext(nb.next().next());
}
else if ( na.next().getExp() < nb.next().getExp() ){
sum.append(nb.next().getCoeff(), nb.next().getExp());
nb.setNext(nb.next().next());
}
}// If there would be some Polynomial which has not been added,
// append them directly to the end of the sum;
if(na.next().getExp() != -1){ // nb has added out
while (na.next().getExp() != -1){
sum.append(na.next().getCoeff(), na.next().getExp());
na.setNext(na.next().next());
}
sum.lastNode.setCoeff(na.getCoeff());
sum.lastNode.setExp(na.getExp());
sum.lastNode.setNext(this.headNode);
}if(nb.next().getExp()!=-1){
while (nb.next().getExp()!=-1){
sum.append(nb.next().getCoeff(), nb.next().getExp());
nb.setNext(nb.next().next());
}
sum.lastNode.setCoeff(nb.getCoeff());
sum.lastNode.setExp(nb.getExp());
sum.lastNode.setNext(b.headNode);
}
return sum;
} /**
* subtract
* The code for this method would be essentially
* identical to that for the add method.
* A different approach will be used here (for variety);
* this method will compute this + (-b) by calling the
* add method. This implementation, then, has a greater
* time complexity than it otherwise would. The code,
* however, is brief.
* @return this - b
*/
public CircularPolynomial subtract(CircularPolynomial b){
CircularPolynomial bb = new CircularPolynomial();
PolyNode nb = new PolyNode(b.headNode.getCoeff(),b.headNode.getExp(),b.headNode.next());
//According to the requirement that the coeffs of b be changed into negative
while(nb.next().getExp() != -1){
bb.append(-nb.next().getCoeff(), nb.next().getExp());
nb.setNext(nb.next().next());
}
return this.add(bb);
} /**
* multiply
* @return w = this * b */
public CircularPolynomial multiply(CircularPolynomial b){
// Initialize two Polynomials: one as the final result, The other as the worker;
CircularPolynomial result = (CircularPolynomial)this.clone();
CircularPolynomial worker = (CircularPolynomial)this.clone();// Initialize PolyNode 'n' to trace Polynomial b:
PolyNode n = new PolyNode(b.headNode.getCoeff(), b.headNode.getExp(), b.headNode.next());while(n.next().getExp() != -1){
worker = this.simpleMultiply(n);
result = result.add(worker);
n.setCoeff(n.next().getCoeff());
n.setExp(n.next().getExp());
n.setNext(n.next().next());
}
return result;
} /**
* simple multiply
* This auxiliary multiplication method multiply a polynomial with a PolyNode,
* and return the resulting polynomial.
* @return w = this * n */
public CircularPolynomial simpleMultiply(PolyNode n){
CircularPolynomial w = new CircularPolynomial();
PolyNode na = new PolyNode(this.headNode.getCoeff(),this.headNode.getExp(),this.headNode.next());// initialize coeff and exp so as to save time;
double coeff = n.getCoeff();
int exp = n.getExp();while (na.next().getExp() != -1){
na.setCoeff(na.next().getCoeff() * coeff);
na.setExp(na.next().getExp() + exp);
w.append(na.getCoeff(), na.getExp());
na.setNext(na.next().next());
}
w.lastNode.setCoeff(na.getCoeff());
w.lastNode.setExp(na.getExp());
w.lastNode.setNext(w.headNode);
return w;
} /**
* valueAt
* @return polynomial value at x, rounded to 2 decimal places */
public double valueAt(double x){
// We can use two stacks to save the coeffs of 'this' Polynomial
Stack CoeffStack = new Stack();
Stack ExpStack = new Stack(); PolyNode na = new PolyNode(this.headNode.getCoeff(),this.headNode.getExp(),this.headNode.next()); while (na.next().getExp() != -1){
CoeffStack.push(new Double(na.next().getCoeff()));
ExpStack.push(new Integer(na.next().getExp()));
na.setNext(na.next().next());
}// 'worker' is the temp value and 'result' would get the final values
double worker = 1, result = 0;
int count, currentExp;
Integer prevExp = (Integer)ExpStack.pop();
Double prevCoeff =(Double) CoeffStack.pop(); // Initialize the manipulations:
if(prevExp.intValue() == 0) result = prevCoeff.doubleValue();
else{
for (count = 0; count < prevExp.intValue(); count ++)worker *= x;
result = prevCoeff.doubleValue()*worker;
}
// Go on processing
currentExp = ((Integer)ExpStack.pop()).intValue();
while( !ExpStack.empty() ){
for (count = 0; count < currentExp - prevExp.intValue(); count ++ )worker *= x;
result += ((Double) CoeffStack.pop()).doubleValue()* worker;
prevExp = new Integer(currentExp);
currentExp =((Integer)ExpStack.pop()).intValue();
}
return result; } /** test program */
public static void main(String [] args) throws Exception { CircularPolynomial p = new CircularPolynomial();
CircularPolynomial q = new CircularPolynomial(); p.input();
System.out.print("Polynomial p(x) = ");
System.out.println(p.toString() + "\n");
System.out.println("p(2) = " +p.valueAt(2) + "\n"); q.input();
System.out.print("Polynomial q(x) = ");
System.out.println(q.toString() + "\n");
System.out.println("q(-1) = " +q.valueAt(-1) + "\n"); System.out.print("(p + q)(x) = ");
System.out.println(p.add(q).toString() + "\n");
System.out.println("(p + q)(3) = " +
(p.add(q)).valueAt(3) + "\n"); System.out.print("(p - q)(x) = ");
System.out.println(p.subtract(q).toString() + "\n"); System.out.print("(p * q)(x) = ");
System.out.println(p.multiply(q).toString() + "\n"); CircularPolynomial zero = new CircularPolynomial();
System.out.print("- q(x) = ");
System.out.println(zero.subtract(q).toString() + "\n"); System.out.print("((p-q)*p)(x) = ");
System.out.println((p.subtract(q)).multiply(p).toString() + "\n");
System.out.println("((p-q)*p)(1) = " +
(p.subtract(q)).multiply(p).valueAt(1) + "\n");
System.out.println("((p-q)*p)(5.5) = " +
(p.subtract(q)).multiply(p).valueAt(5.5) + "\n"); p = new CircularPolynomial();
q = new CircularPolynomial(); p.input();
System.out.print("Polynomial p(x) = ");
System.out.println(p.toString() + "\n");
System.out.println("p(0.0001) = " +p.valueAt(.0001) + "\n"); q.input();
System.out.print("Polynomial q(x) = ");
System.out.println(q.toString() + "\n");
System.out.println("q(-1.001) = " +q.valueAt(-1.001) + "\n"); System.out.println("(p*p*q)(x) = " +
p.multiply(p).multiply(q).toString() + "\n");
System.out.println("(p*p*q)(2.5) = " +
p.multiply(p).multiply(q).valueAt(2.5) + "\n");
}}
访问http://168.168.18.11:81/etechbase/advsearch.php将您的问题输入查询内容框,选择不同的精确程度,即可以找到你所需要的答案。效果还是可以的。