fork download
  1. import java.io.*;
  2. import java.util.*;
  3. public class PolynomialAddition
  4. {
  5. static File dataInpt;
  6. static Scanner inFile;
  7.  
  8. public static void main(String[] args) throws IOException
  9. {
  10. dataInpt=new File("C:\\llpoly.txt");
  11. inFile=new Scanner(dataInpt);
  12. StringTokenizer myTokens;
  13. String line, sentence;
  14. Node parent = new Node();
  15. while (inFile.hasNextLine())
  16. {
  17. Node tempParent = parent;
  18. line=inFile.nextLine();
  19. myTokens=new StringTokenizer(line);
  20. while (myTokens.hasMoreTokens())
  21. {
  22. Node current = new Node();
  23. String polyTerm=myTokens.nextToken();
  24.  
  25. current.value=polyTerm.substring(0,polyTerm.indexOf("x"));
  26. current.value2=polyTerm.substring(polyTerm.indexOf("^")+1);
  27.  
  28. if(parent==null)
  29. parent.value=current.value;
  30. else
  31. {
  32. parent.next=current;
  33. parent=current;
  34. }
  35. }
  36. printList(tempParent.next);
  37.  
  38.  
  39. Node tempParent2 = parent;
  40. line=inFile.nextLine();
  41. myTokens=new StringTokenizer(line);
  42. while (myTokens.hasMoreTokens())
  43. {
  44. Node current = new Node();
  45. String polyTerm=myTokens.nextToken();
  46.  
  47. current.value=polyTerm.substring(0,polyTerm.indexOf("x"));
  48. current.value2=polyTerm.substring(polyTerm.indexOf("^")+1);
  49.  
  50. if(parent==null)
  51. parent.value=current.value;
  52. else
  53. {
  54. parent.next=current;
  55. parent=current;
  56. }
  57. }
  58. printList(tempParent2.next);
  59. System.out.println("Really crude, next two lines\n\n");
  60.  
  61. }
  62. }
  63. public static void printList(Node head)
  64. {
  65. Node ptr; //not pointing anywhere
  66. for(ptr=head;ptr!=null;ptr=ptr.next)
  67. System.out.print(ptr.value + " " + ptr.value2 + " ");
  68. System.out.println();
  69. }
  70. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty