Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. package eg.edu.alexu.csd.datastructure.linkedList.cs2cs16;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class PolynomialSolver implements IPolynomialSolver {
  6.  
  7. DLinkedList A, B, C, R;
  8.  
  9. public void sort(int[][] arr)
  10. {
  11. int n = arr.length;
  12. for (int i = n - 2; i >= 0; i--) {
  13.  
  14. int keyCoef = arr[i][0];
  15. int keyExp = arr[i][1];
  16.  
  17. int j = i + 1;
  18.  
  19. while (j < n && arr[j][1] > keyCoef) {
  20.  
  21. arr[j - 1][0] = arr[j][0];
  22. arr[j - 1][1] = arr[j][1];
  23. j = j + 1;
  24. }
  25. arr[j - 1][1] = keyCoef;
  26. arr[j - 1][1] = keyExp;
  27. }
  28. }
  29.  
  30. public DLinkedList choose (char poly) {
  31.  
  32. DLinkedList n = new DLinkedList();
  33. if (poly == 'A') {
  34.  
  35. A = new DLinkedList();
  36. n = A;
  37. }
  38. else if (poly == 'B') {
  39.  
  40. B = new DLinkedList();
  41. n = B;
  42. }
  43. else if (poly == 'C') {
  44.  
  45. C = new DLinkedList();
  46. n = C;
  47. }
  48. return n;
  49. }
  50.  
  51. public void setPolynomial(char poly, int[][] terms) {
  52.  
  53. DLinkedList n = choose(poly);
  54. for (int i = 0; i < terms.length; i++) {
  55.  
  56. n.add(terms[i]);
  57. }
  58. }
  59.  
  60. public String print(char poly) {
  61.  
  62. DLinkedListNode n = choose(poly).head;
  63. String ans = "";
  64. int[] tmp = new int[2];
  65. while (n != null) {
  66.  
  67. tmp = (int[])n.value;
  68. if (tmp[0] == 0) {
  69.  
  70. continue;
  71. }
  72. else {
  73.  
  74. ans = ans + Integer.toString(tmp[0]);
  75. if (tmp[1] == 1) {
  76.  
  77. ans = ans + 'x';
  78. }
  79. else {
  80.  
  81. ans = ans + 'x' + '^' + tmp[1];
  82. }
  83. }
  84. n = n.next;
  85. }
  86. return ans;
  87. }
  88.  
  89. public void clearPolynomial(char poly) {
  90.  
  91. }
  92.  
  93. public float evaluatePolynomial(char poly, float value) {
  94. return 0;
  95. }
  96.  
  97. public int[][] add(char poly1, char poly2) {
  98. return new int[0][];
  99. }
  100.  
  101. public int[][] subtract(char poly1, char poly2) {
  102. return new int[0][];
  103. }
  104.  
  105. public int[][] multiply(char poly1, char poly2) {
  106. return new int[0][];
  107. }
  108.  
  109. public static void main(String[] args) {
  110.  
  111. PolynomialSolver polynomialSolver = new PolynomialSolver();
  112.  
  113. while (true) {
  114.  
  115. System.out.println("Please choose an action");
  116. System.out.println("-----------------------");
  117. System.out.println("1- Set a polynomial variable");
  118. System.out.println("2- Print the value of a polynomial variable");
  119. System.out.println("3- Add two polynomials");
  120. System.out.println("4- Subtract two polynomials");
  121. System.out.println("5- Multiply two polynomials");
  122. System.out.println("6- Evaluate a polynomial at some point");
  123. System.out.println("7- Clear a polynomial variable");
  124.  
  125. Scanner input = new Scanner(System.in).useDelimiter("\n");
  126. int action = input.nextInt();
  127. char variableName;
  128.  
  129. if (action == 1) {
  130.  
  131. do {
  132.  
  133. System.out.println("Insert the variable name: A, B or C");
  134. variableName = input.next().charAt(0);
  135. } while (variableName != 'A' && variableName != 'B' && variableName != 'C');
  136.  
  137. System.out.println("Insert the polynomial terms in the form:");
  138. System.out.println("(coeff1, exponent1), (coeff2, exponent2), ..");
  139. String line = input.next();
  140.  
  141. int counter = 0;
  142. for (int i = 0, len = line.length(); i < len; i++) {
  143.  
  144. if (line.charAt(i) == '(') {
  145.  
  146. counter++;
  147. }
  148. }
  149.  
  150. int[][] terms = new int[counter][2];
  151. int k = 0, len = line.length();
  152. for (int i = 0; i < counter; i++) {
  153. for (int j = 0; j < 2; j++) {
  154. for (; k < len; k++) {
  155. if (Character.isDigit(line.charAt(k))) {
  156.  
  157. terms[i][j] = Character.getNumericValue(line.charAt(k));
  158. k++;
  159. break;
  160. }
  161. }
  162. }
  163. }
  164. polynomialSolver.sort(terms);
  165.  
  166. polynomialSolver.setPolynomial(variableName, terms);
  167. }
  168.  
  169. if (action == 2) {
  170.  
  171. do {
  172.  
  173. System.out.println("Insert the variable name: A, B or C");
  174. variableName = input.next(".").charAt(0);
  175. } while (variableName != 'A' && variableName != 'B' && variableName != 'C');
  176.  
  177. System.out.println(polynomialSolver.print(variableName));
  178. }
  179.  
  180. }
  181.  
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement