Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.82 KB | None | 0 0
  1.  
  2.  
  3. public class InfiniteInteger implements Comparable<InfiniteInteger>
  4. {
  5. // TO DO: Instance Variables
  6. // Note that it is a good idea to declare them final to
  7. // prevent you from accidentally modify them.
  8.  
  9. int numDigits;
  10. boolean isNegative;
  11. int[] IInteger;
  12. boolean wasPassed = false;
  13.  
  14. /**
  15. * Constructor: Constructs this infinite integer from a string
  16. * representing an integer.
  17. * @param s a string represents an integer
  18. */
  19. // TO DO: Constructor
  20.  
  21. public InfiniteInteger(String s)
  22. {
  23. //remove negative
  24. if (s.contains("-")){
  25. isNegative = true;
  26. s = s.substring(1);
  27. }
  28.  
  29. //remove leading 0s
  30. while(s.length() > 1 && s.charAt(0) == '0'){
  31. s = s.substring(1);
  32. }
  33.  
  34. //split remaining string with "-" and leading 0s removed
  35.  
  36. String[] string = s.split("");
  37.  
  38. //give value to instance variables
  39. int []fuck = new int[string.length];
  40. //don't know why this is - 1
  41. numDigits = string.length - 1;
  42.  
  43. //add digits from s to II
  44. for (int i = 1; i < string.length ; i++){
  45. fuck[i] = Integer.parseInt(string[i]);
  46. }
  47.  
  48. IInteger = new int[string.length-1];
  49.  
  50. for(int i = 1 ; i < fuck.length ; i++){
  51. IInteger[i-1] = (fuck[i]);
  52. }
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59. /**
  60. * Constructor: Constructs this infinite integer from an integer.
  61. * @param anInteger an integer
  62. */
  63. // TO DO: Constructor
  64.  
  65. public InfiniteInteger(int anInteger)
  66. {
  67.  
  68. //convert from int to string
  69. String s = Integer.toString(anInteger);
  70.  
  71. //remove negative
  72.  
  73. if (s.contains("-")){
  74. isNegative = true;
  75. s = s.substring(1);
  76. }
  77.  
  78. //remove leading 0s
  79. while(s.length() > 1 && s.charAt(0) == '0'){
  80. s = s.substring(1);
  81. }
  82.  
  83. //split remaining string with "-" and leading 0s removed
  84.  
  85. String[] string = s.split("");
  86.  
  87. //give value to instance variables
  88. int []fuck = new int[string.length];
  89. //don't know why this is - 1
  90. numDigits = string.length - 1;
  91.  
  92. //add digits from s to II
  93. for (int i = 1; i < string.length ; i++){
  94. fuck[i] = Integer.parseInt(string[i]);
  95. }
  96.  
  97. IInteger = new int[string.length-1];
  98.  
  99. for(int i = 1 ; i < fuck.length ; i++){
  100. IInteger[i-1] = (fuck[i]);
  101. }
  102. }
  103.  
  104.  
  105.  
  106. /**
  107. * Gets the number of digits of this infinite integer.
  108. * @return an integer representing the number of digits
  109. * of this infinite integer.
  110. */
  111. public int getNumberOfDigits()
  112. {
  113. // TO DO
  114. return numDigits;
  115. }
  116.  
  117. /**
  118. * Checks whether this infinite integer is a negative number.
  119. * @return true if this infinite integer is a negative number.
  120. * Otherwise, return false.
  121. */
  122. public boolean isNegative()
  123. {
  124. // TO DO
  125. if (isNegative == true){
  126. return true;
  127. } else{
  128. return false;
  129. }
  130. }
  131.  
  132. /**
  133. * Calculates the result of this infinite integer plus anInfiniteInteger
  134. * @param anInfiniteInteger the infinite integer to be added to this
  135. * infinite integer.
  136. * @return a NEW infinite integer representing the result of this
  137. * infinite integer plus anInfiniteInteger
  138. */
  139. // TO DO
  140.  
  141. public InfiniteInteger plus(final InfiniteInteger anInfiniteInteger)
  142. {
  143. //normal case
  144. if((this.isNegative == false && anInfiniteInteger.isNegative == false || ( this.isNegative == true && anInfiniteInteger.isNegative == true))) {
  145.  
  146. //create sum array
  147. int bigSize;
  148. int smallSize;
  149. InfiniteInteger first = new InfiniteInteger("0");
  150. InfiniteInteger second = new InfiniteInteger("0");
  151.  
  152. if(this.getNumberOfDigits() >= anInfiniteInteger.getNumberOfDigits()){
  153. bigSize = this.getNumberOfDigits();
  154. smallSize = anInfiniteInteger.getNumberOfDigits();
  155.  
  156. //don't know why i have to do this but otherwise i get fk'd
  157. InfiniteInteger blah = new InfiniteInteger(this.toString());
  158. first = blah;
  159.  
  160. InfiniteInteger blah1 = new InfiniteInteger(anInfiniteInteger.toString());
  161. second = blah1;
  162.  
  163.  
  164. }else{
  165. bigSize = anInfiniteInteger.getNumberOfDigits();
  166. smallSize = this.getNumberOfDigits();
  167.  
  168. first = anInfiniteInteger;
  169. second = this;
  170.  
  171. }
  172.  
  173. int[] sum = new int[bigSize];
  174.  
  175. //****BEGIN ADDING SHIT ****
  176.  
  177. int indSum;
  178. int diff = bigSize - smallSize;
  179. int numIter = 1;
  180.  
  181. for(int i = bigSize - 1; i >= 0; i--){
  182.  
  183. //for the no. digs that the other doesnt have
  184. if(numIter > smallSize){
  185. indSum = first.IInteger[i];
  186. //for digs that the other has
  187. }else{
  188. indSum = first.IInteger[i] + second.IInteger[i - diff];
  189. }
  190.  
  191. //change shit if more than 10 and not last dig
  192. if(indSum > 9 && i != 0){
  193.  
  194. first.IInteger[i-1]++;
  195. indSum = indSum - 10;
  196.  
  197. }
  198.  
  199. //set and increment
  200. sum[i] = indSum;
  201. numIter++;
  202.  
  203. }
  204.  
  205. //****STOP ADDING SHIT ****
  206.  
  207. //convert to string
  208. String strSum = "";
  209. for(int i : sum){
  210. Integer.toString(i);
  211. strSum = strSum + i;
  212. }
  213.  
  214. //if double neg
  215. if (this.isNegative == true && anInfiniteInteger.isNegative == true){
  216. strSum = "-" + strSum;
  217. }
  218.  
  219. //return that bad bitch
  220. InfiniteInteger IIadd = new InfiniteInteger(strSum);
  221. return IIadd;
  222.  
  223. //if only one II is negative call
  224. }else{
  225. wasPassed = true;
  226.  
  227. if(this.isNegative == true){
  228. anInfiniteInteger.isNegative = true;
  229. }
  230.  
  231. if (this.isNegative == false){
  232. anInfiniteInteger.isNegative = false;
  233. }
  234.  
  235. InfiniteInteger IIminus = minus(anInfiniteInteger);
  236. return IIminus;
  237. }
  238. }
  239.  
  240.  
  241. /**
  242. * Calculates the result of this infinite integer subtracted by anInfiniteInteger
  243. * @param anInfiniteInteger the infinite integer to subtract.
  244. * @return a NEW infinite integer representing the result of this
  245. * infinite integer subtracted by anInfiniteInteger
  246. */
  247. public InfiniteInteger minus(final InfiniteInteger anInfiniteInteger)
  248. {
  249. // TO DO
  250.  
  251. //normal case
  252. if ( (this.isNegative == true && anInfiniteInteger.isNegative == true) || (this.isNegative == false && anInfiniteInteger.isNegative == false) ){
  253.  
  254. int bigSize;
  255. int smallSize;
  256. InfiniteInteger first;
  257. InfiniteInteger second;
  258. InfiniteInteger temp;
  259.  
  260. if(this.getNumberOfDigits() >= anInfiniteInteger.getNumberOfDigits()){
  261. bigSize = this.getNumberOfDigits();
  262. smallSize = anInfiniteInteger.getNumberOfDigits();
  263. first = this;
  264. second = anInfiniteInteger;
  265.  
  266. //special cases since im really bad at coding
  267. if( this.isNegative == true && anInfiniteInteger.isNegative == true && (second.compareTo(first) == 1) ){
  268. wasPassed = true;
  269. }
  270.  
  271. if( this.isNegative == true && anInfiniteInteger.isNegative == true && (second.compareTo(first) == -1) ){
  272. temp = first;
  273. first = second;
  274. second = temp;
  275. wasPassed = false;
  276. }
  277.  
  278. if( this.isNegative == false && anInfiniteInteger.isNegative == false && (second.compareTo(first) == -1) ){
  279. wasPassed = false;
  280. }
  281.  
  282. if( this.isNegative == false && anInfiniteInteger.isNegative == false && (second.compareTo(first) == 1) ){
  283. temp = first;
  284. first = second;
  285. second = temp;
  286. wasPassed = true;
  287. }
  288.  
  289. }else{
  290. bigSize = anInfiniteInteger.getNumberOfDigits();
  291. smallSize = this.getNumberOfDigits();
  292. first = anInfiniteInteger;
  293. second = this;
  294.  
  295. //some more fun cases
  296. if( this.isNegative == true && anInfiniteInteger.isNegative == true && (second.compareTo(first) == 1) ){
  297. wasPassed = false;
  298. }
  299.  
  300. if( this.isNegative == false && anInfiniteInteger.isNegative == false && (second.compareTo(first) == 1) ){
  301. first = this;
  302. second = anInfiniteInteger;
  303. wasPassed = true;
  304. }
  305.  
  306. if( this.isNegative == false && anInfiniteInteger.isNegative == false && (second.compareTo(first) == -1) ){
  307. wasPassed = true;
  308. }
  309.  
  310. //didn't actually end up needing this but it seems like it would make sense
  311. if( this.isNegative == true && anInfiniteInteger.isNegative == true && (second.compareTo(first) == -1) ){
  312. first = this;
  313. second = anInfiniteInteger;
  314. wasPassed = false;
  315. }
  316. }
  317.  
  318. int[] sum = new int[bigSize];
  319.  
  320.  
  321. //START SUBTRACTING SHIT
  322. int indSum;
  323. int diff = bigSize - smallSize;
  324. int numIter = 1;
  325.  
  326. for(int i = bigSize - 1; i >= 0; i--){
  327. //digits where thisII doesn't have any
  328. if(numIter > smallSize){
  329. indSum = first.IInteger[i];
  330. sum[i] = indSum;
  331.  
  332. //normal case
  333. }else{
  334. //if number at index being subtracted is less than the other
  335. if(second.IInteger[i-diff] > first.IInteger[i]){
  336. //if the number at index is greater than 0 no problemo
  337. if(first.IInteger[i-1] > 0){
  338. first.IInteger[i-1]--;
  339. first.IInteger[i] += 10;
  340. //but if the number is 0 we have special case
  341. }else{
  342. int index = i-1;
  343. while(first.IInteger[index] == 0){
  344. first.IInteger[index] = 9;
  345. index--;
  346. }
  347. first.IInteger[index]--;
  348. first.IInteger[i] += 10;
  349. }
  350. }
  351. indSum = first.IInteger[i] - second.IInteger[i-diff];
  352. sum[i] = indSum;
  353. }
  354.  
  355. numIter++;
  356. }
  357.  
  358.  
  359. //END SUBTRACTING SHIT
  360. String strSum = "";
  361. for(int i : sum){
  362. Integer.toString(i);
  363. strSum = strSum + i;
  364. }
  365.  
  366.  
  367. if (wasPassed == true){
  368. strSum = "-" + strSum;
  369. }
  370.  
  371. //returning
  372. InfiniteInteger IIsub = new InfiniteInteger(strSum);
  373. return IIsub;
  374.  
  375.  
  376. //if only one II is negative call
  377. }else{
  378. if(this.isNegative == false){
  379. anInfiniteInteger.isNegative = false;
  380. }
  381.  
  382. if(this.isNegative == true){
  383. anInfiniteInteger.isNegative = true;
  384. }
  385.  
  386. InfiniteInteger IIplus = plus(anInfiniteInteger);
  387. return IIplus;
  388. }
  389. }
  390.  
  391. /**
  392. * Calculates the result of this infinite integer multiplied by anInfiniteInteger
  393. * @param anInfiniteInteger the multiplier.
  394. * @return a NEW infinite integer representing the result of this
  395. * infinite integer multiplied by anInfiniteInteger.
  396. */
  397. public InfiniteInteger multiply(final InfiniteInteger anInfiniteInteger)
  398. {
  399. // TO DO
  400.  
  401. //create sum array
  402. int bigSize;
  403. int smallSize;
  404. InfiniteInteger first;
  405. InfiniteInteger second;
  406.  
  407. if(this.getNumberOfDigits() > anInfiniteInteger.getNumberOfDigits()){
  408. bigSize = this.getNumberOfDigits();
  409. smallSize = anInfiniteInteger.getNumberOfDigits();
  410. first = this;
  411. second = anInfiniteInteger;
  412.  
  413. }else{
  414. bigSize = anInfiniteInteger.getNumberOfDigits();
  415. smallSize = this.getNumberOfDigits();
  416. first = anInfiniteInteger;
  417. second = this;
  418. }
  419.  
  420. //**LETS GET MULTIPLYING BABY**
  421. int indProd;
  422. int newAddBy = 0;
  423. int numIter = 0;
  424. int[] firstTerm = new int[bigSize *2];
  425. int[] secondTerm = new int[bigSize*2];
  426. String strProdFull = "";
  427.  
  428. for(int i = smallSize - 1; i >= 0; i--){
  429.  
  430. int count = 0;
  431. for(int j = bigSize - 1; j >= 0; j--){
  432.  
  433. indProd = second.IInteger[i] * first.IInteger[j];
  434.  
  435. //add excess from old indProd
  436. if (count > 0 && newAddBy != 0){
  437. indProd += newAddBy;
  438. newAddBy = 0;
  439. //System.out.println("first if exec");
  440. }
  441.  
  442. //if more than 10 stuff
  443. if(indProd > 9 && j != 0){
  444. String strProd = Integer.toString(indProd);
  445. newAddBy = Integer.parseInt(strProd.substring(0,1));
  446.  
  447. //get minusBy
  448. String add0 = Integer.toString(newAddBy);
  449. add0 = add0 + "0";
  450. int minusBy = Integer.parseInt(add0);
  451. indProd = indProd - minusBy;
  452. }
  453.  
  454. //add
  455. if(numIter == 1){
  456. secondTerm[firstTerm.length - 1 - numIter - count] = indProd;
  457. }else{
  458. firstTerm[firstTerm.length - 1 - numIter - count] = indProd;
  459. }
  460. count++;
  461. }
  462. count = 0;
  463.  
  464.  
  465. //have to do this cause i suck at programming
  466. String strFirst1 = "";
  467. for(int l : firstTerm){
  468. Integer.toString(l);
  469. strFirst1 = strFirst1 + l;
  470. }
  471.  
  472. if(smallSize == 1){
  473. if((this.isNegative == true && anInfiniteInteger.isNegative== false) || (this.isNegative == false && anInfiniteInteger.isNegative == true)){
  474. strFirst1 = "-" + strFirst1;
  475. }
  476.  
  477. InfiniteInteger fuck = new InfiniteInteger(strFirst1);
  478. return fuck;
  479. }
  480.  
  481. //add together
  482. if(numIter > 0){
  483. String strSecond = "";
  484. for(int k : secondTerm){
  485. Integer.toString(k);
  486. strSecond = strSecond + k;
  487. }
  488.  
  489. String strFirst = "";
  490. for(int l : firstTerm){
  491. Integer.toString(l);
  492. strFirst = strFirst + l;
  493. }
  494.  
  495. if(!strProdFull.equals("")){
  496. InfiniteInteger secondII = new InfiniteInteger(strProdFull);
  497. InfiniteInteger firstII = new InfiniteInteger(strFirst);
  498. secondII = secondII.plus(firstII);
  499.  
  500. strProdFull = secondII.toString();
  501. }else{
  502. InfiniteInteger secondII = new InfiniteInteger(strSecond);
  503. InfiniteInteger firstII = new InfiniteInteger(strFirst);
  504. secondII = secondII.plus(firstII);
  505.  
  506. strProdFull = secondII.toString();
  507. }
  508.  
  509. //have to reset or it gets fucked up
  510. if(numIter>0){
  511. for(int q = 0; q < firstTerm.length; q++){
  512. firstTerm[q] = 0;
  513. }
  514. }
  515. }
  516. numIter++;
  517. }
  518.  
  519. //**CALM UR DONGER FRIEND**
  520. if((this.isNegative == true && anInfiniteInteger.isNegative== false) || (this.isNegative == false && anInfiniteInteger.isNegative == true)){
  521. strProdFull = "-" + strProdFull;
  522. }
  523.  
  524. InfiniteInteger prod = new InfiniteInteger(strProdFull);
  525. return prod;
  526. }
  527.  
  528.  
  529.  
  530. /**
  531. * Generates a string representing this infinite integer. If this infinite integer
  532. * is a negative number a minus symbol should be in the front of numbers. For example,
  533. * "-12345678901234567890". But if the infinite integer is a positive number, no symbol
  534. * should be in the front of the numbers (e.g., "12345678901234567890").
  535. * @return a string representing this infinite integer number.
  536. */
  537. public String toString()
  538. {
  539. // TO DO
  540.  
  541. //im bad with zeroes
  542. if(IInteger.length == 1 && IInteger[0] == 0){
  543. String s = "0";
  544. return s;
  545. }
  546.  
  547. String val = "";
  548.  
  549. for (int i = 0; i < IInteger.length ; i++){
  550. val = val + Integer.toString(IInteger[i]);
  551. }
  552. if (this.isNegative == true){
  553. val = "-" + val;
  554. }
  555. return val;
  556. }
  557.  
  558. /**
  559. * Compares this infinite integer with anInfiniteInteger
  560. * @return either -1, 0, or 1 as follows:
  561. * If this infinite integer is less than anInfiniteInteger, return -1.
  562. * If this infinite integer is equal to anInfiniteInteger, return 0.
  563. * If this infinite integer is greater than anInfiniteInteger, return 1.
  564. */
  565. public int compareTo(final InfiniteInteger anInfiniteInteger){
  566. // TO DO
  567. //i tried using compareTo() trust me
  568.  
  569. //if one or the other is neg
  570. if (this.isNegative && !anInfiniteInteger.isNegative){
  571. return -1;
  572. }else if(!this.isNegative && anInfiniteInteger.isNegative){
  573. return 1;
  574.  
  575. //both positive
  576. }else if(!this.isNegative && !anInfiniteInteger.isNegative){
  577. if(this.getNumberOfDigits() > anInfiniteInteger.getNumberOfDigits()){
  578. return 1;
  579. }else if(this.getNumberOfDigits() < anInfiniteInteger.getNumberOfDigits()){
  580. return -1;
  581. }else{
  582. for(int i = 0; i < this.getNumberOfDigits(); i++){
  583. if (this.IInteger[i] > anInfiniteInteger.IInteger[i]){
  584. return 1;
  585. }
  586. if (this.IInteger[i] < anInfiniteInteger.IInteger[i]){
  587. return -1;
  588. }
  589. }
  590. return 0;
  591. }
  592. }
  593.  
  594. //both negative
  595. else if(this.isNegative && anInfiniteInteger.isNegative){
  596. if(this.getNumberOfDigits() > anInfiniteInteger.getNumberOfDigits()){
  597. return -1;
  598. }else if(this.getNumberOfDigits() < anInfiniteInteger.getNumberOfDigits()){
  599. return 1;
  600. }else{
  601. for(int i = 0; i < this.getNumberOfDigits(); i++){
  602. if (this.IInteger[i] > anInfiniteInteger.IInteger[i]){
  603. return -1;
  604. }
  605. if (this.IInteger[i] < anInfiniteInteger.IInteger[i]){
  606. return 1;
  607. }
  608. }
  609. return 0;
  610. }
  611. }
  612. return 0;
  613. }
  614. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement