-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigIntArithmetic.cpp
More file actions
271 lines (231 loc) · 5.76 KB
/
Copy pathBigIntArithmetic.cpp
File metadata and controls
271 lines (231 loc) · 5.76 KB
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
#include "BigInt.h"
#include <stdexcept>
#include <vector>
// Arithmetic operators and magnitude subtraction.
BigInt &BigInt::operator+=(const BigInt &other)
{
if (this->negative != other.negative)
{
const BigInt::MagnitudeComparison leftComparison = this->CompareMagnitude(other);
if (leftComparison == MagnitudeComparison::Lesser)
{
BigInt temp = other;
temp.SubtractMagnitude(*this);
*this = temp;
}
else if (leftComparison == MagnitudeComparison::Equal)
{
this->digits.clear();
this->negative = false;
this->digits.push_back(0);
}
else
{
this->SubtractMagnitude(other);
}
}
else
{
int carry = 0;
if (this->digits.size() < other.digits.size())
{
this->digits.resize(other.digits.size());
}
for (std::size_t i = 0; i < this->digits.size(); i++)
{
int otherDigit = 0;
if (other.digits.size() > i)
{
otherDigit = other.digits.at(i);
}
int sum = this->digits.at(i) + otherDigit + carry;
carry = 0;
if (sum >= 10)
{
carry = sum / 10;
sum = sum % 10;
}
this->digits.at(i) = sum;
}
if (carry != 0)
{
this->digits.push_back(carry);
}
}
return *this;
}
BigInt BigInt::operator+(const BigInt &other) const
{
BigInt result = *this;
result += other;
return result;
}
BigInt &BigInt::operator++()
{
return *this += 1;
}
BigInt BigInt::operator++(int)
{
BigInt result = *this;
++*this;
return result;
}
BigInt &BigInt::operator-=(const BigInt &other)
{
*this += -other;
return *this;
}
BigInt BigInt::operator-(const BigInt &other) const
{
BigInt result = *this;
result -= other;
return result;
}
BigInt BigInt::operator-() const
{
BigInt result = *this;
result.negative = !result.negative;
result.Normalize();
return result;
}
BigInt &BigInt::operator--()
{
return *this -= 1;
}
BigInt BigInt::operator--(int)
{
BigInt result = *this;
--*this;
return result;
}
BigInt &BigInt::operator*=(const BigInt &other)
{
if (this->IsZero() || other.IsZero())
{
this->digits.clear();
this->digits.push_back(0);
this->negative = false;
return *this;
}
BigInt result;
result.digits.resize(this->digits.size() + other.digits.size());
result.negative = this->negative != other.negative;
for (std::size_t i = 0; i < this->digits.size(); i++)
{
int carry = 0;
for (std::size_t j = 0; j < other.digits.size(); j++)
{
const int product = this->digits.at(i) * other.digits.at(j);
const int sum = result.digits.at(i + j) + product + carry;
result.digits.at(i + j) = sum % 10;
carry = sum / 10;
}
result.digits.at(i + other.digits.size()) = carry;
}
result.Normalize();
*this = result;
return *this;
}
BigInt BigInt::operator*(const BigInt &other) const
{
BigInt result = *this;
result *= other;
return result;
}
BigInt &BigInt::operator/=(const BigInt &other)
{
if (other.IsZero())
{
throw std::invalid_argument("BigInt: input del divisore non valido");
}
const bool resultNegative = this->negative != other.negative;
BigInt leftNumber = *this;
BigInt rightNumber = other;
leftNumber.negative = false;
rightNumber.negative = false;
BigInt remainder;
std::vector<int> quotientDigits;
for (std::size_t i = leftNumber.digits.size(); i > 0; --i)
{
const std::size_t index = i - 1;
int quotientDigit = 0;
remainder = remainder * 10 + leftNumber.digits.at(index);
while (remainder >= rightNumber)
{
remainder -= rightNumber;
++quotientDigit;
}
quotientDigits.push_back(quotientDigit);
}
this->digits.clear();
for (std::size_t i = quotientDigits.size(); i > 0; --i)
{
this->digits.push_back(quotientDigits.at(i - 1));
}
this->negative = resultNegative;
this->Normalize();
return *this;
}
BigInt BigInt::operator/(const BigInt &other) const
{
BigInt result = *this;
result /= other;
return result;
}
BigInt &BigInt::operator%=(const BigInt &other)
{
BigInt resultDivision = *this;
resultDivision /= other;
*this = *this - (resultDivision * other);
return *this;
}
BigInt BigInt::operator%(const BigInt &other) const
{
BigInt result = *this;
result %= other;
return result;
}
BigInt pow(const BigInt &base, int exponent)
{
return pow(base, BigInt(exponent));
}
// Exponentiation by squaring avoids a linear number of multiplications.
BigInt pow(const BigInt &base, BigInt exponent)
{
if (exponent < 0)
{
throw std::invalid_argument("BigInt: input all'esponente non valido");
}
BigInt result = 1;
BigInt tempBase = base;
while (exponent > 0)
{
if (exponent % 2 != 0)
{
result *= tempBase;
}
tempBase *= tempBase;
exponent /= 2;
}
return result;
}
void BigInt::SubtractMagnitude(const BigInt &other)
{
int borrow = 0;
for (std::size_t i = 0; i < digits.size(); i++)
{
const int rightDigit = other.digits.size() > i ? other.digits.at(i) : 0;
int leftDigit = this->digits.at(i) - borrow;
if (leftDigit < rightDigit)
{
leftDigit += 10;
borrow = 1;
}
else
{
borrow = 0;
}
this->digits.at(i) = leftDigit - rightDigit;
}
this->Normalize();
}