-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigIntCalc.java
272 lines (234 loc) · 5.95 KB
/
BigIntCalc.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
/**
* Big Integer Calculator
*
* Peter Shumate
*/
public class BigIntCalc {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Please enter two integers");
System.out.println("The first integer must be larger than the second, and the second must not be 0");
return;
}
String a = args[0];
String b = args[1];
System.out.printf("ADD RESULT: %s\n", add(a, b));
System.out.printf("SUBTRACT RESULT: %s\n", subtract(a, b));
System.out.printf("MULTIPLY RESULT: %s\n", multiply(a, b));
System.out.printf("DIVIDE RESULT: %s\n", divide(a, b));
}
/**
* Adds two integers (read as strings) together. a + b = c
*
* @param str1 - a
* @param str2 - b
* @return result - c (as String)
*/
public static String add(String str1, String str2) {
int[] bigAr;
int[] smallAr;
int[] sumAr;
if (str1.length() >= str2.length()) {
bigAr = strIntAr(str1);
smallAr = strIntAr(str2);
} else {
bigAr = strIntAr(str2);
smallAr = strIntAr(str1);
}
sumAr = arrPad(bigAr, 1);
int rem = 0;
for (int i = smallAr.length - 1, j = bigAr.length - 1; i >= 0; i--, j--) {
int sum = smallAr[i] + bigAr[j] + rem;
int digit = sum % 10;
rem = sum / 10;
sumAr[j + 1] = digit;
}
int remIndex = bigAr.length - smallAr.length - 1;
while (rem != 0 && remIndex >= 0) {
int sum = bigAr[remIndex] + rem;
int digit = sum % 10;
rem = sum / 10;
sumAr[remIndex + 1] = digit;
remIndex--;
}
sumAr[0] = rem;
return removeZero(sumAr);
}
/**
* Subtracts one integer from another (read as strings). a - b = c
*
* @param str1 - a
* @param str2 - b
* @return result - c (as String)
*/
public static String subtract(String str1, String str2) {
int[] intAr1 = strIntAr(str1);
int[] intAr2 = strIntAr(str2);
boolean borrow = false;
for (int i = intAr1.length - 1, j = intAr2.length - 1; j >= 0; i--, j--) {
int bI = i;
try {
intAr1[i] -= intAr2[j];
while (intAr1[bI] < 0) {
intAr1[bI] += 10;
intAr1[--bI]--;
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: b cannot be greater than a");
return null;
}
}
return removeZero(intAr1);
}
/**
* Multiplies to integers (read as strings) together. a * b = c
*
* @param str1 - a
* @param str2 - b
* @return result - c (as String)
*/
public static String multiply(String str1, String str2) {
int[] bigAr;
int[] smallAr;
int[] prodAr;
if (str1.length() >= str2.length()) {
bigAr = strIntAr(str1);
smallAr = strIntAr(str2);
} else {
bigAr = strIntAr(str2);
smallAr = strIntAr(str1);
}
prodAr = new int[smallAr.length + bigAr.length + 1];
int n = prodAr.length - 1;
for (int i = smallAr.length - 1; i >= 0; i--) {
int rem = 0;
int x = n;
for (int j = bigAr.length - 1; j >= 0; j--) {
int product = smallAr[i] * bigAr[j] + rem;
int digit = product % 10;
rem = product / 10;
prodAr[x] += digit;
if (prodAr[x] >= 10) {
rem += prodAr[x] / 10;
prodAr[x] %= 10;
}
x--;
}
if (rem != 0) {
prodAr[x] += rem;
rem = 0;
}
n--;
}
return removeZero(prodAr);
}
/**
* Divides one integers from another(read as strings). a / b = c
*
* @param str1 - a
* @param str2 - b
* @return result - c (as String)
*/
public static String divide(String str1, String str2) {
int[] ar1 = strIntAr(str1);
int[] ar2 = strIntAr(str2);
if (removeZero(ar2).equals("0")) {
System.out.println("Error: Cannot divide by 0");
return null;
}
int[] result = new int[ar1.length];
int[] initDiv = new int[ar2.length];
int divIndex = -1;
try {
for (int i = 0; i < ar2.length; i++) {
initDiv[i] = ar1[i];
divIndex++;
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Error: b cannot be greater than a");
return null;
}
try {
subtract(removeZero(initDiv), removeZero(ar1));
} catch (ArrayIndexOutOfBoundsException e) {
initDiv = new int[ar2.length + 1];
for (int i = 0; i < ar2.length + 1; i++) {
initDiv[i] = ar1[i];
}
divIndex++;
}
String initStr = removeZero(initDiv);
for (int i = 0; i < ar1.length; i++) {
int count = 0;
String tempInit = new String(initStr);
while (true) {
try {
tempInit = subtract(tempInit, removeZero(ar2));
count++;
} catch (ArrayIndexOutOfBoundsException e) {
break;
}
}
result[divIndex++] = count;
if (divIndex == result.length) {
break;
}
String tempProd = multiply(count + "", str2);
initStr = subtract(initStr, tempProd) + ar1[divIndex];
}
return removeZero(result);
}
/**
* Helper method for converting string to int array.
*
* @param str - String to be converted
* @return int array of string param
*/
private static int[] strIntAr(String str) {
char[] charAr = str.toCharArray();
int[] intAr = new int[charAr.length];
for (int i = 0; i < charAr.length; i++) {
intAr[i] = Character.getNumericValue(charAr[i]);
}
return intAr;
}
/**
* Pads int array with set # of single zeros at beginning of array.
*
* @param arr - array to be padded
* @param zeros - # of zeros to be added to beginning of int array
* @return padded array
*/
private static int[] arrPad(int[] arr, int zeros) {
int[] newArr = new int[arr.length + zeros];
for (int i = 0; i < zeros; i++) {
newArr[i] = 0;
}
for (int i = zeros, oldIndex = 0; oldIndex < arr.length; i++, oldIndex++) {
newArr[i] = arr[oldIndex];
}
return newArr;
}
/**
* Strips zeros from an array and returns a string version.
*
* @param arr - array to be stripped
* @return result - the array represented as a astring with the leading 0's stripped
*/
private static String removeZero(int[] arr) {
String result = "";
boolean zeros = true;
for (int i = 0; i < arr.length; i++) {
if (zeros && arr[i] == 0) {
result += "";
} else {
result += arr[i];
zeros = false;
}
}
if (result.equals("")) {
return "0";
}
return result;
}
}