Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.85 KB | None | 0 0
  1. package eg.edu.alexu.csd.datastructure.linkedList.cs2cs16;
  2.  
  3. import java.util.InputMismatchException;
  4. import java.util.Scanner;
  5.  
  6. public class PolynomialSolver implements IPolynomialSolver {
  7.  
  8. private DLinkedList A = new DLinkedList();
  9. private DLinkedList B = new DLinkedList();
  10. private DLinkedList C = new DLinkedList();
  11. private DLinkedList R = new DLinkedList();
  12. private static Scanner input = new Scanner(System.in).useDelimiter("\n");
  13. static int action = 0;
  14.  
  15. private void sort(int[][] arr) {
  16. int n = arr.length;
  17. for (int i = n - 2; i >= 0; i--) {
  18.  
  19. int keyCoef = arr[i][0];
  20. int keyExp = arr[i][1];
  21.  
  22. int j = i + 1;
  23.  
  24. while (j < n && arr[j][1] > keyExp) {
  25.  
  26. arr[j - 1][0] = arr[j][0];
  27. arr[j - 1][1] = arr[j][1];
  28. j = j + 1;
  29. }
  30. arr[j - 1][0] = keyCoef;
  31. arr[j - 1][1] = keyExp;
  32. }
  33. }
  34.  
  35. private DLinkedList choose(char poly) {
  36.  
  37. DLinkedList n = new DLinkedList();
  38. if (poly == 'A') {
  39.  
  40. n = A;
  41. } else if (poly == 'B') {
  42.  
  43. n = B;
  44. } else if (poly == 'C') {
  45.  
  46. n = C;
  47. } else if (poly == 'R') {
  48.  
  49. n = R;
  50. }
  51. return n;
  52. }
  53.  
  54. private int[][] listtoArr(DLinkedList list) {
  55.  
  56. DLinkedListNode n = list.head;
  57. int[][] ans = new int[list.size()][2];
  58. int[] tmp;
  59. int i = 0;
  60.  
  61. while (n != null) {
  62.  
  63. tmp = (int[]) n.value;
  64. ans[i][0] = tmp[0];
  65. ans[i][1] = tmp[1];
  66. n = n.next;
  67. i++;
  68. }
  69. return ans;
  70. }
  71.  
  72. private int[][] removeElement(int[][] arr, int index)
  73. {
  74.  
  75. int[][] anotherArray = new int[arr.length - 1][2];
  76. for (int i = 0; i < index; i++) {
  77.  
  78. System.arraycopy(arr[i], 0, anotherArray[i], 0, 2);
  79. }
  80. for (int i = index + 1; i < arr.length; i++) {
  81.  
  82. System.arraycopy(arr[i], 0, anotherArray[i - 1], 0, 2);
  83. }
  84. return anotherArray;
  85. }
  86.  
  87. private char checkInput() {
  88.  
  89. PolynomialSolver polynomialSolver = new PolynomialSolver();
  90. char variableName;
  91. do {
  92.  
  93. System.out.println("Insert the variable name: A, B, C or R");
  94. variableName = input.next().charAt(0);
  95. } while (variableName != 'A' && variableName != 'B' && variableName != 'C' && variableName != 'R');
  96.  
  97. if (action > 1 && action < 7) {
  98. while (!polynomialSolver.isNull(variableName) || (variableName != 'A' && variableName != 'B' && variableName != 'C' && variableName != 'R')) {
  99.  
  100. System.out.println("Variable not set");
  101. System.out.println("Insert the variable name: A, B, C or R");
  102. variableName = input.next().charAt(0);
  103. }
  104. }
  105. return variableName;
  106. }
  107.  
  108. private boolean isNull(char variableName) {
  109.  
  110. PolynomialSolver polynomialSolver = new PolynomialSolver();
  111. return polynomialSolver.choose(variableName).head == null;
  112. }
  113.  
  114. private int pow (int a, int b)
  115. {
  116. if (b == 0) return 1;
  117. if (b == 1) return a;
  118. if (b % 2 == 0) return pow ( a * a, b/2); //even a=(a^2)^b/2
  119. else return a * pow ( a * a, b/2); //odd a=a*(a^2)^b/2
  120. }
  121.  
  122. public void setPolynomial(char poly, int[][] terms) {
  123.  
  124. DLinkedList n = choose(poly);
  125. for (int[] term : terms) {
  126.  
  127. n.add(term);
  128. }
  129. }
  130.  
  131. public String print(char poly) {
  132.  
  133. DLinkedListNode n = choose(poly).head;
  134. String ans = "";
  135. int[] tmp = (int[]) n.value;
  136.  
  137. if (tmp[0] != 0) {
  138.  
  139. if (tmp[1] == 0) {
  140.  
  141. ans = ans + tmp[0];
  142. } else {
  143.  
  144. if (tmp[0] == 1 && tmp[1] == 1) {
  145.  
  146. ans = ans + 'x';
  147. } else if (tmp[0] == -1 && tmp[1] == 1) {
  148.  
  149. ans = ans + "-x";
  150. } else if (tmp[0] == 1) {
  151.  
  152. ans = ans + "x^" + tmp[1];
  153. } else if (tmp[0] == -1) {
  154.  
  155. ans = ans + "-x^" + tmp[1];
  156. } else {
  157.  
  158. ans = ans + tmp[0] + "x^" + tmp[1];
  159. }
  160. }
  161. }
  162.  
  163. n = n.next;
  164. while (n != null) {
  165.  
  166. tmp = (int[]) n.value;
  167. if (tmp[0] < 0) {
  168.  
  169. ans = ans + " - ";
  170. tmp[0] = -tmp[0];
  171. } else {
  172.  
  173. ans = ans + " + ";
  174. }
  175.  
  176. if (tmp[0] == 0) {
  177.  
  178. } else if (tmp[0] == 1 && tmp[1] == 0) {
  179.  
  180. ans = ans + '1';
  181. } else {
  182.  
  183. if (tmp[0] == 1 && tmp[1] == 1) {
  184.  
  185. ans = ans + 'x';
  186. } else if (tmp[1] == 1) {
  187.  
  188. ans = ans + tmp[0] + 'x';
  189. } else if (tmp[1] == 0) {
  190.  
  191. ans = ans + tmp[0];
  192. } else {
  193.  
  194. ans = ans + tmp[0] + "x^" + tmp[1];
  195. }
  196. }
  197. n = n.next;
  198. }
  199. return ans;
  200. }
  201.  
  202. public void clearPolynomial(char poly) {
  203.  
  204. DLinkedList n = choose(poly);
  205. n.clear();
  206. }
  207.  
  208. public float evaluatePolynomial(char poly, float value) {
  209.  
  210. DLinkedListNode n = choose(poly).head;
  211. float sum = 0;
  212. int[] tmp;
  213. while (n != null) {
  214.  
  215. tmp = (int[]) n.value;
  216. if (tmp[0] == 0) {
  217.  
  218. continue;
  219. } else {
  220.  
  221. sum += (tmp[0] * Math.pow(value, tmp[1]));
  222. }
  223. n = n.next;
  224. }
  225. return sum;
  226. }
  227.  
  228. public int[][] add(char poly1, char poly2) {
  229.  
  230. R.clear();
  231. DLinkedListNode n1 = choose(poly1).head;
  232. DLinkedListNode n2 = choose(poly2).head;
  233. int[] n1_tmp, n2_tmp;
  234.  
  235. while (n1 != null && n2 != null) {
  236.  
  237. n1_tmp = (int[]) n1.value;
  238. n2_tmp = (int[]) n2.value;
  239. if (n1_tmp[1] == n2_tmp[1]) {
  240.  
  241. int[] ans_tmp = new int[2];
  242. ans_tmp[0] = n1_tmp[0] + n2_tmp[0];
  243. ans_tmp[1] = n1_tmp[1];
  244. R.add(ans_tmp);
  245. n1 = n1.next;
  246. n2 = n2.next;
  247. }
  248. else if (n1_tmp[1] > n2_tmp[1]) {
  249.  
  250. R.add(n1_tmp);
  251. n1 = n1.next;
  252. }
  253. else {
  254.  
  255. R.add(n2_tmp);
  256. n2 = n2.next;
  257. }
  258. }
  259.  
  260. if (n1 != null) {
  261. while (n1 != null) {
  262.  
  263. n1_tmp = (int[]) n1.value;
  264. R.add(n1_tmp);
  265. n1 = n1.next;
  266. }
  267. }
  268. if (n2 != null) {
  269. while (n2 != null) {
  270.  
  271. n2_tmp = (int[]) n2.value;
  272. R.add(n2_tmp);
  273. n2 = n2.next;
  274. }
  275. }
  276.  
  277. PolynomialSolver polynomialSolver = new PolynomialSolver();
  278. return polynomialSolver.listtoArr(R);
  279. }
  280.  
  281. public int[][] subtract(char poly1, char poly2) {
  282.  
  283. R.clear();
  284. DLinkedListNode n1 = choose(poly1).head;
  285. DLinkedListNode n2 = choose(poly2).head;
  286. int[] n1_tmp, n2_tmp;
  287.  
  288. while (n1 != null && n2 != null) {
  289.  
  290. n1_tmp = (int[]) n1.value;
  291. n2_tmp = (int[]) n2.value;
  292. if (n1_tmp[1] == n2_tmp[1]) {
  293.  
  294. int[] ans_tmp = new int[2];
  295. ans_tmp[0] = n1_tmp[0] - n2_tmp[0];
  296. ans_tmp[1] = n1_tmp[1];
  297. R.add(ans_tmp);
  298. n1 = n1.next;
  299. n2 = n2.next;
  300. }
  301. else if (n1_tmp[1] > n2_tmp[1]) {
  302.  
  303. R.add(n1_tmp);
  304. n1 = n1.next;
  305. }
  306. else {
  307.  
  308. int[] ans_tmp = new int[2];
  309. ans_tmp[0] = -n2_tmp[0];
  310. ans_tmp[1] = n2_tmp[1];
  311. R.add(ans_tmp);
  312. n2 = n2.next;
  313. }
  314. }
  315. if (n1 != null) {
  316. while (n1 != null) {
  317.  
  318. n1_tmp = (int[]) n1.value;
  319. R.add(n1_tmp);
  320. n1 = n1.next;
  321. }
  322. }
  323. if (n2 != null) {
  324. while (n2 != null) {
  325.  
  326. int[] ans_tmp = new int[2];
  327. n2_tmp = (int[]) n2.value;
  328. ans_tmp[0] = -n2_tmp[0];
  329. ans_tmp[1] = n2_tmp[1];
  330. R.add(ans_tmp);
  331. n2 = n2.next;
  332. }
  333. }
  334. PolynomialSolver polynomialSolver = new PolynomialSolver();
  335. return polynomialSolver.listtoArr(R);
  336. }
  337.  
  338. public int[][] multiply(char poly1, char poly2) {
  339.  
  340. R.clear();
  341. DLinkedListNode n1 = choose(poly1).head;
  342. DLinkedListNode n2 = choose(poly2).head;
  343. int[] n1_tmp, n2_tmp;
  344. int size = choose(poly1).size() * choose(poly2).size();
  345. int[][] ans = new int[size][2];
  346. int k = 0;
  347.  
  348. while (n1 != null) {
  349.  
  350. n1_tmp = (int[]) n1.value;
  351. while (n2 != null) {
  352.  
  353. n2_tmp = (int[]) n2.value;
  354. ans[k][0] = n1_tmp[0] * n2_tmp[0];
  355. ans[k][1] = n1_tmp[1] + n2_tmp[1];
  356. n2 = n2.next;
  357. k++;
  358. }
  359. n2 = choose(poly2).head;
  360. n1 = n1.next;
  361. }
  362.  
  363. PolynomialSolver polynomialSolver = new PolynomialSolver();
  364. polynomialSolver.sort(ans);
  365.  
  366. for (int i = 0; i < ans.length - 1; i++) {
  367. if (ans[i][1] == ans[i + 1][1]) {
  368.  
  369. ans[i][0] += ans[i + 1][0];
  370. ans = polynomialSolver.removeElement(ans, i + 1);
  371. }
  372. }
  373.  
  374. for (int[] term : ans) {
  375.  
  376. R.add(term);
  377. }
  378. return polynomialSolver.listtoArr(R);
  379. }
  380.  
  381. public static void main(String[] args) {
  382.  
  383. PolynomialSolver polynomialSolver = new PolynomialSolver();
  384.  
  385. while (action != 8) {
  386.  
  387. System.out.println("Please choose an action");
  388. System.out.println("-----------------------");
  389. System.out.println("1- Set a polynomial variable");
  390. System.out.println("2- Print the value of a polynomial variable");
  391. System.out.println("3- Add two polynomials");
  392. System.out.println("4- Subtract two polynomials");
  393. System.out.println("5- Multiply two polynomials");
  394. System.out.println("6- Evaluate a polynomial at some point");
  395. System.out.println("7- Clear a polynomial variable");
  396. System.out.println("8- Exit");
  397.  
  398. try {
  399. action = input.nextInt();
  400.  
  401. } catch (InputMismatchException e) {
  402. System.out.println("Enter valid number!");
  403. input.next();
  404. }
  405.  
  406. if (action == 1) {
  407.  
  408. char variable = polynomialSolver.checkInput();
  409. System.out.println("Insert the polynomial terms in the form:");
  410. System.out.println("(coeff1, exponent1), (coeff2, exponent2), ..");
  411. String line = input.next();
  412.  
  413. int counter = 0;
  414. for (int i = 0, len = line.length(); i < len; i++) {
  415.  
  416. if (line.charAt(i) == '(') {
  417.  
  418. counter++;
  419. }
  420. }
  421.  
  422. int[][] terms = new int[counter][2];
  423. boolean negative;
  424. int k = 0, len = line.length();
  425. for (int i = 0; i < counter; i++) {
  426. for (int j = 0; j < 2; j++) {
  427.  
  428. negative = false;
  429. for (; k < len; k++) {
  430.  
  431. if (line.charAt(k) == '-') {
  432.  
  433. negative = true;
  434. }
  435. if (Character.isDigit(line.charAt(k)) && Character.isDigit(line.charAt(k + 1))) {
  436.  
  437. terms[i][j] = Character.getNumericValue(line.charAt(k)) * 10 + Character.getNumericValue(line.charAt(k + 1));
  438. k++;
  439. if (negative) {
  440.  
  441. terms[i][j] = -terms[i][j];
  442. }
  443. k++;
  444. break;
  445. }
  446. else if (Character.isDigit(line.charAt(k))) {
  447.  
  448. terms[i][j] = Character.getNumericValue(line.charAt(k));
  449. if (negative) {
  450.  
  451. terms[i][j] = -terms[i][j];
  452. }
  453. k++;
  454. break;
  455. }
  456.  
  457. }
  458. }
  459. }
  460. polynomialSolver.sort(terms);
  461. polynomialSolver.setPolynomial(variable, terms);
  462. }
  463.  
  464. if (action == 2) {
  465.  
  466. char variable = polynomialSolver.checkInput();
  467. System.out.println(polynomialSolver.print(variable));
  468. }
  469.  
  470. if (action == 3) {
  471.  
  472. char variable1 = polynomialSolver.checkInput();
  473. char variable2 = polynomialSolver.checkInput();
  474. polynomialSolver.add(variable1, variable2);
  475. System.out.println("Result = " + polynomialSolver.print('R'));
  476. }
  477. if (action == 4) {
  478.  
  479. char variable1 = polynomialSolver.checkInput();
  480. char variable2 = polynomialSolver.checkInput();
  481. polynomialSolver.subtract(variable1, variable2);
  482. System.out.println("Result = " + polynomialSolver.print('R'));
  483. }
  484.  
  485. if (action == 5) {
  486.  
  487. char variable1 = polynomialSolver.checkInput();
  488. char variable2 = polynomialSolver.checkInput();
  489. polynomialSolver.multiply(variable1, variable2);
  490. System.out.println("Result = " + polynomialSolver.print('R'));
  491. }
  492.  
  493. if (action == 6) {
  494.  
  495. char variable = polynomialSolver.checkInput();
  496. System.out.println("Insert the substituted number");
  497. float no = input.nextFloat();
  498. System.out.print("The polynomial at point " + no + " = ");
  499. System.out.println(polynomialSolver.evaluatePolynomial(variable, no));
  500. }
  501. if (action == 7) {
  502.  
  503. char variable = polynomialSolver.checkInput();
  504. polynomialSolver.clearPolynomial(variable);
  505. System.out.println("Polynomial " + variable + " is cleared.");
  506. }
  507. }
  508. }
  509. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement