-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigint.hpp
743 lines (708 loc) · 22.5 KB
/
bigint.hpp
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
#include <iostream>
#include <stdexcept>
#include <vector>
#include <limits>
#include <cmath>
#include <cctype>
#include <string>
using namespace std;
/**
* @brief Enumeration to represent the sign of a bigint number.
*
* Since built-in integer types in C++ do not distinguish between +0 and -0 as in floating point types,
* this enumeration allows representing zero as an independent sign along with positive and negative.
*
*/
enum class sign
{
negative,
zero,
positive
};
/**
* @brief This class implements the arbitrary precision integers and allows simple arithmetic operations
* between these integers.
*
*/
class bigint
{
public:
bigint();
bigint(const int64_t &);
bigint(const string &);
/**
* @brief Construct a new bigint object with another (Default copy constructor)
*
*/
// Start: define an explicit copy constructor to prevent a warning. Source: https://stackoverflow.com/questions/51863588/warning-definition-of-implicit-copy-constructor-is-deprecated
bigint(const bigint &) = default;
//
void set(const int64_t &);
void set(const string &);
bigint &operator=(const bigint &);
bigint operator-() const;
bigint &operator+=(const bigint &);
bigint &operator-=(const bigint &);
bigint &operator*=(const bigint &);
bool operator==(const bigint &) const;
bool operator<(const bigint &) const;
friend ostream &operator<<(ostream &, const bigint &);
/**
* @brief Exception thrown when the input string representing a bigint number contains non-digit characters.
*
*/
inline static invalid_argument non_digit = invalid_argument("The input string contains non digit characters!");
/**
* @brief Exception thrown when the input string contains leading zeros in the beginning.
*
*/
inline static invalid_argument leading_zeros = invalid_argument("The input number cannot have leading zeros!");
/**
* @brief Exception thrown when the input string is empty.
*
*/
inline static invalid_argument empty_string = invalid_argument("The input string is empty!");
private:
/**
* @brief Sign of the current bigint object, can be negative, positive or zero.
*
*/
sign number_sign;
/**
* @brief The vector containing the bigint's digits, each element of the vector is an integer in [0,9].
*
*/
vector<uint8_t> digits;
void fill_digits(const string &);
bool is_digit(const string &) const;
bool is_abs_greater(const bigint &) const;
void zero_remover();
};
/**
* @brief Sets the current bigint number to a signed 64-bit integer number.
*
* @param number A signed 64-bit integer used to set the bigint value with.
*/
void bigint::set(const int64_t &number)
{
if (number == 0)
{
digits.clear();
number_sign = sign::zero;
digits.push_back(0);
}
else
{
if (number < 0)
number_sign = sign::negative;
else
number_sign = sign::positive;
// if the input number is INT64_MIN, casting it to int64_t will cause overflow, so I casted it to uint64_t
// Start: Cast from int to uint8_t source: https://stackoverflow.com/questions/57746321/implicit-conversion-warning-int-to-int-lookalike
uint64_t temp = static_cast<uint64_t>(abs(number));
size_t number_of_digits = static_cast<size_t>(log10(temp) + 1);
// End
size_t cursor = 0;
digits.resize(number_of_digits);
while (temp != 0)
{
// Start: Cast from int to uint8_t source: https://stackoverflow.com/questions/57746321/implicit-conversion-warning-int-to-int-lookalike
digits[cursor] = static_cast<uint8_t>(temp % 10);
// End
temp = temp / 10;
cursor++;
}
}
}
/**
* @brief Sets the current bigint object to the value represented by a string.
*
* It parses the string, assuming it represents a decimal integer number and throws an exception when the number contains non-digit
* characters, has leading zeros or the string is empty. It will also parse the sign of the number and assign the `bigint::number_sign` accordingly.
*
* @param input_string An input string representing a bigint number to set the value of current object with.
*/
void bigint::set(const string &input_string)
{
if (input_string.empty())
throw empty_string;
if (input_string == "+0" or input_string == "-0")
{
digits.clear();
number_sign = sign::zero;
digits.push_back(0);
}
if (input_string[0] == '0')
{
if (input_string.length() == 1)
{
digits.clear();
number_sign = sign::zero;
digits.push_back(0);
}
else
throw leading_zeros;
}
else if (input_string[0] == '-')
{
fill_digits(input_string.substr(1, input_string.length() - 1));
number_sign = sign::negative;
}
else if (input_string[0] == '+')
{
fill_digits(input_string.substr(1, input_string.length() - 1));
number_sign = sign::positive;
}
else
{
fill_digits(input_string);
number_sign = sign::positive;
}
}
/**
* @brief Construct a new bigint::bigint object representing the number zero.
*
*/
bigint::bigint()
{
number_sign = sign::zero;
digits.push_back(0);
}
/**
* @brief Construct a new bigint::bigint object with a signed 64-bit integer using the `set` member function.
*
* @param number A signed 64 input number to initialize the object with.
*/
bigint::bigint(const int64_t &number)
{
set(number);
}
/**
* @brief Construct a new bigint::bigint object with a string using the `set` member function.
*
* @param input_string An input string representing a bigint number to initialize the object with.
*/
bigint::bigint(const string &input_string)
{
set(input_string);
}
/**
* @brief Checks whether a string represents a number.
*
* @param input_string An input string to be checked.
* @return true If all the characters in the string are digits.
* @return false If the string has any non-digit characters.
*/
// Start: check a string with character classification functions, source https://baraksh.com/CSE701/notes.php#io-error-handling
bool bigint::is_digit(const string &input_string) const
{
for (const char &c : input_string)
if (!isdigit(c))
return false;
return true;
}
// End
/**
* @brief Parses a string representing a bigint number and fills it in the `bigint::digits` vector.
*
* This function checks the validity of `input_string` and ensures it only contains digit characters.
* Then it fill fill out the `bigint::digits` vector in a reverse order, meaning that the least significant
* digit will be filled into the first element of vector.
*
* @param input_string An input string representing a bigint number.
*/
void bigint::fill_digits(const string &input_string)
{
if (input_string[0] == '0')
throw leading_zeros;
else if (!is_digit(input_string))
throw non_digit;
digits.resize(input_string.length());
for (size_t i = 0; i < input_string.length(); i++)
{
// Start: Convert char to int, source: https://sentry.io/answers/char-to-int-in-c-and-cpp/
// Cast from int to uint8_t source: https://stackoverflow.com/questions/57746321/implicit-conversion-warning-int-to-int-lookalike
digits[i] = static_cast<uint8_t>(input_string[input_string.length() - i - 1] - '0');
// End
}
}
/**
* @brief Returns a number that is the negation of current bigint object.
*
* @return bigint A copy to a new bigint object that is a negation of the current number.
*/
bigint bigint::operator-() const
{
bigint out = *this;
if (out.number_sign == sign::negative)
out.number_sign = sign::positive;
else if (out.number_sign == sign::positive)
out.number_sign = sign::negative;
return out;
}
/**
* @brief Inserts a bigint object into an output stream.
*
* @param out An output stream.
* @param number The bigint number to be inserted into the output stream.
* @return ostream& A reference to the output stream.
*/
ostream &operator<<(ostream &out, const bigint &number)
{
if (number.number_sign == sign::zero)
{
out << '0';
return out;
}
else if (number.number_sign == sign::negative)
out << '-';
for (size_t i = number.digits.size(); i > 0; i--)
{
// Start: Unsigned 8-bit int is treated as unsigned char, to print it out, it should be casted: https://stackoverflow.com/questions/19562103/uint8-t-cant-be-printed-with-cout
out << unsigned(number.digits[i - 1]);
//
}
return out;
}
/**
* @brief Checks whether current bigint (left-hand side) is equal to another (right-hand side).
*
* @param other The right-hand side bigint operand of the comparison.
* @return true If bigint operands are equal.
* @return false If two bigint operands are not equal.
*/
bool bigint::operator==(const bigint &other) const
{
if ((digits.size() != other.digits.size()) or (number_sign != other.number_sign))
return false;
for (size_t i = 0; i < digits.size(); ++i)
if (digits[i] != other.digits[i])
return false;
return true;
}
/**
* @brief Checks whether two bigint numbers are unequal.
*
* @param lhs The left-hand side bigint operand of the comparison.
* @param rhs The right-hand side bigint operand of the comparison.
* @return true If the current number is equal to `rhs`.
* @return false If the current number is not equal to `rhs`.
*/
bool operator!=(const bigint &lhs, const bigint &rhs)
{
return !(lhs == rhs);
}
/**
* @brief Checks whether current bigint (left-hand side) is less than the other (right-hand side).
*
* @param other The right-hand side bigint operand of the comparison.
* @return true If left-hand side is less than `rhs`;
* @return false If current number is greater than or equal to `rhs`.
*/
bool bigint::operator<(const bigint &other) const
{
if (*this == other)
return false;
switch (number_sign)
{
case sign::negative:
if (other.number_sign == sign::negative)
{
if (digits.size() > other.digits.size())
return true;
else if (digits.size() < other.digits.size())
return false;
else
{
for (size_t i = digits.size(); i > 0; i--)
{
if (digits[i - 1] > other.digits[i - 1])
return true;
else if (digits[i - 1] < other.digits[i - 1])
return false;
}
return false;
}
}
else
return true;
break;
case sign::zero:
if (other.number_sign == sign::positive)
return true;
else
return false;
break;
case sign::positive:
if (other.number_sign == sign::positive)
{
if (digits.size() > other.digits.size())
return false;
else if (digits.size() < other.digits.size())
return true;
else
{
for (size_t i = digits.size(); i > 0; i--)
{
if (digits[i - 1] < other.digits[i - 1])
return true;
else if (digits[i - 1] > other.digits[i - 1])
return false;
}
return false;
}
}
else
return false;
break;
}
return false;
}
/**
* @brief Checks whether left-hand side is greater than the right-hand side.
*
* @param lhs The left-hand side bigint operand of the comparison.
* @param rhs The right-hand side bigint operand of the comparison.
* @return true If `lhs` is greater than `rhs`.
* @return false If `lhs` is less than or equal to `rhs`.
*/
bool operator>(const bigint &lhs, const bigint &rhs)
{
return !((lhs < rhs) or (lhs == rhs));
}
/**
* @brief Checks whether left-hand side is greater than or equal to the right-hand side.
*
* @param lhs The left-hand side bigint operand of the comparison.
* @param rhs The right-hand side bigint operand of the comparison.
* @return true If `lhs` is greater than or equal to `rhs`.
* @return false If `lhs` is less than `rhs`.
*/
bool operator>=(const bigint &lhs, const bigint &rhs)
{
return !(lhs < rhs);
}
/**
* @brief Checks whether left-hand side is less than or equal to the right-hand side.
*
* @param lhs The left-hand side bigint operand of the comparison.
* @param rhs The right-hand side bigint operand of the comparison.
* @return true If `lhs` is less than or equal to rhs.
* @return false If `lhs` is greater than `rhs`.
*/
bool operator<=(const bigint &lhs, const bigint &rhs)
{
return !(lhs > rhs);
}
/**
* @brief Assigns the value of another bigint number to the current number.
*
* @param other The value to be assigned to the current number.
* @return bigint& A reference to the modified bigint number.
*/
bigint &bigint::operator=(const bigint &other)
{
// Start: Check self-assignment. Source: https://en.cppreference.com/w/cpp/language/copy_assignment
if (this != &other)
// End
{
digits = other.digits;
number_sign = other.number_sign;
}
return *this;
}
/**
* @brief Adds the current bigint number to the other number.
*
* @param other The other bigint number (summand).
* @return bigint& A reference to the current object after summation.
*/
bigint &bigint::operator+=(const bigint &other)
{
if (other.number_sign == sign::zero)
return *this;
else if (number_sign == sign::zero)
*this = other;
else if (number_sign == other.number_sign)
{
size_t max_length = max(digits.size(), other.digits.size());
uint8_t carry = 0;
for (size_t i = 0; i < max_length; i++)
{
if (i >= digits.size())
{
uint8_t temp = other.digits[i] + carry;
carry = temp / 10;
digits.push_back(temp % 10);
}
else if (i >= other.digits.size())
{
uint8_t temp = digits[i] + carry;
carry = temp / 10;
digits[i] = temp % 10;
}
else
{
// Start: Cast from int to uint8_t source: https://stackoverflow.com/questions/57746321/implicit-conversion-warning-int-to-int-lookalike
uint8_t temp = static_cast<uint8_t>(digits[i] + other.digits[i] + carry);
// End
carry = temp / 10;
digits[i] = temp % 10;
}
}
if (carry > 0)
digits.push_back(carry);
}
else if (number_sign == sign::positive and other.number_sign == sign::negative)
{
*this -= -other;
}
else if (number_sign == sign::negative and other.number_sign == sign::positive)
{
bigint temp = other;
temp -= -*this;
*this = temp;
}
return *this;
}
/**
* @brief Adds two bigint numbers.
*
* @param lhs The left-hand side operand (summand).
* @param rhs The right-hand side operand (summand).
* @return bigint A copy of the result after subtraction.
*/
bigint operator+(bigint lhs, const bigint &rhs)
{
lhs += rhs;
return lhs;
}
/**
* @brief Compares the absolute value of current bigint number with another.
*
* @param other The other bigint number, the right-hand side of the comparison.
* @return true If the absolute value of the current number is greater than the other.
* @return false If the If the absolute value of the current number is less than the other.
*/
bool bigint::is_abs_greater(const bigint &other) const
{
if (digits.size() != other.digits.size())
return digits.size() > other.digits.size();
else
{
for (size_t i = digits.size(); i > 0; i--)
{
if (digits[i - 1] > other.digits[i - 1])
return true;
else if (digits[i - 1] < other.digits[i - 1])
return false;
}
return false;
}
}
/**
* @brief Removes leading zeros of a bigint object. It will iterate the digits starting from the most significant digit, and remove any leading zeros in the start, if available.
*
*/
void bigint::zero_remover()
{
if (number_sign == sign::zero)
return;
while (digits[digits.size() - 1] == 0)
digits.pop_back();
}
/**
* @brief Subtracts the other bigint number from the current bigint number.
*
* @param other The other bigint number (subtrahend).
* @return bigint& A reference to the current object after subtraction.
*/
bigint &bigint::operator-=(const bigint &other)
{
if (other.number_sign == sign::zero)
return *this;
else if (number_sign == sign::zero)
*this = -other;
else if (*this == other)
*this = bigint();
else if (number_sign == other.number_sign)
{
if (this->is_abs_greater(other))
{
uint8_t borrow = 0;
for (size_t i = 0; i < digits.size(); i++)
{
int16_t temp = digits[i];
if (borrow > 0)
{
temp -= 1;
if (temp < 0)
{
temp += 10;
borrow = 1;
}
else
borrow = 0;
}
if (i < other.digits.size())
temp -= other.digits[i];
if (temp < 0)
{
temp += 10;
borrow = 1;
}
// Start: Cast from int to uint8_t source: https://stackoverflow.com/questions/57746321/implicit-conversion-warning-int-to-int-lookalike
digits[i] = static_cast<uint8_t>(temp);
// End
}
this->zero_remover();
return *this;
}
else if (!this->is_abs_greater(other))
{
bigint _other = other;
uint8_t borrow = 0;
for (size_t i = 0; i < _other.digits.size(); i++)
{
int16_t temp = _other.digits[i];
if (borrow > 0)
{
temp -= 1;
if (temp < 0)
{
temp += 10;
borrow = 1;
}
else
borrow = 0;
}
if (i < digits.size())
temp -= digits[i];
if (temp < 0)
{
temp += 10;
borrow = 1;
}
// Start: Cast from int to uint8_t source: https://stackoverflow.com/questions/57746321/implicit-conversion-warning-int-to-int-lookalike
_other.digits[i] = static_cast<uint8_t>(temp);
// End
}
_other.zero_remover();
if (number_sign == sign::positive)
number_sign = sign::negative;
else
number_sign = sign::positive;
digits = _other.digits;
return *this;
}
}
else if (number_sign == sign::positive and other.number_sign == sign::negative)
{
*this += -other;
}
else if (number_sign == sign::negative and other.number_sign == sign::positive)
{
bigint temp = other;
temp += -*this;
*this = -temp;
}
return *this;
}
/**
* @brief Subtracts two bigint numbers.
*
* @param lhs The left-hand side operand (minuend).
* @param rhs The right-hand side operand (subtrahend).
* @return bigint A copy of the result after subtraction.
*/
bigint operator-(bigint lhs, const bigint &rhs)
{
lhs -= rhs;
return lhs;
}
/**
* @brief Multiplies the current bigint number by another and stores the result in the current object.
*
* @param other The other bigint number (multiplicand).
* @return bigint& A reference to the current object after multiplication.
*/
bigint &bigint::operator*=(const bigint &other)
{
if (number_sign == sign::zero or other.number_sign == sign::zero)
{
number_sign = sign::zero;
digits = {0};
return *this;
}
if (number_sign == other.number_sign)
number_sign = sign::positive;
else
number_sign = sign::negative;
if (digits.size() == 1 and digits[0] == 1)
{
digits = other.digits;
return *this;
}
else if (other.digits.size() == 1 and other.digits[0] == 1)
return *this;
if (digits.size() >= other.digits.size())
{
bigint result;
for (size_t i = 0; i < other.digits.size(); i++)
{
uint8_t carry = 0;
bigint temp;
temp.digits.resize(i, 0);
for (size_t j = 0; j < digits.size(); j++)
{
// Start: Cast from int to uint8_t source: https://stackoverflow.com/questions/57746321/implicit-conversion-warning-int-to-int-lookalike
uint8_t multiply = static_cast<uint8_t>(digits[j] * other.digits[i] + carry);
// End
carry = multiply / 10;
temp.digits.push_back(multiply % 10);
}
if (carry > 0)
temp.digits.push_back(carry);
temp.number_sign = sign::positive;
result += temp;
}
digits = result.digits;
return *this;
}
else
{
bigint result;
for (size_t i = 0; i < digits.size(); i++)
{
uint8_t carry = 0;
bigint temp;
temp.digits.resize(i, 0);
for (size_t j = 0; j < other.digits.size(); j++)
{
// Start: Cast from int to uint8_t source: https://stackoverflow.com/questions/57746321/implicit-conversion-warning-int-to-int-lookalike
uint8_t multiply = static_cast<uint8_t>(digits[i] * other.digits[j] + carry);
// End
carry = multiply / 10;
temp.digits.push_back(multiply % 10);
}
if (carry > 0)
temp.digits.push_back(carry);
temp.number_sign = sign::positive;
result += temp;
}
digits = result.digits;
return *this;
}
}
/**
* @brief Multiplies two bigint numbers.
*
* @param lhs The left-hand side operand (multiplier).
* @param rhs The right-hand side operand (multiplicand).
* @return bigint A copy of the result after multiplication.
*/
bigint operator*(bigint lhs, const bigint &rhs)
{
lhs *= rhs;
return lhs;
}