-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargeInteger.java
383 lines (311 loc) · 12.4 KB
/
LargeInteger.java
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import java.util.Scanner;
public class LargeInteger {
String number;
boolean isNegative;
public LargeInteger(){
this.number = "0";
isNegative = false;
}
// TODO add the rest of your implementation here!!!
public LargeInteger(String str) {
for ( int i = 0; i < str.length(); i++ ){
if (!((str.charAt(i) >= '0' && str.charAt(i) <= '9') || (str.charAt(i) == '-'))){
throw new IllegalArgumentException( "Must be an integer!!" );
}
}
this.number = str;
}
public LargeInteger(long l) {
this.number = String.valueOf(l);
}
public LargeInteger(LargeInteger li) {
this.number = li.number;
}
public boolean equals(LargeInteger li){
String liCopy = li.number;
if ( liCopy.length() != this.number.length() ){
return false;
} else{
for ( int i = 0; i < liCopy.length(); i++ ){
if ( liCopy.charAt(i) != (this.number).charAt(i) ){
return false;
}
}
}
return true;
}
public int[] toIntArray( String given, int len ){
int[] result = new int[len];
int start = 0;
isNegative = false;
if ( given.charAt(0) == '-'){
start = 1;
isNegative = true;
result[0] = 0;
}
for ( int i = start; i < len; i++ ){
result[i] = Integer.parseInt(String.valueOf(given.charAt(i)));
}
return result;
}
public LargeInteger add(LargeInteger li) {
int result = 0;
int carry = 0;
int len1 = this.number.length();
int len2 = (li.number).length();
int smallLen, bigLen;
int num1, num2;
int diffLen = 0;
int[] finalAnswer;
int[] oneCopy = toIntArray( this.number, len1);
int[] twoCopy = toIntArray( li.number, len2 );
String resultStr = "";
LargeInteger a = new LargeInteger();
LargeInteger b = new LargeInteger();
//handles negative numbers
if ( this.isNegative && !li.isNegative ){
a = new LargeInteger(li.number);
b = new LargeInteger(this.number);
a.isNegative = false;
return a.subtract(b);
} else if ( !this.isNegative && li.isNegative){
a = new LargeInteger(this.number);
b = new LargeInteger(li.number);
b.isNegative = false;
return a.subtract(b);
} else {
//checks whether to put negative sign or not
if ( this.isNegative && li.isNegative ){
resultStr += "-";
}
//this is where the actual calculations begin
if ( ( len1 > len2 ) ){
diffLen = len1 - len2;
finalAnswer = new int[len1];
for ( int i = len1 - 1; i >= 0; i-- ){
num1 = oneCopy[i];
if ( (i - diffLen) < 0 ){
num2 = 0;
} else{
num2 = twoCopy[i - diffLen];
}
result = num1 + num2 + carry;
finalAnswer[i] = result % 10; //places the result to this index
carry = result / 10;
}
finalAnswer[0] += (carry * 10);
} else{
//when len2 > len1
diffLen = len2 - len1;
finalAnswer = new int[len2];
for ( int i = len2 - 1; i >= 0; i-- ){
num2 = twoCopy[i];
if ( (i - diffLen) < 0 ){
num1 = 0;
} else{
num1 = oneCopy[i - diffLen];
}
result = num1 + num2 + carry;
finalAnswer[i] = result % 10; //places the result to this index
carry = result / 10;
}
finalAnswer[0] += (carry * 10);
} //end inner if-else condition
//the int elements in the finalAnswer array are stored in a string
for ( int j = 0; j < finalAnswer.length; j++ ){
resultStr += String.valueOf(finalAnswer[j]);
}
} //end outer if-else
return new LargeInteger(resultStr);
}
public LargeInteger subtract(LargeInteger li) {
int result = 0;
int len1 = (this.number).length();
int len2 = (li.number).length();
int diffLen = 0;
int minuNum = 0;
int subtraNum = 0;
int[] minuendCopy = toIntArray( this.number, len1);
int[] subtrahendCopy = toIntArray( li.number, len2 );
String resultStr = "";
int[] finalAnswer;
LargeInteger a = new LargeInteger();
LargeInteger b = new LargeInteger();
//handles negatie numbers
if ( this.isNegative && !li.isNegative ){
a = new LargeInteger(this.number);
b = new LargeInteger(li.number);
b.isNegative = true;
return a.add(b);
} else if ( !this.isNegative && li.isNegative){
a = new LargeInteger(this.number);
b = new LargeInteger(li.number);
b.isNegative = false;
return a.add(b);
} else {
//checks whether to put negative sign or not
if ( this.isNegative && li.isNegative ){
resultStr += "-";
}
//this is where the operations begin
if ( ( len1 > len2 ) ){
diffLen = len1 - len2;
finalAnswer = new int[len1];
for ( int i = len1 - 1; i >= 0; i-- ){
minuNum = minuendCopy[i];
if ( (i - diffLen) < 0 ){
subtraNum = 0;
} else{
subtraNum = subtrahendCopy[i - diffLen];
} //end first if-else
//when this happens, we need to borrow
if ( subtraNum > minuNum ){
minuNum = 10 + minuNum;
if ( (i - 1) > 0 ){
borrow( minuendCopy, i - 1);
}
} //end second if-else
result = minuNum - subtraNum;
finalAnswer[i] = result; //places the result to this index
} //end inner for
} else{
//when len2 > len1
diffLen = len2 - len1;
finalAnswer = new int[len2];
//this time ang subtraNum - minuNum
//but finalRes is negative
for ( int i = len2 - 1; i >= 0; i-- ){
subtraNum = subtrahendCopy[i];
if ( (i - diffLen) < 0 ){
minuNum = 0;
} else{
minuNum = minuendCopy[i - diffLen];
}
//when this happens, we need to borrow
if ( minuNum > subtraNum ){
subtraNum = 10 + subtraNum;
if ( (i - 1) > 0 ){
borrow( subtrahendCopy, i - 1);
}
}
result = subtraNum - minuNum;
finalAnswer[i] = result; //places the result to this index
}
} //end outer if-else
} //end outermost if-else
//to eliminate leading zeros
int k = 0;
for (int i = 0; i < finalAnswer.length; i++){
if ( finalAnswer[i] > 0 ){
k = i;
break;
} else{
k = i;
}
}
//store the finalAnswer to return as String
for ( int j = k; j < finalAnswer.length; j++ ){
resultStr += String.valueOf(finalAnswer[j]);
}
return new LargeInteger(resultStr);
}
public void borrow( int[] arr, int index ){
if ( (arr[index] != 0) ){
arr[index]--;
} else if ( index <= 0 ){
//do nothing
} else{
arr[index] = 9;
borrow(arr, index - 1);
}
}
public LargeInteger multiply(LargeInteger li) {
int len1 = (this.number).length();
int len2 = (li.number).length();
int smallLen = 0;
int bigLen = 0;
String biggerNum;
LargeInteger finalResult = new LargeInteger();
int[] smallerArr;
if ( this.isNegative != li.isNegative ){
finalResult.number = "-" + finalResult.number;
}
if ( this.number.charAt(0) == '1' || li.number.charAt(0) == '1'){
if ( this.number.charAt(0) == '1' ){
return new LargeInteger(li.number);
}
return new LargeInteger(this.number);
} else if ( this.number.charAt(0) == '0' || li.number.charAt(0) == '0'){
return new LargeInteger("0");
} else{
if ( len1 > len2 ){
smallLen = len2;
bigLen = len1;
biggerNum = this.number;
smallerArr = toIntArray( li.number, smallLen );
} else{
smallLen = len1;
bigLen = len2;
biggerNum = li.number;
smallerArr = toIntArray( this.number, smallLen );
}
String[] resultArr = new String[bigLen];
LargeInteger result = new LargeInteger("0");
//Multiplies each element of the smallerArr to the whole biggerArr value,
//then stores each result to the the resultArr array
for ( int i = (smallLen - 1), k = 0; i >= 0; i--, k++ ){
result = LargeInteger("0");
for ( int j = 1; j <= smallerArr[i]; j++ ){
result = result.add(new LargeInteger(biggerNum));
}
finalResult = finalResult.add( new LargeInteger(addZerosToEnd(result, k)) );
}
}
return finalResult;
}
public String addZerosToEnd( LargeInteger liToBeAddedWith, int numOfZerosToAdd ){
String result = liToBeAddedWith.number;
for ( int i = 1; i <= numOfZerosToAdd; i++ ){
result += "0";
}
return result;
}
public LargeInteger divide(LargeInteger li) {
return li;
}
public String toString(){
return this.number;
}
// NOTE: no need to change this method!
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Simply read input from console and perform operations using the LargeInteger class
String operand1 = sc.next();
String operator = sc.next();
String operand2 = sc.next();
// To test String constructor
LargeInteger myInt1 = new LargeInteger(operand1);
// To test LargeInteger constructor
LargeInteger myInt2 = new LargeInteger(new LargeInteger(operand2));
// To test long constructor
LargeInteger result = new LargeInteger(0L);
switch(operator) {
case "+":
result = myInt1.add(myInt2);
break;
case "-":
result = myInt1.subtract(myInt2);
break;
case "*":
result = myInt1.multiply(myInt2);
break;
case "/":
result = myInt1.divide(myInt2);
break;
default:
// do nothing...
}
// print out result:
System.out.println(result); // this line invokes LargeInteger.toString()
}
}