Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.26 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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement