-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigInteger.cpp
287 lines (274 loc) · 7.92 KB
/
BigInteger.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
#include "BigInteger.h"
BigInteger::BigInteger(Sign const & new_sign, uint16_t new_nod, std::string_view new_digits) noexcept
: sign(new_sign), number_of_digits(new_nod)
{
for (size_t i = 0; i < new_nod; ++i)
{
digits.at(new_nod - i - 1) = static_cast<int8_t>(new_digits.at(i) - '0');
}
}
BigInteger::BigInteger() noexcept
{
memset(digits.data(), 0, DIM_MAX);
}
auto operator<<(std::ostream & stream, BigInteger const & big_integer) noexcept -> std::ostream &
{
if (big_integer.sign == BigInteger::Sign::MINUS)
{
stream << '-';
}
for (int i = big_integer.number_of_digits - 1; i >= 0; --i)
{
stream << +big_integer.digits.at(static_cast<size_t>(i));
}
return stream;
}
auto BigInteger::operator+(BigInteger big_integer) noexcept -> BigInteger
{
if (sign == big_integer.sign)
{
BigInteger sum {};
int8_t carry {0};
uint16_t max_length = std::max(number_of_digits, big_integer.number_of_digits);
for (size_t i = 0; i < max_length; ++i)
{
sum.digits.at(i) = static_cast<int8_t>(digits.at(i) + big_integer.digits.at(i) + carry);
carry = static_cast<int8_t>(sum.digits.at(i) / BASE_TEN);
sum.digits.at(i) %= BASE_TEN;
}
if (carry != 0)
{
sum.digits.at(max_length++) = carry;
}
sum.number_of_digits = max_length;
sum.sign = sign;
return sum;
}
if (sign == BigInteger::Sign::PLUS && big_integer.sign == BigInteger::Sign::MINUS)
{
big_integer.sign = BigInteger::Sign::PLUS;
return *this - big_integer;
}
BigInteger aux = *this;
aux.sign = BigInteger::Sign::PLUS;
return big_integer - aux;
}
auto BigInteger::operator-(BigInteger big_integer) noexcept -> BigInteger
{
if (sign == BigInteger::Sign::MINUS && big_integer.sign == BigInteger::Sign::PLUS)
{
big_integer.sign = BigInteger::Sign::MINUS;
return *this + big_integer;
}
if (sign == BigInteger::Sign::PLUS && big_integer.sign == BigInteger::Sign::MINUS)
{
big_integer.sign = BigInteger::Sign::PLUS;
return *this + big_integer;
}
BigInteger difference;
BigInteger first;
BigInteger second;
if (*this > big_integer)
{
first = *this;
second = big_integer;
if (first.sign == BigInteger::Sign::MINUS && second.sign == BigInteger::Sign::MINUS)
{
difference.sign = BigInteger::Sign::MINUS;
}
else
{
difference.sign = BigInteger::Sign::PLUS;
}
}
else if (*this < big_integer)
{
first = big_integer;
second = *this;
if (first.sign == BigInteger::Sign::MINUS && second.sign == BigInteger::Sign::MINUS)
{
difference.sign = BigInteger::Sign::PLUS;
}
else
{
difference.sign = BigInteger::Sign::MINUS;
}
}
else
{
return {BigInteger::Sign::PLUS, 1, "0"};
}
difference.number_of_digits = first.number_of_digits;
int8_t carry {0};
size_t idx {0};
for (idx = 0; idx < difference.number_of_digits; ++idx)
{
difference.digits.at(idx) = static_cast<int8_t>(first.digits.at(idx) - second.digits.at(idx) + carry);
if (difference.digits.at(idx) < 0)
{
difference.digits.at(idx) += BASE_TEN;
carry = -1;
}
else
{
carry = 0;
}
}
idx = difference.number_of_digits - 1;
while (difference.digits.at(idx) == 0)
{
--idx;
}
difference.number_of_digits = static_cast<uint16_t>(idx + 1);
return difference;
}
auto BigInteger::operator*(BigInteger big_integer) noexcept -> BigInteger
{
if ((digits[0] == 0 && number_of_digits == 1) || (big_integer.digits[0] == 0 && big_integer.number_of_digits == 1))
{
return {BigInteger::Sign::PLUS, 1, "0"};
}
BigInteger product;
int8_t carry = 0;
size_t idx1 {0};
size_t idx2 {0};
for (idx1 = 0; idx1 < number_of_digits; ++idx1)
{
carry = 0;
for (idx2 = 0; idx2 < big_integer.number_of_digits; ++idx2)
{
product.digits.at(idx1 + idx2) += digits.at(idx1) * big_integer.digits.at(idx2) + carry;
carry = static_cast<int8_t>(product.digits.at(idx1 + idx2) / BASE_TEN);
product.digits.at(idx1 + idx2) %= BASE_TEN;
}
if (carry != 0)
{
product.digits.at(idx1 + idx2) = carry;
}
}
product.number_of_digits = number_of_digits + big_integer.number_of_digits;
if (product.digits.at(product.number_of_digits - 1) == 0)
{
--product.number_of_digits;
}
if (sign == big_integer.sign)
{
product.sign = BigInteger::Sign::PLUS;
}
else
{
product.sign = BigInteger::Sign::MINUS;
}
return product;
}
auto BigInteger::operator/(BigInteger big_integer) -> BigInteger
{
if (big_integer.digits[0] == 0 && big_integer.number_of_digits == 1)
{
throw std::invalid_argument("Division by 0!\n");
}
if (*this < big_integer)
{
return {BigInteger::Sign::PLUS, 1, "0"};
}
if (*this == big_integer)
{
if (sign == big_integer.sign)
{
return {BigInteger::Sign::PLUS, 1, "1"};
}
return {BigInteger::Sign::MINUS, 1, "1"};
}
BigInteger quotient;
BigInteger remainder;
if (sign == big_integer.sign)
{
quotient.sign = BigInteger::Sign::PLUS;
}
else
{
quotient.sign = BigInteger::Sign::MINUS;
}
big_integer.sign = BigInteger::Sign::PLUS;
quotient.number_of_digits = number_of_digits;
remainder.number_of_digits = 0;
for (int i = number_of_digits; i >= 0; --i)
{
for (int j = number_of_digits - 1; j >= 0; --j)
{
remainder.digits.at(static_cast<size_t>(j + 1)) = remainder.digits.at(static_cast<size_t>(j));
}
++remainder.number_of_digits;
remainder.digits.at(0) = digits.at(static_cast<size_t>(i));
quotient.digits.at(static_cast<size_t>(i)) = 0;
while (remainder >= big_integer)
{
++quotient.digits.at(static_cast<size_t>(i));
remainder = remainder - big_integer;
}
}
while (quotient.digits.at(quotient.number_of_digits - 1) == 0 && quotient.number_of_digits > 1)
{
--quotient.number_of_digits;
}
return quotient;
}
auto BigInteger::operator==(BigInteger const & big_integer) const noexcept -> bool
{
uint16_t nod = number_of_digits;
// Remove leading zeroes if necessary
while (digits.at(nod - 1) == 0 && nod > 1)
{
--nod;
}
for (int i = nod - 1; i >= 0; --i)
{
if (digits.at(static_cast<size_t>(i)) != big_integer.digits.at(static_cast<size_t>(i)))
{
return false;
}
}
return true;
}
auto BigInteger::operator<(BigInteger const & big_integer) const noexcept -> bool
{
uint16_t nod_a = number_of_digits;
uint16_t nod_b = big_integer.number_of_digits;
// Remove leading zeroes if necessary
while (digits.at(nod_a - 1) == 0 && nod_a > 1)
{
--nod_a;
}
while (big_integer.digits.at(nod_b - 1) == 0 && nod_b > 1)
{
--nod_b;
}
if (nod_a < nod_b)
{
return true;
}
if (nod_a > nod_b)
{
return false;
}
for (int i = nod_b - 1; i >= 0; --i)
{
if (digits.at(static_cast<size_t>(i)) < big_integer.digits.at(static_cast<size_t>(i)))
{
return true;
}
if (digits.at(static_cast<size_t>(i)) > big_integer.digits.at(static_cast<size_t>(i)))
{
return false;
}
}
return false;
}
auto BigInteger::operator>(BigInteger const & big_integer) const noexcept -> bool
{
return big_integer < *this;
}
auto BigInteger::operator>=(BigInteger const & big_integer) const noexcept -> bool
{
return !(*this < big_integer);
}