Advertisement
Guest User

Untitled

a guest
Sep 25th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. package bigint;
  2.  
  3. /**
  4. * This class encapsulates a BigInteger, i.e. a positive or negative integer with
  5. * any number of digits, which overcomes the computer storage length limitation of
  6. * an integer.
  7. *
  8. */
  9. public class BigInteger {
  10.  
  11. /**
  12. * True if this is a negative integer
  13. */
  14. boolean negative;
  15.  
  16. /**
  17. * Number of digits in this integer
  18. */
  19. int numDigits;
  20.  
  21. /**
  22. * Reference to the first node of this integer's linked list representation
  23. * NOTE: The linked list stores the Least Significant Digit in the FIRST node.
  24. * For instance, the integer 235 would be stored as:
  25. * 5 --> 3 --> 2
  26. *
  27. * Insignificant digits are not stored. So the integer 00235 will be stored as:
  28. * 5 --> 3 --> 2 (No zeros after the last 2)
  29. */
  30. DigitNode front;
  31.  
  32. /**
  33. * Initializes this integer to a positive number with zero digits, in other
  34. * words this is the 0 (zero) valued integer.
  35. */
  36. public BigInteger() {
  37. negative = false;
  38. numDigits = 0;
  39. front = null;
  40. }
  41.  
  42. /**
  43. * Parses an input integer string into a corresponding BigInteger instance.
  44. * A correctly formatted integer would have an optional sign as the first
  45. * character (no sign means positive), and at least one digit character
  46. * (including zero).
  47. * Examples of correct format, with corresponding values
  48. * Format Value
  49. * +0 0
  50. * -0 0
  51. * +123 123
  52. * 1023 1023
  53. * 0012 12
  54. * 0 0
  55. * -123 -123
  56. * -001 -1
  57. * +000 0
  58. *
  59. * Leading and trailing spaces are ignored. So " +123 " will still parse
  60. * correctly, as +123, after ignoring leading and trailing spaces in the input
  61. * string.
  62. *
  63. * Spaces between digits are not ignored. So "12 345" will not parse as
  64. * an integer - the input is incorrectly formatted.
  65. *
  66. * An integer with value 0 will correspond to a null (empty) list - see the BigInteger
  67. * constructor
  68. *
  69. * @param integer Integer string that is to be parsed
  70. * @return BigInteger instance that stores the input integer.
  71. * @throws IllegalArgumentException If input is incorrectly formatted
  72. */
  73. public static BigInteger parse(String integer)
  74. throws IllegalArgumentException {
  75.  
  76. // following line is a placeholder - compiler needs a return
  77. // modify it according to need
  78. return null;
  79. }
  80.  
  81. /**
  82. * Adds the first and second big integers, and returns the result in a NEW BigInteger object.
  83. * DOES NOT MODIFY the input big integers.
  84. *
  85. * NOTE that either or both of the input big integers could be negative.
  86. * (Which means this method can effectively subtract as well.)
  87. *
  88. * @param first First big integer
  89. * @param second Second big integer
  90. * @return Result big integer
  91. */
  92. public static BigInteger add(BigInteger first, BigInteger second) {
  93. // following line is a placeholder - compiler needs a return
  94. // modify it according to need
  95. return null;
  96. }
  97.  
  98. /**
  99. * Returns the BigInteger obtained by multiplying the first big integer
  100. * with the second big integer
  101. *
  102. * This method DOES NOT MODIFY either of the input big integers
  103. *
  104. * @param first First big integer
  105. * @param second Second big integer
  106. * @return A new BigInteger which is the product of the first and second big integers
  107. */
  108. public static BigInteger multiply(BigInteger first, BigInteger second) {
  109. // following line is a placeholder - compiler needs a return
  110. // modify it according to need
  111. return null;
  112.  
  113. }
  114.  
  115. /* (non-Javadoc)
  116. * @see java.lang.Object#toString()
  117. */
  118. public String toString() {
  119. if (front == null) {
  120. return "0";
  121. }
  122. String retval = front.digit + "";
  123. for (DigitNode curr = front.next; curr != null; curr = curr.next) {
  124. retval = curr.digit + retval;
  125. }
  126.  
  127. if (negative) {
  128. retval = '-' + retval;
  129. }
  130. return retval;
  131. }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement