Advertisement
Guest User

sadasdasd

a guest
Feb 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. import java.util.LinkedList;
  2.  
  3. public class LinkedPolynomial {
  4. private Node first = new Node(0, 0); // sentinel
  5. private Node last = first;
  6.  
  7. private static class Node {
  8. int coef;
  9. int exp;
  10. Node next;
  11. Node(int coef, int exp) {
  12. this.coef = coef;
  13. this.exp = exp;
  14. }
  15. }
  16.  
  17. // 0
  18. private LinkedPolynomial() { }
  19.  
  20. // a * x^b
  21. public LinkedPolynomial(int coef, int exp) {
  22. last.next = new Node(coef, exp);
  23. last = last.next;
  24. }
  25.  
  26. // return c = a + b
  27. public LinkedPolynomial plus(LinkedPolynomial b) {
  28. LinkedPolynomial a = this;
  29. LinkedPolynomial c = new LinkedPolynomial();
  30. Node x = a.first.next;
  31. Node y = b.first.next;
  32. while (x != null || y != null) {
  33. Node t = null;
  34. if (x == null) { t = new Node(y.coef, y.exp); y = y.next; }
  35. else if (y == null) { t = new Node(x.coef, x.exp); x = x.next; }
  36. else if (x.exp > y.exp) { t = new Node(x.coef, x.exp); x = x.next; }
  37. else if (x.exp < y.exp) { t = new Node(y.coef, y.exp); y = y.next; }
  38.  
  39. else {
  40. int coef = x.coef + y.coef;
  41. int exp = x.exp;
  42. x = x.next;
  43. y = y.next;
  44. if (coef == 0) continue;
  45. t = new Node(coef, exp);
  46. }
  47.  
  48. c.last.next = t;
  49. c.last = c.last.next;
  50. }
  51. return c;
  52. }
  53.  
  54.  
  55. // return c = a + b
  56. public LinkedPolynomial times(LinkedPolynomial b) {
  57. LinkedPolynomial a = this;
  58. LinkedPolynomial c = new LinkedPolynomial();
  59. for (Node x = a.first.next; x!= null; x = x.next) {
  60. LinkedPolynomial temp = new LinkedPolynomial();
  61. for (Node y = b.first.next; y!= null; y = y.next) {
  62. temp.last.next = new Node(x.coef * y.coef, x.exp + y.exp);
  63. temp.last = temp.last.next;
  64. }
  65. c = c.plus(temp);
  66. }
  67. return c;
  68. }
  69.  
  70.  
  71. // convert to string representation
  72. public String toString() {
  73. String s = "";
  74. for (Node x = first.next; x != null; x = x.next) {
  75. if (x.coef > 0) s = s + " + " + x.coef + "x^" + x.exp;
  76. else if (x.coef < 0) s = s + " - " + (-x.coef) + "x^" + x.exp;
  77. }
  78. return s;
  79. }
  80.  
  81. // test client
  82. public static void main(String[] args) {
  83. LinkedPolynomial zero = new LinkedPolynomial(0, 0);
  84.  
  85. LinkedPolynomial p1 = new LinkedPolynomial(4, 3);
  86. LinkedPolynomial p2 = new LinkedPolynomial(3, 2);
  87. LinkedPolynomial p3 = new LinkedPolynomial(1, 0);
  88. LinkedPolynomial p4 = new LinkedPolynomial(2, 1);
  89. LinkedPolynomial p = p1.plus(p2).plus(p3).plus(p4); // 4x^3 + 3x^2 + 1
  90.  
  91. LinkedPolynomial q1 = new LinkedPolynomial(3, 2);
  92. LinkedPolynomial q2 = new LinkedPolynomial(5, 0);
  93. LinkedPolynomial q = q1.plus(q2); // 3x^2 + 5
  94.  
  95. LinkedPolynomial r = p.plus(q1);
  96. LinkedPolynomial s = p.times(q);
  97. System.out.println("zero(x) = " + zero.toString());;
  98. System.out.println("p(x) + q(x) = " + r.toString());
  99. System.out.println("p(x) * q(x) = " + s.toString());
  100. // System.out.println("0 - p(x) = " + zero.minus(p));
  101. // System.out.println("p(3) = " + p.evaluate(3));
  102. // System.out.println("p'(x) = " + p.differentiate());
  103. // System.out.println("p''(x) = " + p.differentiate().differentiate());
  104. }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement