日期:2014-05-20 浏览次数:20796 次
import java.io.*; //To implement I/O operations class link { int coef, exp; // data part for the link list link next; // next link in list link(int a, int b) // constructor { coef = a; // initialize data exp = b; // initialize data } void display() // To display the list { System.out.print(" " + coef + "xe" + exp); } } // end of class link class linklist { link p, q, d; link first; // ref to first link on list linklist() // constructor { first = null; // no links on list yet } void create(int a, int b) // To insert into the list { link node = new link(a, b); // make new link node.next = first; first = node; } // End of function create void padd(linklist A, linklist B) // To add the polynomials { int x; // Temporary variable for storing coef p = A.first; q = B.first; d = first; while ((p != null) && (q != null)) { if (p.exp == q.exp) { x = p.coef + q.coef; if (x != 0) { link node = new link(x, p.exp); // make new link node.next = d; d = node; } p = p.next; // move to next node of 'A' q = q.next; // move to next node of 'B' } else if (p.exp > q.exp) { link node = new link(p.coef, p.exp); node.next = d; d = node; p = p.next; } else { link node = new link(q.coef, q.exp); node.next = d; d = node; q = q.next; } } while (p != null) { link node = new link(p.coef, p.exp); node.next = d; d = node; p = p.next; } while (q != null) { link node = new link(q.coef, q.exp); node.next = d; d = node; q = q.next; } first = d; } // end of function padd void disp() // To display the resultant polynomial { link current = first; // start at the beginning of the list while (current != null) // until end of the list { current.display(); if (current.next != null) { System.out.print("+"); } else { System.out.print(" "); // print data } current = current.next; // move to next link } System.out.println(" "); } // end of function disp } // end of class linklist public class Polyadd // The main class add { public static void main(String args[]) // main function { try // to catch any exceptions { int r = 0, n, x, y; System.out.println("/* POLYNOMIAL ADDITION */"); linklist A = new linklist(); // make new linklist 'A' linklist B = new linklist(); // make new linklist 'B' linklist C = new linklist(); // make new linklist 'C' BufferedReader f = new BufferedReader(new InputStreamReader( System.in)); for (int j = 1; j <= 2; j++) { // To insert the polynomials System.out.println("Enter the " + j + " polynomial:"); System.out.println("Enter the no. of terms:"); n = Integer.parseInt(f.readLine()); for (int i = n; i > 0; i--) { System.out.println("Enter the coeff & exponent of " + i + " term"); x = Integer.parseInt(f.readLine()); y = Integer.parseInt(f.readLine()); if (j == 1) A.create(x, y); // Assign values to links else B.create(x, y); // Assign values to links } } System.out.println("FIRST POLYNOMIAL IS:"); A.disp(); // Display the first plynomial System.out.println("SECOND POLYNOMIAL IS:"); B.disp(); // Display the second plynomial C.padd(A, B); // Calling the function padd System.out.println // ("THE SUM OF POLYNOMIALS IS:"); C.disp(); // Display the resultant plynomial } catch (IOException e) // To catch I/O Exception { } } // End of main function }