/
BigInt.cpp
376 lines (348 loc) · 10.8 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
#include "BigInt.h"
//CONSTRUCTORS
BigInt::BigInt()
:digits(1, 0), sign(Sign::positive) // initialize to one digit: zero
{
}
BigInt::BigInt(int a)
: sign(Sign::positive)
{
if (a < 0) {
sign = Sign::negative;
a *= (-1);
}
if (a == 0) // Handle 0 specifically since it won't be caught by the following loop
digits.push_back(0);
for ( ; a != 0; ) {
int lastDigit = a % 10; // get the least sig. digit
digits.push_back(lastDigit);
a /= 10; // get to next digit
}
}
BigInt::BigInt(std::string s)
: sign(Sign::positive)
{
if (s == "") {
BigInt::BigInt();
return;
}
if (s[0] == '-') { // handle the leading '-' sign
sign = Sign::negative;
s.erase(0, 1); // leave only numbers in the string
}
// need to read number is reverse order since that's how they are stored
for (auto it = s.crbegin(); it != s.crend(); ++it) {
if (isdigit(*it))
digits.push_back((*it) - '0');
}
}
//INTERFACE FUNCTIONS
int BigInt::toInt() const
{
if ((*this) > std::numeric_limits<int>::max() || (*this) < std::numeric_limits<int>::min())
throw std::runtime_error("BigInt is too big, or too small to fit in an integer");
int result = 0;
int currPowerOfTen = 1; // instead of using pow() function from <cmath>
for (int i = 0; i < digits.size(); ++i) {
result += digits[i] * currPowerOfTen;
currPowerOfTen *= 10; // get to the next power of ten
}
if (sign == Sign::negative) // handle the number being negative
result *= (-1);
return result;
}
std::string BigInt::toString() const
{
// requires reverse iterator of digits;
std::string num;
if (sign == Sign::negative)
num += "-";
for (auto it = digits.crbegin(); it != digits.crend(); ++it)
num += ('0' + *it);
return num;
}
int BigInt::digitSum() const
{
int sum = 0;
for (auto it = digits.cbegin(); it != digits.cend(); ++it) {
sum += *it;
}
return sum;
}
BigInt BigInt::abs() const // seems very inefficient, perhaps needs some work
{
BigInt copy = (*this);
copy.sign = Sign::positive;
return copy;
}
//HELPER FUNCTIONS
void BigInt::normalize()
// remove all the leading zeroes, if the whole number == 0, then leave one last
{
for (int i = digits.size() - 1; i > 0; --i) {
if (digits[i] != 0)
return;
else
digits.pop_back();
}
if (digits.size() == 1 && digits[0] == 0) // make sure that zero is "positive" or else comparisons may fail
sign = Sign::positive;
}
void BigInt::addSigDigits(int value)
//add the number as the most significant digits, eg. num is 234, addSigDigits(87) == 87234
//if zero is passed, than add one zero
{
if (value == 0)
digits.push_back(0);
for (; value != 0;) {
digits.push_back(value % 10);
value /= 10;
}
}
void BigInt::borrow(int toThisDig)
// Assumes, that borrowing is possible -- it is caller's responsibility to make sure it is
{
auto thisDig = digits.begin() + toThisDig; // thisDig serves as a 'bookmark' to come back to after borrowing
auto it = thisDig + 1; // it is the actual "borrowing" iterator
for (; (*it) != 0; ++it) {
// NOTHING - loop gets 'it' to first non-zero digit after thisDig
}
(*it) -= 1; // the actual borrowing
for (; it != thisDig; --it) // If the number borrowed from is not next to thisDigit, then need to add 9 to the zeros in between
(*it) += 9;
(*it) += 10; // finally got to the borrowed digit; increase it by 10 (because there will be no further borrowing)
}
//ARITHMETIC OPERATORS
BigInt& BigInt::operator+=(const BigInt& rhs) // Implements the basic "long addition"
{
if (sign != rhs.sign) { // it really is a subtraction -- (a + (-b) == a - b) && ( (-a) + b == b - a )
*this -= ((-1) * rhs);
return *this;
}
if (rhs.size() > this->size()) //if a is bigger than expand the space to fit all the digits
digits.resize(rhs.size());
int carryOver = 0;
for (int i = 0; i < rhs.size(); ++i) {
int sum = carryOver + digits[i] + rhs.digits[i];
digits[i] = sum % 10; // set to the last digit of sum: eg 5 + 7 = 12, so you set digit[i] to 2
carryOver = sum / 10; // carry over the rest of the sum: in case of 12 it is 12/10 == 1 (truncated)
}
// Take care of the leftover carryOver
if (carryOver != 0) {
if (rhs.size() < this->size()) { // *this has got place to fit some carry over from rhs -- no need to expand the vector of digits
for (int i = rhs.size(); (i < this->size()) && (carryOver != 0); ++i) {
int sum = digits[i] + carryOver;
digits[i] = sum % 10;
carryOver = sum / 10;
}
}
if (carryOver != 0) // carry over is still nonzero; need to expand vector of digits
addSigDigits(carryOver);
}
normalize();
return *this;
}
BigInt operator+(const BigInt& lhs, const BigInt& rhs)
// simply use the operator+=
{
BigInt res{ lhs };
res += rhs;
return res;
}
BigInt& BigInt::operator-=(const BigInt& rhs) // Implements the basic "long subratction"
{
if (sign != rhs.sign) { // it really is sum (a - (-b) == a + b && (-a) - b == -(a + b)
*this += ((-1) * rhs);
return *this;
}
if ((this->abs()) < rhs.abs()) { // make sure, that long subraction is possible - if |*this| < |rhs| than borrowing will not be possible at some point
BigInt copyRhs{ rhs };
copyRhs -= (*this);
(*this) = copyRhs;
if (sign == Sign::positive) // a - b == -(b - a)
sign = Sign::negative;
else
sign = Sign::positive; // (-a) - (-b) == (-a) + b == b - a
return *this;
}
for (int i = 0; i < rhs.size(); ++i) { // subtract corresponding digits; if second one is bigger, then borrow
if (digits[i] < rhs.digits[i])
borrow(i);
digits[i] -= rhs.digits[i];
}
normalize(); // delete leading zeros
return (*this);
}
BigInt operator-(const BigInt& lhs, const BigInt& rhs)
// simply use the operator-=
{
BigInt res{ lhs };
res -= rhs;
return res;
}
BigInt& BigInt::operator*=(const BigInt& rhs) // Implements the basic "long multiplication"
{
if (sign != rhs.sign) // If signs are different than result is negative
sign = Sign::negative;
else // If they are the same, than the result is positive
sign = Sign::positive;
BigInt result;
for (int i = 0; i < rhs.size(); ++i) {
// when multiplying you have multiplying number by each digit of the other, and then summing the results
BigInt midSum;
for (int j = 0; j < (i - 1); ++j) {
// when multiplying every next sum starts at 10x bigger than previous
// BigInt() initializes to digits to vector<int>(1, 0) so no need to change the value untill you need two zeros
midSum.addSigDigits(0);
}
int carryOver = 0;
for (int j = 0; j < this->size(); ++j) {
int result = (rhs.digits[i] * digits[j]) + carryOver;
if ((i == 0) && (j == 0)) // BigInt() initializes digits to vector<int>(1, 0)
midSum.digits[0] = result % 10;
else
midSum.addSigDigits((result % 10)); // last digit, in the proper power
carryOver = result / 10;
}
// take care of leftover carryOver
if (carryOver != 0)
midSum.addSigDigits(carryOver);
// add the created midSum to the overall result of the multiplication
result += midSum;
}
result.normalize();
digits = result.digits; // not assigning fully (*this = result) so as to preserve sign information
return *this;
}
BigInt operator*(const BigInt& lhs, const BigInt& rhs)
{
// simply use operator*=
BigInt result{ lhs };
result *= rhs;
return result;
}
//OTHER MATHEMATICAL FUNCTIONS
void BigInt::pow(int n)
{
// NOT IMPLEMENTED
std::cout << "POW NOT IMPLEMENTED!\n";
}
//COMPARISON OPERATORS
bool operator==(const BigInt& lhs, const BigInt& rhs)
{
// different signs == different numbers
if ((lhs.size() != rhs.size()) || (lhs.sign != rhs.sign))
return false;
else
for (int i = lhs.size() - 1; i >= 0; --i) {
if (lhs.digits[i] != rhs.digits[i])
return false;
}
return true; // sign and all digits are the same - numbers are equal
}
bool operator!=(const BigInt& lhs, const BigInt& rhs)
{
return (!(lhs == rhs));
}
bool operator<(const BigInt& lhs, const BigInt& rhs)
{
// if signs are not the same, than the smaller number is the one with negative sign
if (lhs.sign != rhs.sign)
return (!(lhs.sign == BigInt::Sign::positive));
// if they are positive, then small absolute value == smaller number
if (lhs.sign == BigInt::Sign::positive) {
if (lhs.size() != rhs.size())
return (lhs.size() < rhs.size());
for (int i = lhs.size() - 1; i >= 0; --i) {
if (lhs.digits[i] != rhs.digits[i])
return (lhs.digits[i] < rhs.digits[i]);
}
}
// and with negatives the situation is reversed
if (lhs.sign == BigInt::Sign::negative) {
if (lhs.size() != rhs.size())
return (!(lhs.size() < rhs.size()));
for (int i = lhs.size() - 1; i >= 0; --i) {
if (lhs.digits[i] != rhs.digits[i])
return (!(lhs.digits[i] < rhs.digits[i]));
}
}
return false; // numbers are equal; return false
}
bool operator>(const BigInt& lhs, const BigInt& rhs)
{
// essentialy copy-pasted operator<, with some changes of '<' to '>', etc
if (lhs.sign != rhs.sign)
return (lhs.sign == BigInt::Sign::positive);
if (lhs.sign == BigInt::Sign::positive) {
if (lhs.size() != rhs.size())
return (lhs.size() > rhs.size());
for (int i = lhs.size() - 1; i >= 0; --i) {
if (lhs.digits[i] != rhs.digits[i])
return (lhs.digits[i] > rhs.digits[i]);
}
}
if (lhs.sign == BigInt::Sign::negative) {
if (lhs.size() != rhs.size())
return (!(lhs.size() > rhs.size()));
for (int i = lhs.size() - 1; i >= 0; --i) {
if (lhs.digits[i] != rhs.digits[i])
return (!(lhs.digits[i] > rhs.digits[i]));
}
}
return false; // numbers are equal; return false
}
bool operator<=(const BigInt& lhs, const BigInt& rhs)
{
return (!(rhs > lhs));
/* I'll just... keep it here
if (lhs.sign != rhs.sign)
return (!(lhs.sign == BigInt::Sign::positive));
if (lhs.sign == BigInt::Sign::positive) {
if (lhs.size() != rhs.size())
return (lhs.size() < rhs.size());
for (int i = lhs.size() - 1; i >= 0; --i) {
if (lhs.digits[i] != rhs.digits[i])
return (lhs.digits[i] < rhs.digits[i]);
}
}
if (lhs.sign == BigInt::Sign::negative) {
if (lhs.size() != rhs.size())
return (!(lhs.size() < rhs.size()));
for (int i = lhs.size() - 1; i >= 0; --i) {
if (lhs.digits[i] != rhs.digits[i])
return (!(lhs.digits[i] < rhs.digits[i]));
}
}
return true; // numbers are equal; return true*/
}
bool operator>=(const BigInt& lhs, const BigInt& rhs)
{
return (!(lhs < rhs));
/* Seriously... sigh
if (lhs.sign != rhs.sign)
return (lhs.sign == BigInt::Sign::positive);
if (lhs.sign == BigInt::Sign::positive) {
if (lhs.size() != rhs.size())
return (lhs.size() > rhs.size());
for (int i = lhs.size() - 1; i >= 0; --i) {
if (lhs.digits[i] != rhs.digits[i])
return (lhs.digits[i] > rhs.digits[i]);
}
}
if (lhs.sign == BigInt::Sign::negative) {
if (lhs.size() != rhs.size())
return (!(lhs.size() > rhs.size()));
for (int i = lhs.size() - 1; i >= 0; --i) {
if (lhs.digits[i] != rhs.digits[i])
return (!(lhs.digits[i] > rhs.digits[i]));
}
}
return true; // numbers are equal; return true*/
}
//FREE FUNCTIONS
std::ostream& operator<<(std::ostream& os, const BigInt& bi)
{
os << bi.toString(); // use the member function of BigInt
return os;
}