-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigNum.cxx
804 lines (671 loc) · 14 KB
/
BigNum.cxx
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
//Sarah Niemeyer
//BigNum Part 2
//100027519
//Matt Sainz
// FILENAME: BigNum.cxx
// This is the implementation file of the BigNum class
#ifndef HW3_BIGNUM_CXX
#define HW3_BIGNUM_CXX
#include <algorithm> // Provides copy function
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
#include <cassert>
#include "BigNum.h"
using namespace std;
namespace HW3
{
BigNum::BigNum()
{
capacity = DEFAULT_CAPACITY;
digits = new unsigned int[capacity];
digits[0] = 0;
positive = true;
used = 1;
}
BigNum::BigNum(int num)
{
digits = nullptr;
if (num == 0)
{
BigNum zero;
*this = zero;
}
else
{
if (num > 0)
{
positive = true;
}
else
{
positive = false;
num = -num;
}
unsigned int i = 0, temp = num;
// count the digits
while (temp > 0)
{
temp = temp/10;
i++;
}
capacity = i;
digits = new unsigned int[capacity];
temp = num;
for (used = 0; used < i; ++used)
{
digits[used] = temp % 10;
temp /= 10;
}
}
}
// Constructor that receives a string; leading 0's will be ignored
BigNum::BigNum(const string& strin)
{
digits = nullptr;
int len = strin.length();
if (len == 0)
{
BigNum zero;
*this = zero;
return;
}
used = len;
positive = true;
int i = 0;
if(strin[i] == '-')
{
positive = false;
i = 1;
used--;
}
else if(strin[i] == '+')
{
i = 1;
used--;
}
capacity = used;
digits = new unsigned int[capacity];
for(unsigned int k = 0; k < used; ++k)
{
digits[used - k - 1] = strin[i++] - '0';
}
if (used == 1 && digits[0] == 0)
positive = true;
trim();
}
BigNum::BigNum(const BigNum& anotherBigNum)
{
digits = nullptr;
// makes operator = do the work; use that function if you use this
*this = anotherBigNum;
}
BigNum::~BigNum()
{
delete [] digits;
}
// assume doubling is done before passing in n
void BigNum::resize(unsigned int n)
{
unsigned int *largerArray;
if (n < used) return; // Can't allocate less than we are using
capacity = n;
largerArray = new unsigned int[capacity];
copy(digits, digits + used, largerArray);
delete [] digits;
digits = largerArray;
}
BigNum& BigNum::operator=(const BigNum& anotherBigNum)
{
if (this == &anotherBigNum) return *this;
if (digits != nullptr)
delete [] digits;
capacity = anotherBigNum.capacity;
digits = new unsigned int[capacity];
positive = anotherBigNum.positive;
used = anotherBigNum.used;
copy(anotherBigNum.digits, anotherBigNum.digits + used, digits);
return *this;
}
BigNum& BigNum::operator+=(const BigNum& addend)
{
BigNum newBigNum = *this + addend;
*this = newBigNum;
return *this;
}
BigNum& BigNum::operator-=(const BigNum& subtractand)
{
BigNum newBigNum = *this - subtractand;
*this = newBigNum;
return *this;
}
BigNum& BigNum::operator*=(const BigNum& multiplicand)
{
BigNum newBigNum = *this * multiplicand;
*this = newBigNum;
return *this;
}
BigNum& BigNum::operator/=(const BigNum& divisor)
{
BigNum newBigNum = BigNum(divisor);
*this = *this/newBigNum;
return *this;
}
BigNum& BigNum::operator%=(const BigNum& divisor)
{
BigNum newBigNum = BigNum(divisor);
*this = *this%newBigNum;
return *this;
}
BigNum& BigNum::operator++()
{
BigNum newBigNum = BigNum(1);
*this = *this + newBigNum;
return *this;
}
BigNum& BigNum::operator--()
{
BigNum newBigNum = BigNum(1);
*this = *this - newBigNum;
return *this;
}
BigNum& BigNum::diff(const BigNum& a, const BigNum& b)
{
unsigned int borrow = 0;
unsigned int c = 0;
used = 0;
BigNum zero = 0;
BigNum temp;
temp.resize(a.used);
*this = temp;
if(a == zero)
{
*this = zero;
return *this;
}
if(b == zero)
{
*this = a;
return *this;
}
while(c < b.used)
{
if(a.digits[c] < b.digits[c] + borrow)
{
digits[c] = a.digits[c]+ 10 - b.digits[c] -borrow;
borrow = 1;
}
else
{
digits[c] = a.digits[c] - b.digits[c] - borrow;
borrow = 0;
}
used++;
c++;
}
used--;
while(c < a.used)
{
if(a.digits[c] < borrow)
{
digits[c] = a.digits[c] + 10 - borrow;
borrow = 1;
}
else
{
digits[c] = a.digits[c] -borrow;
borrow = 0;
}
used++;
c++;
}
trim();
return *this;
}
BigNum& BigNum::mult(const BigNum& a, const BigNum& b)
{
BigNum zero;
*this = zero;
resize(a.used + b.used);
unsigned int product = 0;
unsigned int carry;
unsigned int i, x, y;
if(a == 1)
{
*this = b;
return *this;
}
else if(b == 1)
{
*this = a;
return *this;
}
for (i = 0; i < a.used; ++i)
{
BigNum int_result;
if (a.digits[i] > 0)
{
int_result.resize(a.used + b.used);
int_result.used = 0;
for (y = 0; y < i; ++y)
{
int_result.used = int_result.used + 1;
int_result.digits[y] = 0;
}
carry = 0;
for (x = 0; x < b.used; ++x)
{
product = a.digits[i] * b.digits[x];
product += carry;
int_result.used = int_result.used + 1;
int_result.digits[y + x] = product % 10;
carry = product / 10;
}
while (carry > 0)
{
int_result.used = int_result.used + 1;
int_result.digits[y + x] = carry % 10;
carry = carry / 10;
++y;
}
int_result.trim();
}
*this += int_result;
}
trim();
return *this;
}
BigNum& BigNum::sum(const BigNum& a, const BigNum& b)
{
unsigned int sum = 0;
unsigned int carry = 0;
unsigned int number = 0;
unsigned int x, y;
used = 0;
if(a.used == b.used && a.used ==1 &&a.digits[0] == 0)
{
used = 1;
digits[0] = 0;
return *this;
}
if(a.used > b.used)
{
y = b.used;
x = a.used;
}
else
{
y = a.used;
x = b.used;
}
resize(x+1);
for(unsigned int i = 0; i < x; i++)
{
sum = 0;
number = 0;
if(y> i)
number = a.digits[i]+b.digits[i];
else
if(a.used>b.used)
number = a.digits[i];
else
number = b.digits[i];
number = number + carry;
sum = (sum + number)%10;
carry = number/10;
digits[i] = sum;
used++;
}
if(carry > 0)
{
digits[x] = carry;
used++;
}
trim();
return *this;
}
BigNum operator+(const BigNum& a, const BigNum& b)
{
BigNum result;
if(a.positive ==true && b.positive==true)
{
result.sum(a, b);
result.positive = a.positive;
return result;
}
else if(a.positive == false && b.positive == false)
{
BigNum temp1 = a;
BigNum temp2 = b;
temp1.positive = true;
temp2.positive = true;
result.sum(temp1, temp2);
result.positive = false;
return result;
}
else if(a.positive == true && b.positive == false)
{
BigNum temp1 = b;
temp1.positive = true;
if(a > temp1)
{
result.diff(a, temp1);
result.positive = true;
return result;
}
else if(a < temp1)
{
result.diff(temp1, a);
result.positive = false;
return result;
}
}
else if(a.positive == false && b.positive == true)
{
BigNum temp1 = a;
temp1.positive = true;
if(temp1 > b)
{
result.diff(temp1, b);
result.positive = false;
return result;
}
else if (temp1 < b)
{
result.diff(b, temp1);
result.positive = true;
return result;
}
}
return result;
}
BigNum operator-(const BigNum& a, const BigNum& b)
{
BigNum result;
//see comments for operator +
if(a.positive == true && b.positive == true)
{
if(a.used > b.used)
{
result.diff(a, b);
result.positive = true;
return result;
}
else if(a.used < b.used)
{
result.diff(b, a);
result.positive = false;
return result;
}
else
{
if(a>=b)
{
result.diff(a, b);
result.positive = true;
return result;
}
else
{
result.diff(b,a);
result.positive = false;
return result;
}
}
}
else if(a.positive == true && b.positive == false)
{
BigNum temp1 = b;
temp1.positive = true;
result.sum(a, temp1);
result.positive = true;
}
else if(a.positive == false && b.positive == false)
{
if(a.used < b.used)
{
BigNum temp1 = a;
BigNum temp2 = b;
temp1.positive = true;
temp2.positive = true;
result.diff(temp2, temp1);
result.positive = true;
}
else if(a.used > b.used)
{
BigNum temp1 = a;
BigNum temp2 = b;
temp1.positive = true;
temp2.positive = true;
result.diff(temp1, temp2);
result.positive = false;
}
else
{
BigNum temp1 = a;
BigNum temp2 = b;
temp1.positive = true;
temp2.positive = true;
if(a > b)
{
result.diff(temp1, temp2);
result.positive = false;
}
else
{
result.diff(temp2, temp1);
result.positive = true;
}
}
}
else if(a.positive == false && b.positive == true)
{
BigNum temp1 = a;
temp1.positive = true;
result.sum(temp1, b);
result.positive = false;
}
return result;
}
BigNum operator*(const BigNum& a, const BigNum& b)
{
BigNum result;
result.mult(a, b);
//if they both have the same sign, the result is positive;
//if they have different signs, the result is negative
if(a.positive == b.positive)
{
result.positive = true;
}
else
{
result.positive = false;
}
return result;
}
BigNum operator / (const BigNum& a, const BigNum& b)
{
BigNum result;
BigNum anew = BigNum(a);
BigNum bnew = BigNum(b);
//if they're both the same sign, the answer is positive; if they
//are different signs, the answer is negative
if(anew.positive == false)
{
anew.positive = true;
}
if(bnew.positive == false)
{
bnew.positive = true;
}
//if the divisor is 0, you can't divide it!
if(result == b)
{
cout<<"THE LIMIT DOES NOT EXIST!"<<endl;
}
//if the divisor is 1 then the answer is the dividend
else if(b == 1)
{
result = a;
return result;
}
else if(a == 0)
{
result.used = 1;
result.digits[0] = 0;
return result;
}
//if the divisor is bigger than the dividend, the answer is zero
else if(anew < bnew)
{
return result;
}
//if the dividend is bigger than the divisor, then keep adding one
//to the result until it times the divisor is equal to or just less
//than the dividend BUT you have to decriment result once you're done
//with this because we have to do ++result because that is how you
//can impliment the ++ function with a BigNum... you may also
//be able to just say while anew>result*bnew rather than >=?
else if(anew > bnew)
{
while(anew >= result*bnew)
{
++result;
}
--result;
}
//same as before.. if they're both the same sign then the answer
//is positive and if they're different signs the answer is -
if(a.positive == b.positive)
{
result.positive = true;
}
else
{
result.positive = false;
}
return result;
}
BigNum operator%(const BigNum& a, const BigNum& b)
{
BigNum result;
result = (a - (a/b) * b);
return result;
}
bool operator>(const BigNum& a, const BigNum& b)
{
if (a.positive == true && b.positive == false) return true;
else if (a.positive == false && b.positive == true) return false;
else
{
if (a.used > b.used)
{
if (a.positive == true) return true;
else return false;
}
else if (a.used < b.used)
{
if (a.positive == true) return false;
else return true;
}
else
{
for (unsigned int i = 0; i < a.used; ++i)
{
if (a.digits[a.used - 1 - i] < b.digits[b.used - 1 - i])
{
if(a.positive == true) return false;
else return true;
}
if (a.digits[a.used - 1 - i] > b.digits[b.used - 1 - i])
{
if(a.positive == true) return true;
else return false;
}
}
}
}
return false;
}
bool operator>=(const BigNum& a, const BigNum& b)
{
return ((a > b) || (a == b));
}
bool operator<(const BigNum& a, const BigNum& b)
{
return !(a >= b);
}
bool operator<=(const BigNum& a, const BigNum& b)
{
return !(a > b);
}
bool operator==(const BigNum& a, const BigNum& b)
{
if ((a.positive != b.positive) || (a.used != b.used))
return false;
for (unsigned int i = 0; i < a.used; i++)
{
if (a.digits[a.used - 1 - i] != b.digits[b.used - 1 - i])
return false;
}
return true;
}
int get_int_len(int value)
{
int l = 1;
while(value> 9)
{
l++;
value/=10;
}
return l;
}
bool operator!=(const BigNum& a, const BigNum& b)
{
return !(a == b);
}
// trim leading zeros
void BigNum::trim()
{
while (used > 1 && digits[used-1] == 0)
used--;
}
std::ostream& operator<<(std::ostream &os, const BigNum& bignum)
{
unsigned int i = 0;
unsigned int j = 0;
if (bignum.positive == false) os << '-';
for (i=0; i<bignum.used; ++i)
{
os << bignum.digits[bignum.used - i - 1];
if (j < 60) ++j;
else
{
os << endl;
j = 0;
}
}
return os;
}
std::istream& operator>>(std::istream &is, BigNum& bignum)
{
string str;
is >> str;
BigNum temp = str;
bignum = temp;
return is;
}
BigNum factorial(const BigNum& a)
{
BigNum result;
if(a<=1)
return 1;
else
{
result = a * factorial(a-1);
return result;
}
}
}
#endif