Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 KB | None | 0 0
  1.  
  2. import java.io.IOException;
  3. import java.util.Scanner;
  4.  
  5. class Term {
  6. public float coeff;
  7. public int degree;
  8. public Term(float coeff, int degree) {
  9. this.coeff = coeff; this.degree = degree;
  10. }
  11. public boolean equals(Object other) {
  12. return other != null && other instanceof Term && coeff == ((Term)other).coeff && degree == ((Term)other).degree;
  13. }
  14.  
  15. public String toString() {
  16. if (degree == 0) {
  17. return coeff + "";
  18. } else if (degree == 1) {
  19. return coeff + "x";
  20. } else {
  21. return coeff + "x^" + degree;
  22. }
  23. }
  24. }
  25.  
  26.  
  27. class Node{
  28. Term term;
  29. Node next;
  30. public Node(float coeff, int degree, Node next) {
  31. term = new Term(coeff, degree); this.next = next;
  32. }
  33. }
  34.  
  35. public class Polynomial {
  36.  
  37. /**
  38. * Reads a polynomial from an input stream (file or keyboard). The storage
  39. * format of the polynomial is:
  40. *
  41. * <pre>
  42. * <coeff> <degree>
  43. * <coeff> <degree>
  44. * ...
  45. * <coeff> <degree>
  46. * </pre>
  47. *
  48. * with the guarantee that degrees will be in descending order. For example:
  49. *
  50. * <pre>
  51. * 4 5
  52. * -2 3
  53. * 2 1
  54. * 3 0
  55. * </pre>
  56. *
  57. * which represents the polynomial:
  58. *
  59. * <pre>
  60. * 4 * x ^ 5 - 2 * x ^ 3 + 2 * x + 3
  61. * </pre>
  62. *
  63. * @param sc Scanner from which a polynomial is to be read
  64. * @throws IOException If there is any input error in reading the polynomial
  65. * @return The polynomial linked list (front node) constructed from coefficients
  66. * and degrees read from scanner
  67. */
  68. public static Node read(Scanner sc) throws IOException {
  69. Node poly = null;
  70. while (sc.hasNextLine()) {
  71. Scanner scLine = new Scanner(sc.nextLine());
  72. poly = new Node(scLine.nextFloat(), scLine.nextInt(), poly);
  73. scLine.close();
  74. }
  75. return poly;
  76. }
  77.  
  78. /**
  79. * Returns the sum of two polynomials - DOES NOT change either of the input
  80. * polynomials. The returned polynomial MUST have all new nodes. In other words,
  81. * none of the nodes of the input polynomials can be in the result.
  82. *
  83. * @param poly1 First input polynomial (front of polynomial linked list)
  84. * @param poly2 Second input polynomial (front of polynomial linked list
  85. * @return A new polynomial which is the sum of the input polynomials - the
  86. * returned node is the front of the result polynomial
  87. */
  88. public static Node add(Node poly1, Node poly2) {
  89. Node poly = null;
  90.  
  91. Node p1 = poly1;
  92. while (p1 != null) {
  93. poly = addTerm(poly, p1.term.coeff, p1.term.degree);
  94. p1 = p1.next;
  95. }
  96. Node p2 = poly2;
  97. while (p2 != null) {
  98. poly = addTerm(poly, p2.term.coeff, p2.term.degree);
  99. p2 = p2.next;
  100. }
  101.  
  102. return poly;
  103. }
  104.  
  105. /**
  106. * Returns the product of two polynomials - DOES NOT change either of the input
  107. * polynomials. The returned polynomial MUST have all new nodes. In other words,
  108. * none of the nodes of the input polynomials can be in the result.
  109. *
  110. * @param poly1 First input polynomial (front of polynomial linked list)
  111. * @param poly2 Second input polynomial (front of polynomial linked list)
  112. * @return A new polynomial which is the product of the input polynomials - the
  113. * returned node is the front of the result polynomial
  114. */
  115. public static Node multiply(Node poly1, Node poly2) {
  116. Node poly = null;
  117.  
  118. while(poly1.next != null) {
  119.  
  120. Node start = poly2;
  121. while(start != null) {
  122. poly = addTerm(poly, poly1.term.coeff * start.term.coeff,
  123. poly1.term.degree + start.term.degree);
  124. start = start.next;
  125. }
  126.  
  127. poly1 = poly1.next;
  128. }
  129.  
  130. return poly;
  131. }
  132.  
  133. // helper method to add a node in already existing list
  134. // The passed list poly is modified
  135. private static Node addTerm(Node poly, float coeff, int degree) {
  136. if(coeff == 0)
  137. return poly;
  138.  
  139. // create node
  140. Node n = new Node(coeff, degree, null);
  141.  
  142. // if it is the first element
  143. if (poly == null) {
  144. return n;
  145. } else {
  146.  
  147. // if it has same exponent as head
  148. if (poly.term.degree == degree) {
  149. poly.term.coeff = (poly.term.coeff + coeff);
  150. return poly;
  151. } else if (poly.term.degree < degree) {
  152. // it need to be inserted before head
  153. n.next = poly;
  154. poly = n;
  155. return poly;
  156. }
  157.  
  158. Node p = poly;
  159. while (p.next != null) {
  160.  
  161. if (degree == p.term.degree) {
  162. // if same exponent term is there
  163. p.term.coeff = (p.term.coeff + coeff);
  164. return poly;
  165. } else if (degree > p.next.term.degree) {
  166. // loop till we get some lower exponent position
  167. n.next = p.next;
  168. p.next = n;
  169. return poly;
  170. }
  171.  
  172. // move to next
  173. p = p.next;
  174. }
  175.  
  176. if(p.term.degree == degree) {
  177. p.term.coeff = (p.term.coeff + coeff);
  178. return poly;
  179. }
  180.  
  181. // add to the last
  182. p.next = n;
  183. return poly;
  184. }
  185. }
  186.  
  187. /**
  188. * Evaluates a polynomial at a given value.
  189. *
  190. * @param poly Polynomial (front of linked list) to be evaluated
  191. * @param x Value at which evaluation is to be done
  192. * @return Value of polynomial p at x
  193. */
  194. public static float evaluate(Node poly, float x) {
  195. float result = 0;
  196.  
  197. while(poly != null) {
  198. result += poly.term.coeff * Math.pow(x, poly.term.degree);
  199. poly = poly.next;
  200. }
  201.  
  202. return result;
  203. }
  204.  
  205. /**
  206. * Returns string representation of a polynomial
  207. *
  208. * @param poly Polynomial (front of linked list)
  209. * @return String representation, in descending order of degrees
  210. */
  211. public static String toString(Node poly) {
  212. if (poly == null) {
  213. return "0";
  214. }
  215.  
  216. String retval = poly.term.toString();
  217. for (Node current = poly.next; current != null; current = current.next) {
  218. retval = current.term.toString() + " + " + retval;
  219. }
  220. return retval;
  221. }
  222. }
  223. Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!
  224.  
  225. 11 Comments
  226. Comments
  227. Anonymous's AvatarAnonymous posted 1 year ago
  228. this is the node class public class Node{ Term term; Node next; public Node(float coeff, int degree, Node next) { term = new Term(coeff, degree); this.next = next; } }
  229. Anonymous's AvatarAnonymous posted 1 year ago
  230. where is term class?
  231. Anonymous's AvatarAnonymous posted 1 year ago
  232. public class Term { public float coeff; public int degree; public Term(float coeff, int degree) { this.coeff = coeff; this.degree = degree; } public boolean equals(Object other) { return other != null && other instanceof Term && coeff == ((Term)other).coeff && degree == ((Term)other).degree; }
  233. Anonymous's AvatarAnonymous posted 1 year ago
  234. public String toString() { if (degree == 0) { return coeff + ""; } else if (degree == 1) { return coeff + "x"; } else { return coeff + "x^" + degree; } }
  235. Anonymous's AvatarAnonymous posted 1 year ago
  236. i have updated the answer.. pleas check
  237. Anonymous's AvatarAnonymous posted 1 year ago
  238. thanks a lot now it works I really appreciate the help
  239. Anonymous's AvatarAnonymous posted 1 year ago
  240. Welcome!
  241. Anonymous's AvatarAnonymous posted 1 year ago
  242. Can you help with the part where if we add two opposite polynomials, one is negative, one is positive, it returns only 0? The code returns, 0x^5+0x^3... and so on
  243. Anonymous's AvatarAnonymous posted 1 year ago
  244. Can you please post a separate question with full details.. like what is input and output.. and we willl help.
  245. Anonymous's AvatarAnonymous posted 8 months ago
  246. Sir, this is a 5 months old question, we are not allowed to re-open old questions.. the person who posted this question has different requirements which were met.. now if you have any specific requirements, please post a new question with your issues.. we will surely help. Thanks!
  247. Anonymous's AvatarAnonymous posted 3 days ago
  248. Where is the addTerm function you called in class add?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement