//package Spring02.PS8; import javax.swing.*; public class PolyTest1 { public static void main(String[] args) { String text; Polynomial p0= new Polynomial(); Polynomial p1= new Polynomial(); // Read in polynomial 0 System.out.println("Polynomial 0"); do { text= JOptionPane.showInputDialog("Enter polynomial 0 term degree (-99 to end): "); int d= Integer.parseInt(text); if (d == -99) break; text= JOptionPane.showInputDialog("Enter polynomial 0 term coefficient: "); double c= Double.parseDouble(text); p0.addTerm(d, c); p0.printPoly(); } while (true); // Read in polynomial 1 System.out.println("Polynomial 1"); do { text= JOptionPane.showInputDialog("Enter polynomial 1 term degree (-99 to end): "); int d= Integer.parseInt(text); if (d == -99) break; text= JOptionPane.showInputDialog("Enter polynomial 1 term coefficient: "); double c= Double.parseDouble(text); p1.addTerm(d, c); p1.printPoly(); } while (true); do { text= JOptionPane.showInputDialog("Action:A)dd,S)calar mult,M)ult,E)val,D)eriv,Q)uit:"); if (text.equals("A") || text.equals("a")) { System.out.println("Add Polynomial1 to Polynomial0:"); p0.addPoly(p1); System.out.println("Polynomial0 is now:"); p0.printPoly(); } else if (text.equals("S") || text.equals("s")) { String text1= JOptionPane.showInputDialog("Enter scalar multiplier: "); double z= Double.parseDouble(text1); System.out.println("Scalar multiply by " + z); p0.multScalar(z); p0.printPoly(); p1.multScalar(z); p1.printPoly(); } else if (text.equals("M") || text.equals("m")) { Polynomial p3= Polynomial.multPoly(p0, p1); System.out.println("Multiply:"); p3.printPoly(); } else if (text.equals("E") || text.equals("e")) { String text2= JOptionPane.showInputDialog("Enter x: "); double x= Double.parseDouble(text2); System.out.println("Evaluate at x= " + x); p0.printPoly(); System.out.println("Result: " + p0.evalPoly(x)); p1.printPoly(); System.out.println("Result: " + p1.evalPoly(x)); } else if (text.equals("D") || text.equals("d")) { System.out.println("Derivative"); Polynomial p2= Polynomial.derivPoly(p0); p2.printPoly(); Polynomial p3= Polynomial.derivPoly(p1); p3.printPoly(); } else break; } while (true); System.out.println("Done"); System.exit(0); } } class Node { public Node(int d, double c){ degree = d; coef = c; next = null; } int degree; double coef; Node next; } class Polynomial { private Node head; public final static double TOLERANCE = 1E-6; public Polynomial(){ head = null; } //Add a term to the polynomial public void addTerm(int d, double c) { if (Math.abs(c) < TOLERANCE) return; if (head==null) { Node n= new Node(d, c); head= n; } else if (d > head.degree) { Node n= new Node(d, c); n.next= head; head= n; } else { Node curr= head; Node prev= null; while (curr != null && curr.degree > d) { prev= curr; curr= curr.next; } if (curr != null && curr.degree==d) { // If degree exists, add coeffs curr.coef += c; if (Math.abs(curr.coef) < TOLERANCE) // If coeff=0, delete node if (prev != null) // If prev node exists prev.next= curr.next; else head= curr.next; } else { // curr.degree < d; insert node Node n= new Node(d, c); prev.next= n; n.next= curr; } } } //Remove the node referenced by n from the linked list. public void removeTerm(Node n){ if (head == null) return; if (n == head){ head = head.next; return; } Node tmp = head; while (tmp.next != null){ if (tmp.next == n) { tmp.next = n.next; return; } tmp = tmp.next; } } public void addPoly(Polynomial p){ Node tmp = p.head; while (tmp != null){ addTerm(tmp.degree, tmp.coef); tmp = tmp.next; } } //Multiply a polynomial by a scalar. A member //method that modifies the polynomial being modified. public void multScalar(double s){ Node tmp = head; while (tmp != null){ tmp.coef *= s; if ( Math.abs(tmp.coef) < TOLERANCE) removeTerm(tmp); tmp = tmp.next; } } //Multiply two polynomials. A static method that returns a new polynomial that holds the //result. It does not modify either of the two input polynomials. public static Polynomial multPoly(Polynomial p1, Polynomial p2) { Node tmp1 = p1.head; Polynomial result = new Polynomial(); while (tmp1 != null) { Node tmp2 = p2.head; while (tmp2 != null){ result.addTerm(tmp1.degree + tmp2.degree, tmp1.coef * tmp2.coef); tmp2 = tmp2.next; } tmp1 = tmp1.next; } return result; } //Evaluate a poly for a given value of x public double evalPoly(double x){ Node tmp = head; double result = 0.0; while (tmp!=null){ result += tmp.coef * Math.pow(x, tmp.degree); tmp = tmp.next; } return result; } public static Polynomial derivPoly(Polynomial p){ Polynomial result = new Polynomial(); Node tmp = p.head; while (tmp != null){ // Add term won't add term w/ 0 coeff result.addTerm(tmp.degree-1, tmp.coef * tmp.degree); tmp = tmp.next; } return result; } public String getPoly(){ Node tmp = head; String s = new String(); if (tmp == null) s = s.concat("Empty polynomial"); while (tmp != null) { if ((tmp != head) && (tmp.coef > 0) ) { s = s.concat(" + "); } s = s.concat(tmp.coef + " x^ " + tmp.degree + " "); tmp = tmp.next; } return s; } public void printPoly(){ Node tmp = head; if (tmp == null) System.out.println("Empty polynomial"); while (tmp != null) { if ((tmp != head) && (tmp.coef > 0) ) { System.out.print(" + "); } System.out.print(tmp.coef + " x^ " + tmp.degree + " "); tmp = tmp.next; } System.out.println(""); } }