-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigNumber.cpp
More file actions
342 lines (339 loc) · 8.68 KB
/
Copy pathBigNumber.cpp
File metadata and controls
342 lines (339 loc) · 8.68 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
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
#include <iostream>
#include <string>
#include <math.h>
class BigNumber
{
public:
BigNumber(const std::string &str)
{
m_numbetString = str;
m_isNegative = str[0] == '-' ? true : false;
removeHighZeros();
}
BigNumber(){}
BigNumber(long long number)
{
m_numbetString = std::to_string(number);
m_isNegative = number < 0 ? true : false;
}
BigNumber &operator=(const BigNumber&rh)
{
m_numbetString = rh.m_numbetString;
m_isNegative = rh.m_isNegative;
removeHighZeros();
return *this;
}
BigNumber(const BigNumber& rh)
{
*this = rh;
}
BigNumber Add(const BigNumber& number2)const
{
BigNumber bn1 = *this;
BigNumber bn2 = number2;
if (bn1.isNegative() || bn2.isNegative())
{
if (bn2.isNegative() && !bn1.isNegative())
return bn1 - bn2.negate();
else if (bn1.isNegative() && !bn2.isNegative())
return bn2 - bn1.negate();
else
return (bn1.negate()+bn2.negate()).negate();
}
if (bn1 < bn2)
return bn2 + bn1;
bn2.fillZeros(bn1.m_numbetString.size());
std::string result;
int carry = 0;
for (int i = bn1.m_numbetString.size() - 1; i >= 0; --i)
{
int cur_add = bn1[i] + bn2[i] + carry;
carry = cur_add / 10;
result.insert(0, std::to_string(cur_add % 10));
}
return BigNumber(result);
}
BigNumber Subtract(const BigNumber& number2)const
{
BigNumber bn1 = *this;
BigNumber bn2 = number2;
if (bn1.isNegative() || bn2.isNegative())
{
if (bn2.isNegative() && !bn1.isNegative())
return bn1 + bn2.negate();
else if (bn1.isNegative() && !bn2.isNegative())
return bn2 - bn1.negate();
else
return (bn2.negate() - bn1.negate());
}
std::cout <<"bn1 = " << bn1 << " = ";
if (bn1 < bn2)
return (bn2-bn1).negate();
if (bn1 == bn2)
return 0;
bn2.fillZeros(bn1.m_numbetString.size());
std::string result;
int tmp_diff = 0;
for (int i = bn1.m_numbetString.size() - 1; i >= 0; --i)
{
int cur_sub = bn1[i] - bn2[i] + tmp_diff;
if (cur_sub < 0)
{
cur_sub += 10;
tmp_diff = -1;
}else
tmp_diff = 0;
result.insert(0, std::to_string(cur_sub));
}
return BigNumber(result);
}
BigNumber Multiple(const BigNumber& number2) const
{
BigNumber bn1 = *this;
BigNumber bn2 = number2;
if (bn1.isNegative() || bn2.isNegative())
{
if (bn2.isNegative() && !bn1.isNegative())
return (bn1 * bn2.negate()).negate();
else if (bn1.isNegative() && !bn2.isNegative())
return (bn2 * bn1.negate()).negate();
else
return bn1.negate() * bn2.negate();
}
if (bn1 < bn2)
return bn2 * bn1;
if (bn1 == 0 || bn2 == 0)
return 0;
if (bn2 == 1)
return bn1;
bn2.fillZeros(bn1.m_numbetString.size());
std::size_t length = m_numbetString.size();
if (length <= 9) //notation 可能不同的平台会不一样?有些平台可能会溢出
{
unsigned long long oprands1 = bn1.getNumberLongLong();
unsigned long long oprands2 = bn2.getNumberLongLong();
unsigned long long res = oprands1 * oprands2;
return BigNumber(res);
}
size_t len_left = length / 2;
size_t len_right = length - len_left;
BigNumber bn_a = BigNumber(bn1.m_numbetString.substr(0, len_left));
BigNumber bn_b = BigNumber(bn1.m_numbetString.substr(len_left, len_right));
BigNumber bn_c = BigNumber(bn2.m_numbetString.substr(0, len_left));
BigNumber bn_d = BigNumber(bn2.m_numbetString.substr(len_left, len_right));
BigNumber bn_ac = (bn_a * bn_c).powEleven(2 * len_right);
BigNumber bn_ad = (bn_a * bn_d).powEleven(len_right);
BigNumber bn_bc = (bn_b * bn_c).powEleven(len_right);
BigNumber bn_bd = bn_b * bn_d;
return bn_ac + bn_ad + bn_bc + bn_bd;
}
BigNumber Div(const BigNumber& number2)
{
BigNumber bn1 = *this;
BigNumber bn2 = number2;
if (bn2 == 0)
{
std::cerr << "dived by zero\n";
}
if (bn1.isNegative() || bn2.isNegative())
{
if (bn2.isNegative() && !bn1.isNegative())
return (bn1 / bn2.negate()).negate();
else if (bn1.isNegative() && !bn2.isNegative())
return (bn2 / bn1.negate()).negate();
else
return bn1.negate() / bn2.negate();
}
if (bn1 < bn2)
return 0;
if (bn1 == bn2)
return 1;
//bn2.fillZeros(bn1.m_numbetString.size());
int res = 0;
while(bn1 >= bn2)
{
bn1 = bn1 - bn2;
res++;
}
return res;
}
BigNumber Mod(const BigNumber& number2)
{
BigNumber bn1 = *this;
BigNumber bn2 = number2;
if (bn1.isNegative() || bn2.isNegative())
{
if (bn2.isNegative() && !bn1.isNegative())
return (bn1 % bn2.negate()).negate();
else if (bn1.isNegative() && !bn2.isNegative())
return (bn2 % bn1.negate()).negate();
else
return bn1.negate() % bn2.negate();
}
return bn1 - (bn1/bn2)*bn2;
}
BigNumber operator+(const BigNumber &rh)
{
return Add(rh);
}
BigNumber operator-(const BigNumber &rh)
{
return Subtract(rh);
}
BigNumber operator*(const BigNumber &rh)
{
return Multiple(rh);
}
BigNumber operator/(const BigNumber &rh)
{
return Div(rh);
}
BigNumber operator%(const BigNumber &rh)
{
return Mod(rh);
}
bool operator<(const BigNumber& rh)
{
return !(*this == rh) && !(*this > rh);
}
bool operator<=(const BigNumber& rh)
{
return *this < rh || *this == rh;
}
bool operator>=(const BigNumber& rh)
{
return *this > rh || *this == rh;
}
bool operator==(const BigNumber &rh) const
{
return this->m_numbetString == rh.m_numbetString;
}
bool operator>(const BigNumber &b2) {
BigNumber b1 = *this;
if (b1.isNegative() || b2.isNegative()) {
if (b1.isNegative() && b2.isNegative()) {
BigNumber bt = b2;
b1.m_numbetString.erase(0, 1);
bt.m_numbetString.erase(0, 1);
return b1 < bt;
}
else {
return !(b1.isNegative() && !b2.isNegative());
}
}
if (b1 == b2) {
return false;
}
if (b1.m_numbetString.size() > b2.m_numbetString.size()) {
return true;
}
else if (b2.m_numbetString.size() > b1.m_numbetString.size()) {
return false;
}
else {
for (unsigned int i = 0; i < b1.m_numbetString.size(); ++i) {
if (b1[i] == static_cast<unsigned int>(b2.m_numbetString[i] - '0')) {
continue;
}
return b1[i] > static_cast<unsigned int>(b2.m_numbetString[i] - '0');
}
}
return false;
}
bool isNegative() const
{
return m_isNegative;
}
BigNumber negate() {
if (this->m_numbetString[0] == '-') {
this->m_numbetString.erase(0, 1);
}
else {
this->m_numbetString.insert(this->m_numbetString.begin(), '-');
}
m_isNegative = m_isNegative == false ? true : false;
return *this;
}
unsigned int operator[](int index) {
if (this->m_numbetString[index] == '-') {
std::cerr << "Can't get the signal \n";
}
return static_cast<unsigned int>(this->m_numbetString[index] - '0');
}
private:
friend std::ostream &operator<<(std::ostream &os, const BigNumber &rh);
private:
//将str的左边填入0,使总共的位数满足totalLength位
void fillZeros(std::size_t totalLength)
{
std::size_t origLen = m_numbetString.size();
if (totalLength <= origLen) return;
std::size_t diff = totalLength - origLen;
std::string tmp_str = "";
for (std::size_t i = 0; i < diff; ++i)
{
tmp_str += "0";
}
m_numbetString.insert(0,tmp_str);
}
void removeHighZeros()
{
//移除高位多余的0
int index_not_zero;;
if (m_isNegative)
{
//是负数
index_not_zero = m_numbetString.find_first_not_of("0",1);
if (index_not_zero != -1)
m_numbetString = "-" + m_numbetString.substr(index_not_zero,m_numbetString.size() - index_not_zero);
else
{ //全是0
m_numbetString = "0";
m_isNegative = false;
}
}else{
index_not_zero = m_numbetString.find_first_not_of("0",0);
if (index_not_zero != -1)
m_numbetString = m_numbetString.substr(index_not_zero,m_numbetString.size() - index_not_zero);
else
m_numbetString = "0";
}
}
unsigned long long getNumberLongLong()
{
unsigned long long result = 0;
size_t len = m_numbetString.size();
for (int i = 0; i < len; ++i)
{
result = result + operator[](i) * pow(10.0, len - i - 1);
}
return result;
}
//*10^n
BigNumber &powEleven(int n)
{
m_numbetString += std::string(n,'0');
return *this;
}
bool m_isNegative;
std::string m_numbetString;
};
std::ostream &operator<<(std::ostream &os, const BigNumber &rh)
{
size_t size = rh.m_numbetString.size();
for (int i = 0; i < size; ++i)
{
os << rh.m_numbetString[i];
}
return os;
}
int main()
{
BigNumber b1("-999999999819283012749173821680227340273958920347298364826398162012471974837492836482365868162874917204728479832486234926");
BigNumber b2("1");
BigNumber b3 = b2 * b1;
BigNumber b4 = b3 - b1;
std::cout << b3 << std::endl;
std::cout << b4 << std::endl;
return 0;
}