fork download
  1. import java.io.*;
  2. import java.text.MessageFormat;
  3. import java.util.*;
  4.  
  5. class Node
  6. {
  7. public int factor;
  8. public int exponent;
  9. public Node next;
  10.  
  11. public Node()
  12. {
  13. factor=0;
  14. exponent=0;
  15. next=null;
  16. }
  17. public Node (int factor, int exponent, Node next)
  18. {
  19. this.factor=factor;
  20. this.exponent=exponent;
  21. this.next=next;
  22. }
  23. }
  24.  
  25. class PolynomialAddition {
  26. static File dataInpt;
  27. static Scanner inFile;
  28.  
  29. public static void main(String[] args) throws IOException {
  30. dataInpt = new File("/tmp/input.txt");
  31. inFile = new Scanner(dataInpt);
  32.  
  33. while (inFile.hasNextLine()) {
  34. Node first = readPolynomial();
  35. printList(first);
  36.  
  37. Node second = readPolynomial();
  38. printList(second);
  39.  
  40. Node merged = addPolynomials(first, second);
  41.  
  42. System.out.println("Really crude, next two lines\n\n");
  43. }
  44. }
  45.  
  46. private static Node addPolynomials(Node first, Node second) {
  47. return null;
  48. }
  49.  
  50. private static Node readPolynomial() {
  51. String line = inFile.nextLine();
  52. StringTokenizer myTokens = new StringTokenizer(line);
  53.  
  54. Node head = null, previous = null;
  55. while (myTokens.hasMoreTokens()) {
  56. Node current = new Node();
  57. String term = myTokens.nextToken();
  58.  
  59. current.factor = Integer.valueOf(
  60. term.substring(0, term.indexOf("x")));
  61. current.exponent = Integer.valueOf(
  62. term.substring(term.indexOf("^") + 1));
  63.  
  64. if (previous == null)
  65. {
  66. head = current;
  67. previous = head;
  68. } else
  69. {
  70. previous.next = current;
  71. previous = current;
  72. }
  73. }
  74. return head;
  75. }
  76.  
  77. public static void printList(Node head) {
  78. for (Node ptr = head; ptr != null; ptr = ptr.next)
  79. System.out.print(MessageFormat.format("{0} {1} ", ptr.factor,
  80. ptr.exponent));
  81. System.out.println();
  82. }
  83. }
  84.  
  85. public class Main {
  86. public static void main(String[] args) throws IOException {
  87. PolynomialAddition go = new PolynomialAddition();
  88. go.main(args);
  89. }
  90. }
Runtime error #stdin #stdout 0.1s 212864KB
stdin
Standard input is empty
stdout
Standard output is empty