Repository navigation
Expand file tree
/
Copy pathBigInt.java
More file actions
174 lines (158 loc) · 4.41 KB
/
Copy pathBigInt.java
File metadata and controls
174 lines (158 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package pl.edu.mimuw;
import java.util.Arrays;
import static java.lang.Integer.max;
import static java.lang.Integer.min;
import static java.lang.Math.abs;
public final class BigInt {
private final int[] digits;
private final boolean isPositive;
public BigInt(String number) {
int b;
if(number.substring(0,1) == "-")
b=1;
else
b=0;
String digitchars=number.substring(b,number.length());
int digitints[] = new int[digitchars.length()], p;
for (int i=0; i<digitchars.length(); i++){
p=digitchars.charAt(i);
digitints[i]=p-'0';
}
this.digits=digitints;
this.isPositive=(b==0);
//throw new IllegalStateException("TODO task 1: parse number from valid String");
}
public BigInt(int[] digits, boolean isPositive) {
this.digits = digits;
this.isPositive = isPositive;
}
public BigInt(int number) {
this(Integer.toString(number));
}
public BigInt neg() {
final var digits = Arrays.copyOf(this.digits, this.digits.length);
return new BigInt(digits, !this.isPositive);
}
public BigInt add(BigInt other) {
int a, b, pa, pb, ta[], tb[];
boolean sign;
if (this.digits.length>other.digits.length) {
sign=this.isPositive;
a=this.digits.length;
ta=this.digits;
if(this.isPositive)
pa=1;
else
pa=-1;
b=other.digits.length;
tb=other.digits;
if(other.isPositive)
pb=1;
else
pb=-1;
} else {
if (this.digits.length<other.digits.length){
sign=other.isPositive;
} else {
int i=0;
while (this.digits[i]==other.digits[i]){
i++;
}
if(this.digits[i]>other.digits[i])
sign=this.isPositive;
else
sign=other.isPositive;
}
b=this.digits.length;
tb=this.digits;
if(this.isPositive)
pb=1;
else
pb=-1;
a=other.digits.length;
ta=other.digits;
if(other.isPositive)
pa=1;
else
pa=-1;
}
int r = 0, da, db, helper;
int[] tab = new int[a + 1];
for (int i = 1; i <= b; i++) {
da = pa * ta[a - i];
db = pb * tb[b - i];
helper=da+db+r;
if (helper<0 && sign) {
tab[a + 1 + i] = abs((-helper)%10);
r=helper/10;
} else {
tab[a+1+i] =helper%10;
r=helper/10;
}
}
for (int i=b+1; i<a; i++){
tab[a-i]=abs((pa*ta[a-i]+r))%10;
r=(pa*ta[a-i]+r)/10;
}
if (r!=0) {
tab[0] = abs(r);
return new BigInt(tab, (r>0));
}
else {
return new BigInt(tab, sign);
}
//throw new IllegalStateException("TODO task 2: implement addition");
}
public BigInt multi(int m) {
String s="";
int r=0;
for (int i=1; i<this.digits.length; i++){
s+='0'+(m*this.digits[this.digits.length-i]+r)%10;
r=(m*this.digits[this.digits.length-i]+r)/10;
}
if (!this.isPositive)
s+='-';
StringBuilder reverseString = new StringBuilder(s);
reverseString.reverse();
return new BigInt(reverseString.toString());
}
public BigInt expo(int m) {
for (int i=0; i<m; i++){
this.digits[this.digits.length+m]=0;
}
return this;
}
public BigInt times(BigInt other) {
boolean sign = (this.isPositive==other.isPositive);
BigInt acc = new BigInt(new int[0], true);
for (int i=1; i<=this.digits.length; i++){
acc=acc.add((other.multi(this.digits[this.digits.length-i])).expo(i-1));
}
return new BigInt(acc.digits, sign);
//throw new IllegalStateException("TODO task 3: implement multiplication");
}
@Override
public String toString() {
int l=this.digits.length;
String s="";
if (!this.isPositive)
s+="-";
for (int i=0; i<l; i++)
s+='0'+this.digits[i];
return s;
//throw new IllegalStateException("TODO task 4: implement creating representation");
}
@Override
public int hashCode() {
final var arrayHashCode = Arrays.hashCode(digits);
final var positiveHashCode = Boolean.hashCode(isPositive);
return arrayHashCode * 17 + positiveHashCode;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof BigInt)) return false;
final var other = (BigInt) obj;
return Arrays.equals(this.digits, other.digits)
&& this.isPositive == other.isPositive;
}
}