-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBigInt.cpp
464 lines (410 loc) · 10.5 KB
/
BigInt.cpp
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/**
* BigInt class stores and manupulates Integers as an ordered array of
* groups of digits where a group is considered as an integer. Thi approach
* allows faster integer operations as compared to traditional way of treating
* large integers as array of chars with each char representing one digit.
*
* Author: Varun Gupta <thevarungupta A T gmail>
*
* Caveat: This is a work under progress and therefore you may witness bugs
* Please feel free to report any sort of discrepancies.
*
* Last Modified: July 14, 2009
*
* Proposed Enhancements:
* 1. Dynamic Bucket Allocation. This will remove the upper limit on number
* of buckets and thus number of digits.
* 2. Validation of method parameters
*/
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
using namespace std;
#define MAX 20 // Max. number of buckets. Default 20 buckets (max 20*9 digits).
#define BUCKET_LIMIT 1000000000 // 999999999 is the highest possible number in a single bucket
#define BUCKET_NUM_DIGITS 9 // Max. number of digits of in one bucket
class BigInt {
private:
long long content[MAX]; // Buckets holding actual number
short sign; // -1/0/1; depending on number being negetive, zero r positive respectively
unsigned short length; // Number of buckets actually utilized to hold the number
void addLSBucket(int); // Add Least Significant Buckets: Internally used in multiplication operation
public:
// Constructors
BigInt ();
BigInt (long long);
BigInt (string);
BigInt (const BigInt &n);
// Assignment Operators
BigInt operator = (BigInt);
BigInt operator = (long long); // TODO
// Comparison operators
bool operator == (BigInt);
bool operator == (long long); // TODO
bool operator != (BigInt);
bool operator != (int); // TODO
bool operator < (BigInt);
bool operator < (int); // TODO
bool operator <= (BigInt);
bool operator <= (int); // TODO
bool operator > (BigInt);
bool operator > (int); // TODO
bool operator >= (BigInt);
bool operator >= (int); // TODO
// Arithmetic operators
BigInt operator + (long long);
BigInt operator + (BigInt);
BigInt operator - (long long);
BigInt operator - (BigInt);
BigInt operator * (long long);
BigInt operator * (BigInt);
BigInt operator / (long long); // TODO
BigInt operator / (BigInt); // TODO
BigInt operator % (long long); // TODO
BigInt operator % (BigInt); // TODO
// Methods
string toString(void); // Serializes BigInt object
void debug(void);
};
BigInt :: BigInt () {
length = 0;
sign = 0;
memset(content, 0, sizeof(content));
}
BigInt :: BigInt (long long n) {
length = 0;
if (n == 0) {
sign = 0;
}
else {
sign = (n > 0) ? 1 : -1;
if (n < 0) n = 0 - n; // Removing minus sign
for (int i = 0; n > 0; n /= BUCKET_LIMIT) {
length ++;
content[i ++] = n % BUCKET_LIMIT;
}
}
memset(content + length, 0, (MAX - length) * sizeof(long long));
}
BigInt :: BigInt (string n) {
length = 0;
if (n[0] == '0') {
sign = 0;
memset(content, 0, sizeof(content));
}
else {
int i, posFirstDigit;
stringstream s;
if (n[0] == '-') {
sign = -1;
posFirstDigit = 1;
}
else {
sign = 1;
posFirstDigit = 0;
}
for (i = n.size() - BUCKET_NUM_DIGITS; i >= posFirstDigit; i -= 9) {
s << n.substr(i, BUCKET_NUM_DIGITS);
s >> content[length ++];
s.clear();
}
if (i < posFirstDigit) {
s << n.substr(posFirstDigit, BUCKET_NUM_DIGITS - posFirstDigit + i);
s >> content[length ++];
}
memset(content + length, 0, (MAX - length) * sizeof(long long));
}
}
BigInt :: BigInt (const BigInt &n) {
length = n.length;
sign = n.sign;
memcpy(content, n.content, MAX * sizeof(long long));
}
/**
* BigInt::addLSBucket shifts the contents of BigInt::content array by numBucketsToBeAdded
* places. This is needed while computing the product of two BigInt instances.
* In case of multiplication of two integers:
* 6374 * 23 is computed as 6374 * 3 + 6374 * 2 * 10
* Multiplication by 10 just shifts the content. Same purpose is served by BigInt::addLSBucket
* in our context.
*/
void BigInt :: addLSBucket (int numBucketsToBeAdded) {
if (length + numBucketsToBeAdded > MAX) return;
for (int i = length + numBucketsToBeAdded -1; i >= 0; i --) {
content[i] = (i > numBucketsToBeAdded - 1) ? content[i - numBucketsToBeAdded] : 0;
}
length += numBucketsToBeAdded;
}
string BigInt :: toString(void) {
if (length == 0) return "0";
stringstream s;
if (sign == -1) s << "-";
s << content[length - 1];
for (int i = length - 2; i >= 0; i --) {
if (content[i] < 10) {
s << "00000000" << content[i];
}
else if (content[i] < 100) {
s << "0000000" << content[i];
}
else if (content[i] < 1000) {
s << "000000" << content[i];
}
else if (content[i] < 10000) {
s << "00000" << content[i];
}
else if (content[i] < 100000) {
s << "0000" << content[i];
}
else if (content[i] < 1000000) {
s << "000" << content[i];
}
else if (content[i] < 10000000) {
s << "00" << content[i];
}
else if (content[i] < 100000000) {
s << "0" << content[i];
}
else {
s << content[i];
}
}
return s.str();
}
BigInt BigInt :: operator = (BigInt n) {
sign = n.sign;
length = n.length;
memcpy(content, n.content, sizeof(content));
}
bool BigInt :: operator == (BigInt n) {
if (sign != n.sign) {
return false;
}
else if (length != n.length) {
return false;
}
else {
for (int i = 0; i < length; i ++) {
if (content[i] != n.content[i]) return false;
}
return true;
}
}
bool BigInt :: operator != (BigInt n) {
return !operator == (n);
}
bool BigInt :: operator < (BigInt n) {
if (sign < n.sign) {
return true;
}
else if (sign > n.sign) {
return false;
}
else if (sign == 0) {
return false;
}
else if (sign == 1 && length < n.length) {
return true;
}
else if (sign == 1 && length > n.length) {
return false;
}
else if (sign == 1 && length == n.length) {
for (int i = 0; i < length; i ++) {
if (content[i] < n.content[i]) {
return true;
}
else if (content[i] > n.content[i]) {
return false;
}
}
return false;
}
else if (sign == -1 && length < n.length) {
return false;
}
else if (sign == -1 && length > n.length) {
return true;
}
else if (sign == -1 && length == n.length) {
for (int i = 0; i < length; i ++) {
if (content[i] < n.content[i]) {
return false;
}
else if (content[i] > n.content[i]) {
return true;
}
}
return false;
}
}
bool BigInt :: operator <= (BigInt n) {
return (operator < (n) || operator == (n));
}
bool BigInt :: operator > (BigInt n) {
return !operator <= (n);
}
bool BigInt :: operator >= (BigInt n) {
return !operator < (n);
}
BigInt BigInt :: operator + (BigInt n) {
if (length == 0) {
return n;
}
else if (n.length == 0) {
BigInt num(*this);
return num;
}
else if (sign == n.sign) {
BigInt num;
num.length = (length > n.length) ? length : n.length;
num.sign = sign;
long long bucketSum, carry = 0;
for (int i = 0; i < num.length; i ++) {
bucketSum = content[i] + n.content[i] + carry;
num.content[i] = bucketSum % BUCKET_LIMIT;
carry = bucketSum / BUCKET_LIMIT;
}
if (carry > 0) {
num.content[length ++] = carry;
}
return num;
}
else {
BigInt num;
if (sign == -1) {
num = (*this);
num.sign = 1;
return n - num;
}
else {
num = n;
num.sign = 1;
return (*this) - num;
}
}
}
BigInt BigInt :: operator + (long long n) {
return (*this) + BigInt(n);
}
BigInt BigInt :: operator - (BigInt n) {
if (sign == n.sign) {
bool hasZeroValue = true;
if (length == 0) {
BigInt subtrahend(n);
subtrahend.sign *= -1;
}
else if (n.length == 0) {
BigInt num(*this);
return num;
}
if (*this > n) {
BigInt minuend(*this);
for (int i = 0; i < minuend.length; i ++) {
if (minuend.content[i] >= n.content[i]) {
minuend.content[i] -= n.content[i];
}
else {
minuend.content[i] = 10 * minuend.content[i] - n.content[i];
minuend.content[i + 1] --;
}
if (hasZeroValue && minuend.content[i] != 0) hasZeroValue = false;
}
return hasZeroValue ? BigInt(0) : minuend;
}
else {
BigInt minuend(n);
for (int i = 0; i < minuend.length; i ++) {
if (minuend.content[i] >= content[i]) {
minuend.content[i] -= content[i];
}
else {
minuend.content[i] = 10 * n.content[i] - content[i];
minuend.content[i + 1] --;
}
if (hasZeroValue && minuend.content[i] != 0) hasZeroValue = false;
}
if (hasZeroValue) {
return BigInt(0);
}
else {
minuend.sign *= -1;
return minuend;
}
}
}
else {
BigInt subtrahend(n);
if (n.sign = -1) {
subtrahend.sign = 1;
}
else {
subtrahend.sign = -1;
}
return operator + (subtrahend);
}
}
BigInt BigInt :: operator - (long long n) {
return (*this) - BigInt(n);
}
BigInt BigInt :: operator * (long long n) {
if (n == 0 || length == 0) return BigInt(0);
long long carry = 0, tempProduct;
BigInt num(*this);
num.sign = ((n > 0 && sign == 1) || (n < 0 && sign == -1)) ? 1 : -1;
if (n < 0) n *= -1;
for (int i = 0; i < length; i ++) {
tempProduct = num.content[i] * n;
num.content[i] = (tempProduct + carry) % BUCKET_LIMIT;
carry = (tempProduct + carry) / BUCKET_LIMIT;
}
if (carry > 0) num.content[num.length ++] = carry;
return num;
}
BigInt BigInt :: operator * (BigInt n) {
BigInt rs, temp;
rs = n * content[0];
for (int i = 1; i < length; i ++) {
temp = n * content[i];
temp.addLSBucket(i);
rs = rs + temp;
}
rs.sign = (sign == n.sign) ? 1 : -1;
return rs;
}
void BigInt :: debug (void) {
cout << endl << "--------------------------------------------------" << endl;
cout << "In debug mode" << endl;
cout << "Number: " << toString() << endl;
cout << "Sign: " << sign << "\tLength: " << length << endl;
cout << "Content: ";
for (int i = 0; i < MAX; i ++) {
cout << content[i] << " ";
}
cout << endl << "--------------------------------------------------" << endl;
}
int main ()
{
BigInt a("9999999999123456789123456"), b("12345678912"), c;
cout << (a - 12345678912).toString() << endl;
c = a + b;
cout << a.toString() + " + " + b.toString() + " = " + c.toString() << endl;
c = a - b;
cout << a.toString() + " - " + b.toString() + " = " + c.toString() << endl;
c = a * b;
cout << a.toString() + " * " + b.toString() + " = " + c.toString() << endl;
if (a == b) {
cout << a.toString() + " is equal to " + b.toString() << endl;
}
else {
cout << a.toString() + " is not equal to " + b.toString() << endl;
}
if (a > b) {
cout << a.toString() + " is greater than " + b.toString() << endl;
}
else {
cout << a.toString() + " is not greater than " + b.toString() << endl;
}
return 0;
}