fork download
  1. package oop.asg02;
  2.  
  3. import java.io.IOException;
  4.  
  5. public class BigInteger
  6. {
  7. private String s;
  8. public BigInteger(long init) {
  9. s = String.valueOf(init);
  10. }
  11.  
  12. public BigInteger(String init) throws IOException {
  13. if (init == "")
  14. s = "0";
  15. else s = init;
  16. while (s.charAt (0) == '0' && s.length() > 1)
  17. s = s.substring (1);
  18. for(int i=0; i<init.length(); i++){
  19. if(init.charAt(i)< '0' | init.charAt(i)> '9'){
  20. throw new IOException ();
  21. }
  22. }
  23. }
  24.  
  25. public String toString() {
  26. return s;
  27. }
  28.  
  29. public boolean equals(Object other) {
  30. return toString().equals(other.toString());
  31. }
  32.  
  33. public long toLong() {
  34. return Long.parseLong(s);
  35. }
  36.  
  37. public BigInteger add(BigInteger other)throws IOException {
  38. while(this.s.length()>other.s.length()){
  39. other.s = "0" + other.s;
  40. }
  41. while(this.s.length()<other.s.length()){
  42. s = "0" + s;
  43. }
  44.  
  45. String result = "";
  46. int remember = 0, temp = 0;
  47.  
  48. for(int i = s.length()- 1;i >= 0;i--){
  49. temp = (int)(s.charAt(i)-'0') + (int)(other.s.charAt(i)-'0') + remember;
  50. if(temp >=10){
  51. temp = temp % 10;
  52. remember = 1;
  53. }
  54. else{
  55. remember = 0;
  56. }
  57. result = Integer.toString(temp) + result;
  58. }
  59. if(remember == 1){
  60. result = '1' + result ;
  61. }
  62. return new BigInteger(result);
  63. }
  64. public int compareTo(BigInteger other){
  65. if(this.s.length() < other.s.length())
  66. return -1;
  67. if(this.s.length() > other.s.length())
  68. return 1;
  69. if(this.s.length() == other.s.length()){
  70. for(int i=0; i<this.s.length(); i++){
  71. if(this.s.charAt(i) < other.s.charAt(i))
  72. return -1;
  73. if(this.s.charAt(i) > other.s.charAt(i))
  74. return 1;
  75. }
  76. }
  77. return 0;
  78. }
  79.  
  80. public BigInteger subtract(BigInteger other)throws IOException {
  81. int remember = 0;
  82. int temp = 0;
  83. String result = new String ();
  84. while(this.s.length()>other.s.length()){
  85. other.s = "0" + other.s;
  86. }
  87. while(this.s.length()<other.s.length()){
  88. s = "0" + s;
  89. }
  90. BigInteger s1 = new BigInteger(s);
  91. if(s1.compareTo(other)==1 || s1.compareTo(other)==0)
  92. {
  93. for(int i = s.length()- 1;i >= 0;i--){
  94. temp = (int)(this.s.charAt(i))-'0' - ((int)(other.s.charAt(i))-'0' + remember);
  95. if(temp <0)
  96. {
  97. temp = (int)(this.s.charAt(i))-'0' + 10 - ((int)(other.s.charAt(i))-'0' + remember);
  98. remember = 1;
  99. }
  100. else{
  101. remember=0;
  102. }
  103. result = Integer.toString(temp) + result;
  104. }
  105. return new BigInteger(result);
  106. }
  107. else{
  108. for(int i = s.length()- 1;i >= 0;i--){
  109. temp = (int)(other.s.charAt(i))-'0' - ((int)(this.s.charAt(i))-'0' + remember);
  110. if(temp <0)
  111. {
  112. temp = (int)(other.s.charAt(i))-'0' + 10 - ((int)(this.s.charAt(i))-'0' + remember);
  113. remember = 1;
  114. }
  115. else{
  116. remember=0;
  117. }
  118. result = Integer.toString(temp) + result;
  119. }
  120. return new BigInteger("-" + result);
  121.  
  122. }
  123. }
  124. public BigInteger clone(){
  125. BigInteger init = null;
  126. try{
  127. init = new BigInteger(s);
  128. } catch (IOException e){
  129. System.out.println("day co ki tu char!");
  130. }
  131. return init;
  132. }
  133. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:5: error: class BigInteger is public, should be declared in a file named BigInteger.java
public class BigInteger
       ^
1 error
stdout
Standard output is empty