Well, after a long chat, this is what we “came up with”. I understand that this only erodes the answer.
However, with a solid implementation in a clean style, Java 1.4 code could do a lot to help you understand.
Particular attention was paid to printing the result in tabular form, aligning the terms of different operands in the columns of their corresponding indicator.
the code
There are two files:
Node.java
class Node { int factor; int exponent; Node next; public Node() { factor = 0; exponent = 0; next = null; } public Node(int factor, int exponent, Node next) { this.factor = factor; this.exponent = exponent; this.next = next; } public String toString() { return String.format("%+4dx^%d ", new Integer[] { new Integer(factor), new Integer(exponent) }); } }
PolynomialAddition.java
import java.io.*; import java.util.*; public class PolynomialAddition { static File dataInpt; static Scanner inFile; public static void main(String[] args) throws IOException { dataInpt = new File("/tmp/input.txt"); inFile = new Scanner(dataInpt); while (inFile.hasNextLine()) { Node first = readPolynomial(); // printList(first); Node second = readPolynomial(); // printList(second); Node addition = addPolynomials(first, second); // printList(addition); printTabulated(first, second, addition); System.out.println("\n"); } } private static Node addPolynomials(Node first, Node second) { Node head = null, current = null; while (null!=first || null!=second) { boolean pickfirst = false; boolean haveBoth = (null!=first && null!=second); Node node; if (haveBoth && first.exponent == second.exponent) { node = new Node(first.factor + second.factor, first.exponent, null); first = first.next; second = second.next; } else { pickfirst = first!=null && ((second == null) || first.exponent > second.exponent); if (pickfirst) { node = new Node(first.factor, first.exponent, null); first = first.next; } else { node = new Node(second.factor, second.exponent, null); second = second.next; } } if (current == null) { head = node; current = head; } else { current.next = node; current = node; } } return head; } private static void printTabulated(Node first, Node second, Node addition) { String line1="", line2="", barline="", line3=""; while (addition != null) { String part1 = " ", part2 = " ", part3 = " "; if (null!=first && first.exponent == addition.exponent) { part1 = first.toString(); first = first.next; } if (null!=second && second.exponent == addition.exponent) { part2 = second.toString(); second = second.next; } part3 = addition.toString(); addition = addition.next; line1 += part1; line2 += part2; barline += "-----------"; line3 += part3; } System.out.println(line1); System.out.println(line2); System.out.println(barline); System.out.println(line3); } private static Node readPolynomial() { String line = inFile.nextLine(); StringTokenizer myTokens = new StringTokenizer(line); Node head = null, previous = null; while (myTokens.hasMoreTokens()) { Node current = new Node(); String term = myTokens.nextToken(); if (term.startsWith("+")) term = term.substring(1); current.factor = Integer.parseInt( term.substring(0, term.indexOf("x"))); current.exponent = Integer.parseInt( term.substring(term.indexOf("^") + 1)); if (previous == null) { head = current; previous = head; } else { previous.next = current; previous = current; } } return head; } private static void printList(Node head) { for (Node ptr = head; ptr != null; ptr = ptr.next) System.out.print(ptr); System.out.println(); } }
Sample data:
Input:
2x^4 -5x^3 +9x^2 -10x^0 3x^4 -6x^3 +10x^2 -11x^0 -2x^1 +4x^0 2x^1 -4x^0 8x^5 +6x^4 +5x^2 -3x^0 -12x^8 +2x^7 +14x^5 1x^5 +7x^2 +8x^1 -5x^4 -7x^3 -4x^2 -3x^0 10x^5 -3x^3 +4x^2 -234x^1 -12x^0 -5x^5 -2x^3 +10x^0
Output:
<sub>
+2x^4 -5x^3 +9x^2 -10x^0 +3x^4 -6x^3 +10x^2 -11x^0
sub>