-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber.cpp
369 lines (343 loc) · 10.9 KB
/
Number.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
#include "Number.hpp"
Number one = Number("1");
Number zero = Number("0");
const char* Number::NotANumberException::what() const throw()
{
return "BAD";
}
const char* Number::NumberTooLongException::what() const throw()
{
return "BAD";
}
const char* Number::DivisionByZeroException::what() const throw()
{
return "BAD";
}
const char* Number::ModuloByZeroException::what() const throw()
{
return "BAD";
}
const char* Number::NegativePowerException::what() const throw()
{
return "BAD";
}
Number::Number() : _value("0")
{
Symbol::_type = VALUE;
}
Number::~Number()
{
}
Number::Number(string const &data) throw(NotANumberException, NumberTooLongException)
{
Symbol::_type = VALUE;
if (data.length() > NUMBER_LIMIT)
throw NumberTooLongException();
if (data[0] != '-' && (data[0] < '0' || data[0] > '9'))
throw NotANumberException();
for (int idx = 1; idx < (int)data.length(); idx++)
if (data[idx] < '0' || data[idx] > '9')
throw NotANumberException();
if (data.compare("-0") == 0)
this->_value = "0";
else
{
int idx = 0;
while(idx < (int)data.length()-1 && data[idx] == '0')
idx++;
this->_value = data.substr(idx);
}
}
Number Number::operator=(Number const &rhs)
{
this->_value = rhs._value;
return *this;
}
bool Number::operator!=(Number const &rhs)
{
return this->_value.compare(rhs._value) != 0;
}
bool Number::operator==(Number const &rhs)
{
if (this->_value.compare("0") == 0 && rhs._value.compare("-0") == 0)
return true;
if (this->_value.compare("-0") == 0 && rhs._value.compare("0") == 0)
return true;
return this->_value.compare(rhs._value) == 0;
}
Number Number::operator+(Number const &rhs)
{
if (this->_value[0] == '-' && rhs._value[0] == '-')
return Number("-1") * (Number(this->_value.substr(1,this->_value.length())) +
Number(rhs._value.substr(1,rhs._value.length())));
else if (this->_value[0] == '-')
return Number(rhs) - Number(this->_value.substr(1,this->_value.length()));
else if (rhs._value[0] == '-')
return *this - Number(rhs._value.substr(1,rhs._value.length()));
else
{
int len = max(this->_value.length(), rhs._value.length());
string sum(len+1,'0');
int i = len;
int j = this->_value.length() - 1;
int k = rhs._value.length() - 1;
int got = 0;
for (int idx = i; idx >= 0; idx--,j--,k--)
{
if (j >= 0)
got += this->_value[j] - '0';
if (k >= 0)
got += rhs._value[k] - '0';
sum[idx] = (char)('0' + got%10);
got /= 10;
}
if (sum[0] == '0')
sum = sum.substr(1, sum.length());
return Number(sum);
}
}
bool Number::operator>=(Number const &rhs)
{
if (this->_value[0] == '-' && rhs._value[0] != '-')
return false;
if (this->_value[0] != '-' && rhs._value[0] == '-')
return true;
if (this->_value.length() > rhs._value.length())
return true;
if (this->_value.length() == rhs._value.length())
{
int idx = 0;
while(idx < (int)this->_value.length() && this->_value[idx] == rhs._value[idx])
idx++;
return (idx == (int)this->_value.length() || this->_value[idx] >= rhs._value[idx]);
}
return false;
}
bool Number::operator<=(Number const &rhs)
{
Number tmp = Number(rhs);
return tmp >= *this;
}
bool Number::operator<(Number const &rhs)
{
Number tmp = Number(rhs);
return tmp > *this;
}
bool Number::operator>(Number const &rhs)
{
if (this->_value[0] == '-' && rhs._value[0] != '-')
return false;
if (this->_value[0] != '-' && rhs._value[0] == '-')
return true;
if (this->_value.length() > rhs._value.length())
return true;
if (this->_value.length() == rhs._value.length())
{
int idx = 0;
while(idx < (int)this->_value.length() && idx < (int)rhs._value.length() && this->_value[idx] == rhs._value[idx])
idx++;
if (idx == (int)this->_value.length())
return false;
if (idx == (int)rhs._value.length())
return true;
return (this->_value[idx] > rhs._value[idx]);
}
return false;
}
Number Number::operator-(Number const &rhs)
{
if (this->_value[0] == '-' && rhs._value[0] == '-')
return Number(rhs._value.substr(1,rhs._value.length())) + *this;
else if (this->_value[0] == '-')
return Number("-1")* (Number(rhs) + Number(this->_value.substr(1, this->_value.length())));
else if (rhs._value[0] == '-')
{
return (*this + Number(rhs._value.substr(1, rhs._value.length())));
}
else
{
if (*this >= rhs)
{
int len = max(this->_value.length(), rhs._value.length());
string diff(len, '0');
int i = len - 1;
int j = this->_value.length() - 1;
int k = rhs._value.length() - 1;
int got = 0;
int carry = 0;
for (int idx = i; idx >= 0; idx--, j--, k--)
{
if (k >= 0)
{
if (this->_value[j] < rhs._value[k] + carry)
{
got = 10 + this->_value[j] - rhs._value[k] - carry;
carry = 1;
}
else
{
got = this->_value[j] - rhs._value[k] - carry;
carry = 0;
}
}
else
{
got = this->_value[j] - '0' - carry;
carry = 0;
if (got < 0)
{
got += 10;
carry =1;
}
}
diff[idx] = (char)(got + '0');
}
trim(diff);
return Number(diff);
}
else
return Number("-1") * (Number(rhs) - *this);
}
}
Number karatsuba(Number a, Number b)
{
if (a.getValue().length() == 1)
{
long long int got = 0;
string digitProduct(b.getValue().length() + 1, '0');
int pos = digitProduct.length() - 1;
for (int j = b.getValue().length() - 1; j >= 0; j--)
{
got += (a.getValue()[0] - '0') * (b.getValue()[j] - '0');
digitProduct[pos] = (char)(got % 10 + '0');
got /= 10;
pos--;
}
if (got != 0)
digitProduct[pos] = (char)(got + '0');
trim(digitProduct);
return Number(digitProduct);
}
else if (b.getValue().length() == 1)
{
long long int got = 0;
string digitProduct(a.getValue().length() + 1, '0');
int pos = digitProduct.length() - 1;
for (int j = a.getValue().length() - 1; j >= 0; j--)
{
got += (b.getValue()[0] - '0') * (a.getValue()[j] - '0');
digitProduct[pos] = (char)(got % 10 + '0');
got /= 10;
pos--;
}
if (got != 0)
digitProduct[pos] = (char)(got + '0');
trim(digitProduct);
return Number(digitProduct);
}
else
{
int minLen = min(a.getValue().length(), b.getValue().length());
int split = minLen/2;
string zeros1 = string(2*split, '0');
string zeros2 = string(split, '0');
string a1,a2,b1,b2;
a1 = a.getValue().substr(0, a.getValue().length() - split);
a2 = a.getValue().substr(a.getValue().length() - split);
b1 = b.getValue().substr(0, b.getValue().length() - split);
b2 = b.getValue().substr(b.getValue().length() - split);
Number z0, z1, z2;
z0 = karatsuba(Number(a2), Number(b2));
z1 = karatsuba(Number(a2) + Number(a1), Number(b2) + Number(b1));
z2 = karatsuba(Number(a1), Number(b1));
Number diff = z1-z2-z0;
assert((diff +z0+z2) == z1);
Number result = Number(Number(z2.getValue()+zeros1) + Number((z1-z2-z0).getValue() + zeros2) + z0);
string tmp = result.getValue();
trim(tmp);
result = Number(tmp);
return result;
}
}
Number Number::operator*(Number const &rhs)
{
if (this->_value[0] == '-' && rhs._value[0] == '-')
return Number(this->_value.substr(1, this->_value.length())) *
Number(rhs._value.substr(1, rhs._value.length()));
else if (this->_value[0] == '-')
return Number("-" + (Number(this->_value.substr(1, this->_value.length())) * rhs)._value);
else if (rhs._value[0] == '-')
return Number("-" + (Number(rhs._value.substr(1, rhs._value.length())) * (*this))._value);
else
return karatsuba(*this, rhs);
}
Number Number::operator/(Number const &rhs) throw(DivisionByZeroException)
{
if (rhs._value == "0")
throw DivisionByZeroException();
if (this->_value[0] == '-' && rhs._value[0] == '-')
return Number(this->_value.substr(1, this->_value.length())) / Number(rhs._value.substr(1, rhs._value.length()));
else if (this->_value[0] == '-')
return Number("-1") * (Number(this->_value.substr(1, this->_value.length())) / rhs);
else if (rhs._value[0] == '-')
return Number("-1") * (*this / Number(rhs._value.substr(1, rhs._value.length())));
else
{
if (rhs._value == "2")
{
Number tmp = *this * Number("5");
if (tmp._value.length() == 1)
return Number("0");
return Number(tmp._value.substr(0,tmp._value.length() -1));
}
else
{
Number low("0");
Number hi = *this;
while (hi >= low)
{
Number tmp = (low + hi) * Number("5");
Number mid = (tmp._value.length() == 1 ? Number("0") : Number(tmp._value.substr(0, tmp._value.length() - 1)));
if (mid * rhs > *this)
hi = mid - Number("1");
else
low = mid + Number("1");
}
return Number(hi);
}
}
}
Number Number::operator%(Number const &rhs) throw(ModuloByZeroException)
{
if (Number(rhs) == Number("0"))
throw ModuloByZeroException();
return *this - (*this / rhs) * rhs;
}
Number Number::operator^(Number power) throw(NegativePowerException)
{
if (Number("0") > power)
throw NegativePowerException();
Number res("1");
Number tmp(*this);
while (power > Number("0"))
{
if ((power._value[power._value.length() - 1] - '0') % 2 == 1)
res = res * tmp;
tmp = tmp * tmp;
power = power / Number("2");
}
return Number(res);
}
Number gcd(Number a, Number b)
{
return b == Number("0") ? a : gcd(b, a % b);
}
string Number::getValue() const
{
return this->_value;
}
ostream& operator<<(ostream &o, Number const &rhs)
{
o << rhs.getValue();
return o;
}