Advertisement
Guest User

Untitled

a guest
Apr 27th, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.94 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.StringBuilder;
  3.  
  4. public class ArrayWithExponentAsIndexPolynomial implements PolynomialInterface {
  5.  
  6.  
  7. String originalString;
  8. ArrayList<Integer> polyCoeList = new ArrayList<Integer>();
  9. ArrayList<Integer> constantList = new ArrayList<Integer>();
  10.  
  11. public ArrayWithExponentAsIndexPolynomial(String polyString) {
  12.  
  13. originalString = polyString;
  14. String polyStringStripped = polyString.replaceAll("\\s+","");
  15. int tempNumCount = 0;
  16. for (int i = 0; i < polyStringStripped.length(); i++){
  17. if(polyStringStripped.charAt(i) == 'x') {
  18. if(getExponent(polyStringStripped, i) > polyCoeList.size()) {
  19.  
  20. for(int n = 0; n <= getExponent(polyStringStripped, i); n++) {
  21. polyCoeList.add(polyCoeList.size(),0);
  22. }
  23. }
  24. try {
  25. if(polyStringStripped.charAt(i - 1) == '-') {
  26. if(polyCoeList.get(getExponent(polyStringStripped, i)) != 0) {
  27. polyCoeList.set(getExponent(polyStringStripped, i), -1 + polyCoeList.get(getExponent(polyStringStripped, i)));
  28. }
  29.  
  30. else {
  31. polyCoeList.set(getExponent(polyStringStripped, i), -1);
  32. }
  33. }
  34.  
  35. else if(polyStringStripped.charAt(i - 1) == '+') {
  36. if(polyCoeList.get(getExponent(polyStringStripped, i)) != 0) {
  37. polyCoeList.set(getExponent(polyStringStripped, i), 1 + polyCoeList.get(getExponent(polyStringStripped, i)));
  38. }
  39.  
  40. else {
  41. polyCoeList.set(getExponent(polyStringStripped, i), 1);
  42. }
  43.  
  44. }
  45.  
  46. else if(Character.isDigit(polyStringStripped.charAt(i - 1))) {
  47.  
  48. tempNumCount = 0;
  49.  
  50. for(int n = i - 1; n >= 0; n--) {
  51.  
  52. if(Character.isDigit(polyStringStripped.charAt(n))) {
  53.  
  54. tempNumCount++;
  55.  
  56. }
  57.  
  58. else {
  59.  
  60. break;
  61.  
  62. }
  63.  
  64. }
  65.  
  66. try {
  67. if(polyStringStripped.charAt(i - tempNumCount - 1) == '-') {
  68. if(polyCoeList.get(getExponent(polyStringStripped, i)) != 0) {
  69.  
  70. polyCoeList.set(getExponent(polyStringStripped, i),(-1 * Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)) + polyCoeList.get(getExponent(polyStringStripped,i))));
  71.  
  72. }
  73.  
  74. else {
  75. polyCoeList.set(getExponent(polyStringStripped, i),-1 * Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)));
  76. }
  77.  
  78.  
  79.  
  80. }
  81.  
  82. else {
  83. if(polyCoeList.get(getExponent(polyStringStripped, i)) != 0) {
  84.  
  85. polyCoeList.set(getExponent(polyStringStripped, i),(Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)) + polyCoeList.get(getExponent(polyStringStripped,i))));
  86.  
  87. }
  88.  
  89. else {
  90. polyCoeList.set(getExponent(polyStringStripped, i),Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)));
  91. }
  92. }
  93. }
  94. catch(Exception StringIndexOutOfBoundsException) {
  95. if(polyCoeList.get(getExponent(polyStringStripped, i)) != 0) {
  96.  
  97. polyCoeList.set(getExponent(polyStringStripped, i),(Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)) + polyCoeList.get(getExponent(polyStringStripped,i))));
  98.  
  99. }
  100.  
  101. else {
  102. polyCoeList.set(getExponent(polyStringStripped, i),Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)));
  103. }
  104. }
  105. }
  106.  
  107. else {
  108. if(polyCoeList.get(getExponent(polyStringStripped, i)) != 0) {
  109. polyCoeList.set(getExponent(polyStringStripped, i), 1 + polyCoeList.get(getExponent(polyStringStripped, i)));
  110. }
  111.  
  112. else {
  113. polyCoeList.set(getExponent(polyStringStripped, i), 1);
  114. }
  115.  
  116. }
  117. }
  118. catch(Exception StringIndexOutOfBoundsException) {
  119.  
  120. if(polyCoeList.get(getExponent(polyStringStripped, i)) != 0) {
  121. polyCoeList.set(getExponent(polyStringStripped, i), 1 + polyCoeList.get(getExponent(polyStringStripped, i)));
  122. }
  123.  
  124. else {
  125. polyCoeList.set(getExponent(polyStringStripped, i), 1);
  126. }
  127.  
  128.  
  129. }
  130.  
  131. }
  132. }
  133. for (int i = 0; i < polyStringStripped.length(); i++){
  134.  
  135. if(Character.isDigit(polyStringStripped.charAt(i))) {
  136.  
  137. tempNumCount = 0;
  138.  
  139. for(int n = i; n < polyStringStripped.length(); n++) {
  140.  
  141. if(Character.isDigit(polyStringStripped.charAt(n))) {
  142.  
  143. tempNumCount++;
  144.  
  145. }
  146. else {
  147.  
  148. break;
  149.  
  150. }
  151. }
  152. try {
  153. if(polyStringStripped.charAt(i - 1) == '-' || polyStringStripped.charAt(i - 1) == '+') {
  154.  
  155.  
  156. try {
  157. if(polyStringStripped.charAt(i + tempNumCount) == '-' || polyStringStripped.charAt(i + tempNumCount) == '+') {
  158.  
  159. if(polyStringStripped.charAt(i - 1) == '-') {
  160.  
  161. constantList.add(-1 * Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
  162.  
  163. }
  164. else {
  165.  
  166. constantList.add(Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
  167.  
  168. }
  169. }
  170. }
  171.  
  172. catch(Exception StringIndexOutOfBoundsException) {
  173.  
  174. if(polyStringStripped.charAt(i - 1) == '-') {
  175. constantList.add(-1 * Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
  176.  
  177. }
  178. else {
  179.  
  180. constantList.add(Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
  181.  
  182.  
  183. }
  184. }
  185.  
  186.  
  187. }
  188. }
  189.  
  190. catch(Exception StringIndexOutOfBoundsException) {
  191. if(i + tempNumCount < polyStringStripped.length()) {
  192. if(polyStringStripped.charAt(i + tempNumCount) == '-' || polyStringStripped.charAt(i + tempNumCount) == '+') {
  193.  
  194. constantList.add(Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
  195.  
  196. }
  197.  
  198. }
  199.  
  200. else {
  201. constantList.add(Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
  202. }
  203.  
  204. }
  205. }
  206. }
  207.  
  208. if(constantList.isEmpty()) {
  209.  
  210. constantList.add(0);
  211.  
  212. }
  213.  
  214. }
  215. private int getExponent(String polyStringStripped, int i) {
  216. int tempNumCount;
  217.  
  218. try {
  219. if (polyStringStripped.charAt(i + 1) == '^') {
  220.  
  221. tempNumCount = 0;
  222.  
  223. for(int n = i + 2; n < polyStringStripped.length(); n++) {
  224.  
  225. if(Character.isDigit(polyStringStripped.charAt(n))) {
  226.  
  227. tempNumCount++;
  228.  
  229. }
  230.  
  231. else {
  232.  
  233. break;
  234.  
  235. }
  236. }
  237.  
  238. return Integer.parseInt(polyStringStripped.substring(i + 2, i + tempNumCount + 2));
  239.  
  240. }
  241.  
  242. else {
  243.  
  244. return 1;
  245.  
  246. }
  247. }
  248. catch(Exception StringIndexOutOfBoundsException) {
  249.  
  250. return 1;
  251.  
  252. }
  253. }
  254.  
  255.  
  256.  
  257. public String toString() {
  258. StringBuilder backToString = new StringBuilder();
  259. ArrayList<Integer> tempList = new ArrayList<Integer>();
  260. int tempConstant = 0;
  261. for(int i = 0; i < polyCoeList.size(); i ++) {
  262.  
  263. tempList.add(polyCoeList.get(i));
  264.  
  265. }
  266.  
  267. for(int i = 0; i < polyCoeList.size(); i++) {
  268.  
  269.  
  270. if(polyCoeList.get(i) != 0 ) {
  271.  
  272. backToString.insert(backToString.length(),Integer.toString(tempList.get(i)) + "x" + "^" + tempList.indexOf(tempList.get(i)) + "+");
  273.  
  274. tempList.set(tempList.indexOf(tempList.get(i)), 0);
  275. }
  276.  
  277.  
  278. }
  279.  
  280. for(int i = 0; i < constantList.size(); i++) {
  281.  
  282. tempConstant = tempConstant + constantList.get(i);
  283.  
  284. }
  285. backToString.insert(backToString.length(),tempConstant);
  286. return backToString.toString();
  287.  
  288. }
  289.  
  290.  
  291. public PolynomialInterface add(PolynomialInterface other) {
  292. ArrayWithExponentAsIndexPolynomial poly1 = (ArrayWithExponentAsIndexPolynomial)other;
  293.  
  294. return new ArrayWithExponentAsIndexPolynomial(toString() + "+" + poly1.toString());
  295.  
  296. }
  297.  
  298. public PolynomialInterface subtract(PolynomialInterface other) {
  299.  
  300. ArrayWithExponentAsIndexPolynomial poly1 = (ArrayWithExponentAsIndexPolynomial)other;
  301. ArrayList<Integer> tempList = new ArrayList<Integer>();
  302. ArrayList<Integer> tempList1 = new ArrayList<Integer>();
  303. String tempStr;
  304.  
  305. for(int i = 0; i < poly1.polyCoeList.size(); i++) {
  306.  
  307. tempList.add(poly1.polyCoeList.get(i));
  308.  
  309. }
  310. for(int i = 0; i < poly1.constantList.size(); i++) {
  311.  
  312. tempList.add(poly1.constantList.get(i));
  313.  
  314. }
  315.  
  316. for(int i = 0; i < poly1.polyCoeList.size(); i++) {
  317.  
  318. if(poly1.polyCoeList.get(i) != 0 ) {
  319. poly1.polyCoeList.set(i, -1 * poly1.polyCoeList.get(i));
  320. }
  321.  
  322. }
  323.  
  324. for(int i = 0; i < poly1.constantList.size(); i++) {
  325.  
  326.  
  327. poly1.constantList.set(i, -1 * poly1.constantList.get(i));
  328.  
  329.  
  330. }
  331.  
  332. tempStr = poly1.toString();
  333.  
  334. poly1.polyCoeList.removeAll(poly1.polyCoeList);
  335. poly1.constantList.removeAll(poly1.constantList);
  336.  
  337.  
  338. for(int i = 0; i < tempList.size(); i++) {
  339.  
  340. poly1.polyCoeList.add(tempList.get(i));
  341.  
  342. }
  343.  
  344. for(int i = 0; i < tempList1.size(); i++) {
  345.  
  346. poly1.constantList.add(tempList.get(i));
  347.  
  348. }
  349.  
  350.  
  351.  
  352. return new ArrayWithExponentAsIndexPolynomial(toString() + "+" + tempStr);
  353.  
  354. }
  355.  
  356.  
  357. public void readPolynomial() {
  358.  
  359. System.out.print(toString());
  360.  
  361. }
  362.  
  363. public static void main(String[] args)
  364. {
  365.  
  366. }
  367. }
  368. class ArraySortedPolynomial implements PolynomialInterface {
  369.  
  370.  
  371. String originalString;
  372. ArrayList<Node> polyCoeList = new ArrayList<Node>();
  373.  
  374. public ArraySortedPolynomial(String polyString) {
  375.  
  376. originalString = polyString;
  377. String polyStringStripped = polyString.replaceAll("\\s+","");
  378. int tempNumCount = 0;
  379. for (int i = 0; i < polyStringStripped.length(); i++){
  380. if(polyStringStripped.charAt(i) == 'x') {
  381.  
  382. try {
  383. if(polyStringStripped.charAt(i - 1) == '-') {
  384.  
  385. polyCoeList.add(new Node(getExponent(polyStringStripped, i), -1, null));
  386.  
  387. }
  388.  
  389. else if(polyStringStripped.charAt(i - 1) == '+') {
  390.  
  391. polyCoeList.add(new Node(getExponent(polyStringStripped, i), 1, null));
  392.  
  393. }
  394.  
  395. else if(Character.isDigit(polyStringStripped.charAt(i - 1))) {
  396.  
  397. tempNumCount = 0;
  398.  
  399. for(int n = i - 1; n >= 0; n--) {
  400.  
  401. if(Character.isDigit(polyStringStripped.charAt(n))) {
  402.  
  403. tempNumCount++;
  404.  
  405. }
  406.  
  407. else {
  408.  
  409. break;
  410.  
  411. }
  412.  
  413. }
  414.  
  415. try {
  416. if(polyStringStripped.charAt(i - tempNumCount - 1) == '-') {
  417.  
  418. polyCoeList.add(new Node(getExponent(polyStringStripped, i),-1 * Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)), null));
  419.  
  420. }
  421.  
  422. else {
  423.  
  424. polyCoeList.add(new Node(getExponent(polyStringStripped, i),Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)), null));
  425.  
  426. }
  427. }
  428. catch(Exception StringIndexOutOfBoundsException) {
  429.  
  430. polyCoeList.add(new Node(getExponent(polyStringStripped, i),Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)), null));
  431. }
  432. }
  433.  
  434. else {
  435. polyCoeList.add(new Node(getExponent(polyStringStripped, i),1, null));
  436.  
  437. }
  438. }
  439. catch(Exception StringIndexOutOfBoundsException) {
  440.  
  441. polyCoeList.add(new Node(getExponent(polyStringStripped, i),1, null));
  442.  
  443. }
  444.  
  445. }
  446. }
  447. for (int i = 0; i < polyStringStripped.length(); i++){
  448.  
  449. if(Character.isDigit(polyStringStripped.charAt(i))) {
  450.  
  451. tempNumCount = 0;
  452.  
  453. for(int n = i; n < polyStringStripped.length(); n++) {
  454.  
  455. if(Character.isDigit(polyStringStripped.charAt(n))) {
  456.  
  457. tempNumCount++;
  458.  
  459. }
  460. else {
  461.  
  462. break;
  463.  
  464. }
  465. }
  466. try {
  467. if(polyStringStripped.charAt(i - 1) == '-' || polyStringStripped.charAt(i - 1) == '+') {
  468.  
  469.  
  470. try {
  471. if(polyStringStripped.charAt(i + tempNumCount) == '-' || polyStringStripped.charAt(i + tempNumCount) == '+') {
  472.  
  473. if(polyStringStripped.charAt(i - 1) == '-') {
  474.  
  475. polyCoeList.add(new Node(0, -1 * Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)), null));
  476.  
  477. }
  478. else {
  479.  
  480. polyCoeList.add(new Node(0, Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)), null));
  481.  
  482. }
  483. }
  484. }
  485.  
  486. catch(Exception StringIndexOutOfBoundsException) {
  487.  
  488. if(polyStringStripped.charAt(i - 1) == '-') {
  489.  
  490. polyCoeList.add(new Node(0, -1 * Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)), null));
  491.  
  492. }
  493. else {
  494.  
  495. polyCoeList.add(new Node(0, Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)), null));
  496.  
  497. }
  498. }
  499.  
  500.  
  501. }
  502. }
  503.  
  504. catch(Exception StringIndexOutOfBoundsException) {
  505. if(i + tempNumCount < polyStringStripped.length()) {
  506. if(polyStringStripped.charAt(i + tempNumCount) == '-' || polyStringStripped.charAt(i + tempNumCount) == '+') {
  507.  
  508. polyCoeList.add(new Node(0, Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)), null));
  509.  
  510. }
  511.  
  512. }
  513.  
  514. else {
  515. polyCoeList.add(new Node(0, Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)), null));
  516. }
  517.  
  518. }
  519. }
  520. }
  521. }
  522. private int getExponent(String polyStringStripped, int i) {
  523. int tempNumCount;
  524.  
  525. try {
  526. if (polyStringStripped.charAt(i + 1) == '^') {
  527.  
  528. tempNumCount = 0;
  529.  
  530. for(int n = i + 2; n < polyStringStripped.length(); n++) {
  531.  
  532. if(Character.isDigit(polyStringStripped.charAt(n))) {
  533.  
  534. tempNumCount++;
  535.  
  536. }
  537.  
  538. else {
  539.  
  540. break;
  541.  
  542. }
  543. }
  544.  
  545. return Integer.parseInt(polyStringStripped.substring(i + 2, i + tempNumCount + 2));
  546.  
  547. }
  548.  
  549. else {
  550.  
  551. return 1;
  552.  
  553. }
  554. }
  555. catch(Exception StringIndexOutOfBoundsException) {
  556.  
  557. return 1;
  558.  
  559. }
  560. }
  561.  
  562.  
  563.  
  564. public String toString() {
  565. StringBuilder backToString = new StringBuilder();
  566. addLikeTerms();
  567. for(int i = 0; i < polyCoeList.size(); i++) {
  568.  
  569. if(polyCoeList.get(i).getExp() != 0 && polyCoeList.get(i).getCoe() != 0 ) {
  570. backToString.insert(backToString.length(),Integer.toString(polyCoeList.get(i).getCoe()) + "x" + "^" + polyCoeList.get(i).getExp() + "+");
  571.  
  572. }
  573.  
  574. }
  575. if(polyCoeList.get(polyCoeList.size() - 1).getExp() == 0 && polyCoeList.get(polyCoeList.size() - 1).getCoe() != 0 ) {
  576.  
  577. backToString.insert(backToString.length(), polyCoeList.get(polyCoeList.size() - 1).getCoe());
  578.  
  579. }
  580. else {
  581. backToString.deleteCharAt(backToString.length()- 1);
  582. }
  583. return backToString.toString();
  584.  
  585. }
  586.  
  587. public void addLikeTerms() {
  588. Collections.sort(polyCoeList);
  589. Collections.reverse(polyCoeList);
  590.  
  591. for(int i = 0; i < polyCoeList.size(); i++) {
  592. try {
  593. if(polyCoeList.get(i).getExp() == polyCoeList.get(i + 1).getExp()) {
  594.  
  595. polyCoeList.add(new Node(polyCoeList.get(i).getExp(), polyCoeList.get(i).getCoe() + polyCoeList.get(i + 1).getCoe(), null));
  596. polyCoeList.remove(i);
  597. polyCoeList.remove(i);
  598. i--;
  599. Collections.sort(polyCoeList);
  600. Collections.reverse(polyCoeList);
  601.  
  602. }
  603. }
  604. catch(Exception ArrayIndexOutOfBoundsException) {
  605. break;
  606.  
  607. }
  608.  
  609. }
  610.  
  611. }
  612. public PolynomialInterface add(PolynomialInterface other) {
  613. ArraySortedPolynomial poly1 = (ArraySortedPolynomial)other;
  614. ArraySortedPolynomial poly2 = new ArraySortedPolynomial(toString() + "+" + poly1.toString());
  615. poly2.addLikeTerms();
  616. return new ArraySortedPolynomial(poly2.toString());
  617.  
  618. }
  619.  
  620. public PolynomialInterface subtract(PolynomialInterface other) {
  621.  
  622. ArraySortedPolynomial poly1 = (ArraySortedPolynomial)other;
  623.  
  624. for(int i = 0; i < poly1.polyCoeList.size(); i++) {
  625.  
  626. poly1.polyCoeList.get(i).turnNegative();
  627.  
  628. }
  629.  
  630. ArraySortedPolynomial poly2 = new ArraySortedPolynomial(toString() + "+" + poly1.toString());
  631. poly2.addLikeTerms();
  632.  
  633. for(int i = 0; i < poly1.polyCoeList.size(); i++) {
  634.  
  635. poly1.polyCoeList.get(i).turnNegative();
  636.  
  637. }
  638.  
  639. return new ArraySortedPolynomial(poly2.toString());
  640. }
  641.  
  642.  
  643. public void readPolynomial() {
  644.  
  645. System.out.print(toString());
  646.  
  647. }
  648.  
  649. }
  650.  
  651.  
  652. class LinkListPolynomial implements PolynomialInterface {
  653.  
  654.  
  655. String originalString;
  656. PolyLinkedList polyCoeList = new PolyLinkedList();
  657.  
  658. public LinkListPolynomial(String polyString) {
  659.  
  660. originalString = polyString;
  661. String polyStringStripped = polyString.replaceAll("\\s+","");
  662. int tempNumCount = 0;
  663. for (int i = 0; i < polyStringStripped.length(); i++){
  664. if(polyStringStripped.charAt(i) == 'x') {
  665.  
  666. try {
  667. if(polyStringStripped.charAt(i - 1) == '-') {
  668.  
  669. polyCoeList.insert(getExponent(polyStringStripped, i), -1);
  670.  
  671. }
  672.  
  673. else if(polyStringStripped.charAt(i - 1) == '+') {
  674.  
  675. polyCoeList.insert(getExponent(polyStringStripped, i), -1);
  676. }
  677.  
  678. else if(Character.isDigit(polyStringStripped.charAt(i - 1))) {
  679.  
  680. tempNumCount = 0;
  681.  
  682. for(int n = i - 1; n >= 0; n--) {
  683.  
  684. if(Character.isDigit(polyStringStripped.charAt(n))) {
  685.  
  686. tempNumCount++;
  687.  
  688. }
  689.  
  690. else {
  691.  
  692. break;
  693.  
  694. }
  695.  
  696. }
  697.  
  698. try {
  699. if(polyStringStripped.charAt(i - tempNumCount - 1) == '-') {
  700.  
  701. polyCoeList.insert(getExponent(polyStringStripped, i),-1 * Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)));
  702.  
  703. }
  704.  
  705. else {
  706.  
  707. polyCoeList.insert(getExponent(polyStringStripped, i),Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)));
  708.  
  709. }
  710. }
  711. catch(Exception StringIndexOutOfBoundsException) {
  712.  
  713. polyCoeList.insert(getExponent(polyStringStripped, i),Integer.parseInt(polyStringStripped.substring(i - tempNumCount, i)));
  714. }
  715. }
  716.  
  717. else {
  718. polyCoeList.insert(getExponent(polyStringStripped, i),1);
  719.  
  720. }
  721. }
  722. catch(Exception StringIndexOutOfBoundsException) {
  723.  
  724. polyCoeList.insert(getExponent(polyStringStripped, i),1);
  725.  
  726. }
  727.  
  728. }
  729. }
  730. for (int i = 0; i < polyStringStripped.length(); i++){
  731.  
  732. if(Character.isDigit(polyStringStripped.charAt(i))) {
  733.  
  734. tempNumCount = 0;
  735.  
  736. for(int n = i; n < polyStringStripped.length(); n++) {
  737.  
  738. if(Character.isDigit(polyStringStripped.charAt(n))) {
  739.  
  740. tempNumCount++;
  741.  
  742. }
  743. else {
  744.  
  745. break;
  746.  
  747. }
  748. }
  749. try {
  750. if(polyStringStripped.charAt(i - 1) == '-' || polyStringStripped.charAt(i - 1) == '+') {
  751.  
  752.  
  753. try {
  754. if(polyStringStripped.charAt(i + tempNumCount) == '-' || polyStringStripped.charAt(i + tempNumCount) == '+') {
  755.  
  756. if(polyStringStripped.charAt(i - 1) == '-') {
  757.  
  758. polyCoeList.insert(0, -1 * Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
  759.  
  760. }
  761. else {
  762.  
  763. polyCoeList.insert(0, Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
  764.  
  765. }
  766. }
  767. }
  768.  
  769. catch(Exception StringIndexOutOfBoundsException) {
  770.  
  771. if(polyStringStripped.charAt(i - 1) == '-') {
  772.  
  773. polyCoeList.insert(0, -1 * Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
  774.  
  775. }
  776. else {
  777.  
  778. polyCoeList.insert(0, Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
  779.  
  780. }
  781. }
  782.  
  783.  
  784. }
  785. }
  786.  
  787. catch(Exception StringIndexOutOfBoundsException) {
  788. if(i + tempNumCount < polyStringStripped.length()) {
  789. if(polyStringStripped.charAt(i + tempNumCount) == '-' || polyStringStripped.charAt(i + tempNumCount) == '+') {
  790.  
  791. polyCoeList.insert(0, Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
  792.  
  793. }
  794.  
  795. }
  796.  
  797. else {
  798. polyCoeList.insert(0, Integer.parseInt(polyStringStripped.substring(i, i + tempNumCount)));
  799. }
  800.  
  801. }
  802. }
  803. }
  804. }
  805. private int getExponent(String polyStringStripped, int i) {
  806. int tempNumCount;
  807.  
  808. try {
  809. if (polyStringStripped.charAt(i + 1) == '^') {
  810.  
  811. tempNumCount = 0;
  812.  
  813. for(int n = i + 2; n < polyStringStripped.length(); n++) {
  814.  
  815. if(Character.isDigit(polyStringStripped.charAt(n))) {
  816.  
  817. tempNumCount++;
  818.  
  819. }
  820.  
  821. else {
  822.  
  823. break;
  824.  
  825. }
  826. }
  827.  
  828. return Integer.parseInt(polyStringStripped.substring(i + 2, i + tempNumCount + 2));
  829.  
  830. }
  831.  
  832. else {
  833.  
  834. return 1;
  835.  
  836. }
  837. }
  838. catch(Exception StringIndexOutOfBoundsException) {
  839.  
  840. return 1;
  841.  
  842. }
  843. }
  844.  
  845.  
  846.  
  847. public String toString() {
  848.  
  849. //System.out.println(polyCoeList);
  850. polyCoeList = polyCoeList.sort();
  851. StringBuilder backToString = new StringBuilder();
  852. //polyCoeList =addLikeTerms();
  853. for(int i = 0; i < polyCoeList.size(); i++) {
  854.  
  855. if(polyCoeList.get(i).getExp() != 0 && polyCoeList.get(i).getCoe() != 0 ) {
  856. backToString.insert(backToString.length(),Integer.toString(polyCoeList.get(i).getCoe()) + "x" + "^" + polyCoeList.get(i).getExp() + "+");
  857.  
  858. }
  859.  
  860. }
  861.  
  862. if(polyCoeList.get(polyCoeList.size() - 1).getExp() == 0 && polyCoeList.get(polyCoeList.size() - 1).getCoe() != 0 ) {
  863.  
  864. backToString.insert(backToString.length(), polyCoeList.get(polyCoeList.size() - 1).getCoe());
  865.  
  866. }
  867. else {
  868. backToString.deleteCharAt(backToString.length()- 1);
  869. }
  870. return backToString.toString();
  871.  
  872. }
  873.  
  874. public PolyLinkedList addLikeTerms() {
  875.  
  876. //System.out.println(polyCoeList);
  877. polyCoeList = polyCoeList.sort();
  878. //System.out.println(polyCoeList);
  879. PolyLinkedList tempList = new PolyLinkedList();
  880. for(int i = 0; i < polyCoeList.size(); i++) {
  881. //System.out.println(polyCoeList);
  882. //System.out.println(tempList);
  883. try {
  884. if(polyCoeList.get(i).getExp() == polyCoeList.get(i + 1).getExp()) {
  885.  
  886. tempList.insert(polyCoeList.get(i).getExp(), polyCoeList.get(i).getCoe() + polyCoeList.get(i + 1).getCoe());
  887. polyCoeList.deleteAt(i + 1);
  888. if(polyCoeList.get(i).getExp() == polyCoeList.get(i + 1).getExp()) {
  889. i--;
  890. }
  891. }
  892. else {
  893.  
  894. tempList.insert(polyCoeList.get(i).getExp(), polyCoeList.get(i).getCoe());
  895.  
  896. }
  897.  
  898. }
  899. catch(Exception ArrayIndexOutOfBoundsException) {
  900. //System.out.println(polyCoeList);
  901. //System.out.println(tempList);
  902. if(polyCoeList.get(i).getExp() == polyCoeList.get(i - 1).getExp()) {
  903.  
  904. tempList.insert(polyCoeList.get(i).getExp(), polyCoeList.get(i).getCoe() + polyCoeList.get(i - 1).getCoe());
  905.  
  906. }
  907. else {
  908.  
  909. tempList.insert(polyCoeList.get(i).getExp(), polyCoeList.get(i).getCoe());
  910.  
  911. }
  912. break;
  913.  
  914. }
  915.  
  916. }
  917. tempList = tempList.sort();
  918. return tempList;
  919.  
  920. }
  921. public PolynomialInterface add(PolynomialInterface other) {
  922. LinkListPolynomial poly1 = (LinkListPolynomial)other;
  923. LinkListPolynomial poly2 = new LinkListPolynomial(toString() + "+" + poly1.toString());
  924. poly2.polyCoeList = poly2.addLikeTerms();
  925.  
  926. return new LinkListPolynomial(poly2.toString());
  927.  
  928. }
  929.  
  930. public PolynomialInterface subtract(PolynomialInterface other) {
  931.  
  932. LinkListPolynomial poly1 = (LinkListPolynomial)other;
  933.  
  934. for(int i = 0; i < poly1.polyCoeList.size(); i++) {
  935.  
  936. poly1.polyCoeList.get(i).turnNegative();
  937.  
  938. }
  939.  
  940. LinkListPolynomial poly2 = new LinkListPolynomial(toString() + "+" + poly1.toString());
  941. poly2.polyCoeList = poly2.addLikeTerms();
  942.  
  943. for(int i = 0; i < poly1.polyCoeList.size(); i++) {
  944.  
  945. poly1.polyCoeList.get(i).turnNegative();
  946.  
  947. }
  948.  
  949. return new LinkListPolynomial(poly2.toString());
  950. }
  951.  
  952.  
  953. public void readPolynomial() {
  954.  
  955. System.out.print(toString());
  956.  
  957. }
  958. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement