C++大数处理

一般我们编程处理数字用4位到8位就够了,但是这样的数字处理是极限值的,一旦超出这个极限值就不能计算了,这就是常见的爆仓问题
那么,如何处理这种情况呢?一种常见的解决办法就是使用大数

本算法标准基于ISO/IEC 14882:2011

原理

例如 这里有两个字符串
std::string s1(“123456789123456”)
std::string s2(“123456789”)

首先,先比较两个字符串的长度,长度小的字符串在前面填充 ‘0’,直到两者长度相同为止
所以 s2 变成了 “000000123456789”
然后,遍历两个字符串,将遍历的每个字符转换成数字相加,此时可以定义个bool变量用来记录相加是否大于10,如果true则下一次运算+1
如果循环结束这个bool变量依然是true那么就往前面再加个’1’
在此之前可以判断两者的第一个字符是否是’-‘,然后再考虑交给减运算或者是最后在前面加个’-‘

首先,先比较两个字符串的长度,长度小的字符串在前面填充 ‘0’,直到两者长度相同为止
所以 s2 变成了 “000000123456789”
减的判断还得多一个两者大小,遍历两个字符串,每一位比较大小,如果一直是相等那么可以判定这两个大数是相等的,否则就返回大或小,根据这个值,最后可得知:

  • s1 > s2 => s1 - s2
  • s1 = s2 => 0
  • s1 < s2 => ‘-‘ + (s2 - s1)

然后,遍历两个字符串,将遍历的每个字符转换成数字相减,此时可以定义个bool变量用来记录相减是否小于0,如果true则下一次运算-1
在此之前可以判断两者的第一个字符是否是’-‘,然后再考虑交给加运算或者是最后在前面加个’-‘

根据小学的十位数乘法
例如:11 x 11
乘法

遍历s1和s2,不过这次是

s1[s1.size() - 1] s2[s1.size() - 1]
s1[s1.size() - 2]
s2[s1.size() - 1]
s1[s1.size() - 3] * s2[s1.size() - 1]

s1[s1.size() - 1] s2[s1.size() - 2]
s1[s1.size() - 2]
s2[s1.size() - 2]
s1[s1.size() - 3] s2[s1.size() - 2]

s1[2]
s2[0]
s1[1] s2[0]
s1[0]
s2[0]

除了对 s2[s1.size() - 1] 相乘不需要加’0’以外,其他都要在后面补’0’
最后两者相加交给加运算即可

其实就是减法

除法

代码

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
class BigInteger {
private:
std::string num{"0"};
public:
BigInteger() = default;
explicit BigInteger(std::string num) : num(std::move(num)) {};
std::string toString() const {
return this -> num;
}
BigInteger operator+(const BigInteger &another) {
std::string result, _num = num, _ano_num = another.num;
bool negative_num = _num[0] == '-';
if (_ano_num[0] == '-') {
return BigInteger(*this - BigInteger(_ano_num.substr(1, _ano_num.size())));
}
if (negative_num) {
_num = _num.substr(1, _num.size());
}
std::string __num = _num;
std::string __ano_num = _ano_num;
unsigned long size = (__num.size() >= __ano_num.size() ? __num.size() : __ano_num.size()) + 1;
for (unsigned long i = 0; i < size; ++i) {
if ((__num.size() <= __ano_num.size() ? __num.size() : __ano_num.size()) < i) {
__num.size() <= __ano_num.size() ? _num.insert(0, "0") : _ano_num.insert(0, "0");
}
}
int les{};
if (negative_num) {
if (*this > another) {
les = -1;
} else if (*this == another) {
les = 0;
} else {
les = 1;
}
}
bool enter = false;
for (std::string::reverse_iterator rev_iter_num = _num.rbegin(), rev_iter_ano_num = _ano_num.rbegin(); rev_iter_num != _num.rend() && rev_iter_ano_num != _ano_num.rend(); ++rev_iter_num, ++rev_iter_ano_num) {
if (!negative_num) {
int value{};
value = (*(rev_iter_num) - '0') + (*(rev_iter_ano_num) - '0');
if (enter) {
++value;
enter = false;
}
if (value >= 10) {
result.insert(0, std::to_string(value - 10));
enter = true;
} else {
result.insert(0, std::to_string(value));
}
} else {
if (les == -1) {
BigInteger _value = BigInteger(_num) - BigInteger(_ano_num);
result = "-" + _value.toString();
} else if (les == 0) {
result = "0";
} else {
BigInteger _result = BigInteger(_ano_num) - BigInteger(_num);
result = _result.toString();
}
}
}
if (enter) result.insert(0, "1");
while (result[0] == '0' && result.size() > 1) {
result = result.substr(1, result.size());
}
return BigInteger(result);
}
BigInteger operator-(const BigInteger &another) {
std::string result, _num = num, _ano_num = another.num;
bool negative_num = _num[0] == '-';
if (_ano_num[0] == '-') {
return *this + (BigInteger(_ano_num.substr(1, _ano_num.size())));
}
if (negative_num) {
_num = _num.substr(1, _num.size());
}
std::string __num = _num;
std::string __ano_num = _ano_num;
unsigned long size = (__num.size() >= __ano_num.size() ? __num.size() : __ano_num.size()) + 1;
for (unsigned long i = 0; i < size; ++i) {
if ((__num.size() <= __ano_num.size() ? __num.size() : __ano_num.size()) < i) {
__num.size() <= __ano_num.size() ? _num.insert(0, "0") : _ano_num.insert(0, "0");
}
}
int les{};
if (*this > another) {
les = -1;
} else if (*this == another) {
les = 0;
} else {
les = 1;
}
bool back = false;
for (std::string::reverse_iterator rev_iter_num = _num.rbegin(), rev_iter_ano_num = _ano_num.rbegin(); rev_iter_num != _num.rend() && rev_iter_ano_num != _ano_num.rend(); ++rev_iter_num, ++rev_iter_ano_num) {
if (!negative_num) {
if (les == -1) {
int value{};
value = (*(rev_iter_num) - '0') - (*(rev_iter_ano_num) - '0');
if (back) {
--value;
back = false;
}
if (value < 0) {
result.insert(0, std::to_string(value + 10));
back = true;
} else {
result.insert(0, std::to_string(value));
}
} else if (les == 0) {
result = "0";
} else {
BigInteger value = BigInteger(_ano_num) - BigInteger(_num);
result = "-" + value.toString();
}
} else {
BigInteger _value = BigInteger(_num) + BigInteger(_ano_num);
result = "-" + _value.toString();
}
}
while (result[0] == '0' && result.size() > 1) {
result = result.substr(1, result.size());
}
return BigInteger(result);
}
BigInteger operator*(const BigInteger &another) {
std::string result, _num = num, _ano_num = another.num;
bool negative_num = _num[0] == '-';
bool negative_ano_num = _ano_num[0] == '-';
bool negative = false;
if (negative_num) {
_num = _num.substr(1, _num.size());
}
if (negative_ano_num) {
_ano_num = _ano_num.substr(1, _ano_num.size());
}
if ((negative_num && !negative_ano_num) || (!negative_num && negative_ano_num)) {
negative = true;
}
_num.insert(0, "0");
_ano_num.insert(0, "0");
std::string _m{};
for (std::string::reverse_iterator rev_ano_iter_num = _ano_num.rbegin(); rev_ano_iter_num != _ano_num.rend(); ++rev_ano_iter_num, _m += "0") {
int enter{};
std::string _m_value{};
for (std::string::reverse_iterator rev_iter_num = _num.rbegin(); rev_iter_num != _num.rend(); ++rev_iter_num) {
int _value = ((*(rev_ano_iter_num) - '0') * (*(rev_iter_num) - '0') + enter);
if (_value >= 10) {
_m_value.insert(0, std::to_string(_value % 10));
enter = _value / 10 % 10;
} else {
_m_value.insert(0, std::to_string(_value));
enter = 0;
}
}
if (!enter) {
_m_value.insert(0, std::to_string(enter));
}
_m_value += _m;
BigInteger _result = BigInteger(_m_value) + BigInteger(result);
result = _result.toString();
}
if (negative) {
result = '-' + result;
}
return BigInteger(result);
}
BigInteger operator/(const BigInteger &another) {
std::string result, _num = num, _ano_num = another.num;
bool negative_num = _num[0] == '-';
bool negative_ano_num = _ano_num[0] == '-';
bool negative = false;
if (negative_num) {
_num = _num.substr(1, _num.size());
}
if (negative_ano_num) {
_ano_num = _ano_num.substr(1, _ano_num.size());
}
if ((negative_num && !negative_ano_num) || (!negative_num && negative_ano_num)) {
negative = true;
}
int les{};
if (*this > another) {
les = -1;
} else if (*this == another) {
les = 0;
} else {
les = 1;
}
if (les == -1) {
long _size{(long)(_num.size() - _ano_num.size())};
for (unsigned long i = 0; i < _size; ++i) {
_ano_num += "0";
}
BigInteger _count{"0"};
while (_size >= 0) {
bool __big = false;
unsigned long __size = (_num.size() >= _ano_num.size() ? _num.size() : _ano_num.size()) + 1;
for (unsigned long i = 0; i < __size; ++i) {
if ((_num.size() <= _ano_num.size() ? _num.size() : _ano_num.size()) < i) {
_num.size() <= _ano_num.size() ? _num.insert(0, "0") : _ano_num.insert(0, "0");
}
}
if (BigInteger(_num) >= BigInteger(_ano_num)) {
__big = true;
} else {
__big = false;
--_size;
_ano_num = _ano_num.substr(0, _ano_num.size() - 1);
}
if (__big) {
_num = (BigInteger(_num) - BigInteger(_ano_num)).toString();
BigInteger _zero{"1"};
for (unsigned long __tmp = 0; __tmp < _size; ++__tmp) {
_zero = BigInteger(_zero) * BigInteger("10");
}
_count = BigInteger(_count) + _zero;
}
}
result = _count.toString();
} else if (les == 0) {
result = "1";
} else {
return BigInteger("0");
}
if (negative) result.insert(0, "-");
return BigInteger(result);
}
bool operator<(const BigInteger &another) {
bool _b = false;
std::string _num = num, _ano_num = another.num;
unsigned long _size = (_num.size() >= _ano_num.size() ? _num.size() : _ano_num.size()) + 1;
for (unsigned long i = 0; i < _size; ++i) {
if ((_num.size() <= _ano_num.size() ? _num.size() : _ano_num.size()) < i) {
_num.size() <= _ano_num.size() ? _num.insert(0, "0") : _ano_num.insert(0, "0");
}
}
for (std::string::iterator iter_num = _num.begin(), iter_ano_num = _ano_num.begin(); iter_num != _num.end() && iter_ano_num != _ano_num.end(); ++iter_num, ++iter_ano_num) {
if ((*(iter_num) - '0') > (*(iter_ano_num) - '0')) {
_b = false;
break;
} else if ((*(iter_num) - '0') == (*(iter_ano_num) - '0')) {
_b = false;
} else {
_b = true;
break;
}
}
return _b;
}
bool operator<=(const BigInteger &another) {
bool _b = false;
std::string _num = num, _ano_num = another.num;
unsigned long _size = (_num.size() >= _ano_num.size() ? _num.size() : _ano_num.size()) + 1;
for (unsigned long i = 0; i < _size; ++i) {
if ((_num.size() <= _ano_num.size() ? _num.size() : _ano_num.size()) < i) {
_num.size() <= _ano_num.size() ? _num.insert(0, "0") : _ano_num.insert(0, "0");
}
}
for (std::string::iterator iter_num = _num.begin(), iter_ano_num = _ano_num.begin(); iter_num != _num.end() && iter_ano_num != _ano_num.end(); ++iter_num, ++iter_ano_num) {
if ((*(iter_num) - '0') > (*(iter_ano_num) - '0')) {
_b = false;
break;
} else if ((*(iter_num) - '0') == (*(iter_ano_num) - '0')) {
_b = true;
} else {
_b = true;
break;
}
}
return _b;
}
bool operator==(const BigInteger &another) {
bool _b = false;
std::string _num = num, _ano_num = another.num;
unsigned long _size = (_num.size() >= _ano_num.size() ? _num.size() : _ano_num.size()) + 1;
for (unsigned long i = 0; i < _size; ++i) {
if ((_num.size() <= _ano_num.size() ? _num.size() : _ano_num.size()) < i) {
_num.size() <= _ano_num.size() ? _num.insert(0, "0") : _ano_num.insert(0, "0");
}
}
for (std::string::iterator iter_num = _num.begin(), iter_ano_num = _ano_num.begin(); iter_num != _num.end() && iter_ano_num != _ano_num.end(); ++iter_num, ++iter_ano_num) {
if ((*(iter_num) - '0') > (*(iter_ano_num) - '0')) {
_b = false;
break;
} else if ((*(iter_num) - '0') == (*(iter_ano_num) - '0')) {
_b = true;
} else {
_b = false;
break;
}
}
return _b;
}
bool operator>=(const BigInteger &another) {
bool _b = false;
std::string _num = num, _ano_num = another.num;
unsigned long _size = (_num.size() >= _ano_num.size() ? _num.size() : _ano_num.size()) + 1;
for (unsigned long i = 0; i < _size; ++i) {
if ((_num.size() <= _ano_num.size() ? _num.size() : _ano_num.size()) < i) {
_num.size() <= _ano_num.size() ? _num.insert(0, "0") : _ano_num.insert(0, "0");
}
}
for (std::string::iterator iter_num = _num.begin(), iter_ano_num = _ano_num.begin(); iter_num != _num.end() && iter_ano_num != _ano_num.end(); ++iter_num, ++iter_ano_num) {
if ((*(iter_num) - '0') > (*(iter_ano_num) - '0')) {
_b = true;
break;
} else if ((*(iter_num) - '0') == (*(iter_ano_num) - '0')) {
_b = true;
} else {
_b = false;
break;
}
}
return _b;
}
bool operator>(const BigInteger &another) {
bool _b = false;
std::string _num = num, _ano_num = another.num;
unsigned long _size = (_num.size() >= _ano_num.size() ? _num.size() : _ano_num.size()) + 1;
for (unsigned long i = 0; i < _size; ++i) {
if ((_num.size() <= _ano_num.size() ? _num.size() : _ano_num.size()) < i) {
_num.size() <= _ano_num.size() ? _num.insert(0, "0") : _ano_num.insert(0, "0");
}
}
for (std::string::iterator iter_num = _num.begin(), iter_ano_num = _ano_num.begin(); iter_num != _num.end() && iter_ano_num != _ano_num.end(); ++iter_num, ++iter_ano_num) {
if ((*(iter_num) - '0') > (*(iter_ano_num) - '0')) {
_b = true;
break;
} else if ((*(iter_num) - '0') == (*(iter_ano_num) - '0')) {
_b = false;
} else {
_b = false;
break;
}
}
return _b;
}
};
1
2
3
BigInteger bi1("123456789");  
BigInteger bi2("123456789123456");
std::cout << (b1 + b2).toString() << std::endl;

如有错误 请联系qaralotte@gmail.com
敬请雅正:)