有一个名为Polynomial的类,它有一个Node类,Node类由一个术语类组成,它自己包含相应多项式的系数和程度。因此有两个对象,p多项式和这个多项式,这些是节点的链表,每个节点都有一个术语对象(包含有关度和系数的数据)。我得到了p多项式和这个多项式,并且必须使用它们来构建newPoly,它是一个包含节点链表的对象,每个节点都包含一个term对象。我必须返回newPoly对象。运行时,我的代码不显示我返回的新添加的多项式。请帮我。
public Polynomial add(Polynomial p) {
/** COMPLETE THIS METHOD **/
Polynomial x = this;
Polynomial newPoly = new Polynomial ();
Polynomial y = p;
while (x.poly.next != null || y.poly.next != null){
Node ptr = null;
if (x.poly.term.degree == y.poly.term.degree ){
/*ptr.term.coeff = x.poly.term.coeff + y.poly.term.coeff;
ptr.term.degree = x.poly.term.degree;*/
ptr = new Node (x.poly.term.coeff+y.poly.term.coeff,x.poly.term.degree, newPoly.poly);
//ptr.next = newPoly.poly;
y.poly = y.poly.next;
x.poly = x.poly.next;
}else if (x.poly.term.degree>y.poly.term.degree){
ptr.term.coeff = x.poly.term.coeff;
ptr.term.degree = x.poly.term.degree;
ptr.next = newPoly.poly;
/*
Polynomial ptr2 = new Polynomial();
ptr2.poly.term.coeff = y.poly.term.coeff;
ptr2.poly.term.degree = y.poly.term.degree;
ptr2.poly.next = newPoly.poly;
*/
x.poly = x.poly.next;
}else if (x.poly.term.degree<y.poly.term.degree){
ptr.term.coeff = y.poly.term.coeff;
ptr.term.degree = y.poly.term.degree;
ptr.next = newPoly.poly;
y.poly = y.poly.next;
}else {
if (x.poly.next == null){
float coef = x.poly.term.coeff + y.poly.term.coeff;
int exp = x.poly.term.degree;
x.poly = x.poly.next;
y.poly= y.poly.next;
if (coef == 0) continue;
ptr = new Node(coef, exp, null);
}
}
}
return newPoly;
}
//整数多项式类代码 包poly;
import java.io.*;
import java.util.StringTokenizer;
/**
* This class implements a term of a polynomial.
*
* @author runb-cs112
*
*/
class Term {
/**
* Coefficient of term.
*/
public float coeff;
/**
* Degree of term.
*/
public int degree;
/**
* Initializes an instance with given coefficient and degree.
*
* @param coeff Coefficient
* @param degree Degree
*/
public Term(float coeff, int degree) {
this.coeff = coeff;
this.degree = degree;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object other) {
return other != null &&
other instanceof Term &&
coeff == ((Term)other).coeff &&
degree == ((Term)other).degree;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
if (degree == 0) {
return coeff + "";
} else if (degree == 1) {
return coeff + "x";
} else {
return coeff + "x^" + degree;
}
}
}
/**
* This class implements a linked list node that contains a Term instance.
*
* @author runb-cs112
*
*/
class Node {
/**
* Term instance.
*/
Term term;
/**
* Next node in linked list.
*/
Node next;
/**
* Initializes this node with a term with given coefficient and degree,
* pointing to the given next node.
*
* @param coeff Coefficient of term
* @param degree Degree of term
* @param next Next node
*/
public Node(float coeff, int degree, Node next) {
term = new Term(coeff, degree);
this.next = next;
}
}
/**
* This class implements a polynomial.
*
* @author runb-cs112
*
*/
public class Polynomial {
/**
* Pointer to the front of the linked list that stores the polynomial.
*/
Node poly;
/**
* Initializes this polynomial to empty, i.e. there are no terms.
*
*/
public Polynomial() {
poly = null;
}
/**
* Reads a polynomial from an input stream (file or keyboard). The storage format
* of the polynomial is:
* <pre>
* <coeff> <degree>
* <coeff> <degree>
* ...
* <coeff> <degree>
* </pre>
* with the guarantee that degrees will be in descending order. For example:
* <pre>
* 4 5
* -2 3
* 2 1
* 3 0
* </pre>
* which represents the polynomial:
* <pre>
* 4*x^5 - 2*x^3 + 2*x + 3
* </pre>
*
* @param br BufferedReader from which a polynomial is to be read
* @throws IOException If there is any input error in reading the polynomial
*/
public Polynomial(BufferedReader br) throws IOException {
String line;
StringTokenizer tokenizer;
float coeff;
int degree;
poly = null;
while ((line = br.readLine()) != null) {
tokenizer = new StringTokenizer(line);
coeff = Float.parseFloat(tokenizer.nextToken());
degree = Integer.parseInt(tokenizer.nextToken());
poly = new Node(coeff, degree, poly);
}
}
/**
* Returns the polynomial obtained by adding the given polynomial p
* to this polynomial - DOES NOT change this polynomial
*
* @param p Polynomial to be added
* @return A new polynomial which is the sum of this polynomial and p.
*/
public Polynomial add(Polynomial p) {
/** COMPLETE THIS METHOD **/
Polynomial x = this;
Polynomial newPoly = new Polynomial ();
Polynomial y = p;
while (x.poly.next != null || y.poly.next != null){
Node ptr = null;
if (x.poly.term.degree == y.poly.term.degree ){
/*ptr.term.coeff = x.poly.term.coeff + y.poly.term.coeff;
ptr.term.degree = x.poly.term.degree;*/
ptr = new Node (x.poly.term.coeff+y.poly.term.coeff,x.poly.term.degree, newPoly.poly);
//ptr.next = newPoly.poly;
y.poly = y.poly.next;
x.poly = x.poly.next;
}else if (x.poly.term.degree>y.poly.term.degree){
ptr.term.coeff = x.poly.term.coeff;
ptr.term.degree = x.poly.term.degree;
ptr.next = newPoly.poly;
/*
Polynomial ptr2 = new Polynomial();
ptr2.poly.term.coeff = y.poly.term.coeff;
ptr2.poly.term.degree = y.poly.term.degree;
ptr2.poly.next = newPoly.poly;
*/
x.poly = x.poly.next;
}else if (x.poly.term.degree<y.poly.term.degree){
ptr.term.coeff = y.poly.term.coeff;
ptr.term.degree = y.poly.term.degree;
ptr.next = newPoly.poly;
y.poly = y.poly.next;
}else {
if (x.poly.next == null){
float coef = x.poly.term.coeff + y.poly.term.coeff;
int exp = x.poly.term.degree;
x.poly = x.poly.next;
y.poly= y.poly.next;
if (coef == 0) continue;
ptr = new Node(coef, exp, null);
}
}
}
return newPoly;
}
/**
* Returns the polynomial obtained by multiplying the given polynomial p
* with this polynomial - DOES NOT change this polynomial
*
* @param p Polynomial with which this polynomial is to be multiplied
* @return A new polynomial which is the product of this polynomial and p.
*/
public Polynomial multiply(Polynomial p) {
/** COMPLETE THIS METHOD **/
return null;
}
/**
* Evaluates this polynomial at the given value of x
*
* @param x Value at which this polynomial is to be evaluated
* @return Value of this polynomial at x
*/
public float evaluate(float x) {
/** COMPLETE THIS METHOD **/
return 0;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
String retval;
if (poly == null) {
return "0";
} else {
retval = poly.term.toString();
for (Node current = poly.next ;
current != null ;
current = current.next) {
retval = current.term.toString() + " + " + retval;
}
return retval;
}
}
}
//main
package poly;
import java.io.*;
public class Polytest {
static BufferedReader br1, br2;
static Polynomial p1, p2;
public static final int ADD = 1;
public static final int MULTIPLY = 2;
public static final int EVALUATE = 3;
public static final int QUIT = 4;
public static int getChoice()
throws IOException {
System.out.println();
System.out.println(ADD + ". ADD polynomial");
System.out.println(MULTIPLY + ". MULTIPLY polynomial");
System.out.println(EVALUATE + ". EVALUATE polynomial");
System.out.println(QUIT + ". QUIT");
System.out.print("\tEnter choice # => ");
return (Integer.parseInt(br1.readLine()));
}
public static void add()
throws IOException {
System.out.print("Enter the file containing the polynomial to add => ");
br2 = new BufferedReader(new FileReader(br1.readLine()));
p2 = new Polynomial(br2);
System.out.println("\n" + p2 + "\n");
System.out.println("Sum: " + p1.add(p2) + "\n");
}
public static void multiply()
throws IOException {
System.out.print("Enter the file containing the polynomial to multiply => ");
br2 = new BufferedReader(new FileReader(br1.readLine()));
p2 = new Polynomial(br2);
System.out.println("\n" + p2 + "\n");
System.out.println("Product: " + p1.multiply(p2) + "\n");
}
public static void evaluate()
throws IOException {
System.out.print("Enter the evaluation point x => ");
float x = Float.parseFloat(br1.readLine());
System.out.println("Value at " + x + ": " + p1.evaluate(x) + "\n");
}
public static void main(String[] args) throws IOException {
br1 = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the name of the polynomial file => ");
br2 = new BufferedReader(new FileReader(br1.readLine()));
p1 = new Polynomial(br2);
System.out.println("\n" + p1 + "\n");
int choice = getChoice();
while (choice != QUIT) {
if (choice < 1 || choice > QUIT) {
System.out.println("\tIncorrect choice " + choice);
} else {
switch (choice) {
case ADD: add(); break;
case MULTIPLY: multiply(); break;
case EVALUATE: evaluate(); break;
default: break;
}
}
choice = getChoice();
}
}
}