大数运算

自己写的大数运算,目前只写了加减乘三个操作。负数、小数、除法还在研究当中。。。

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
370
371
372
373
374
375
376
377
378
379
380

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#define DEFAULT_BUFF_SIZE 3000
#define SECURITY_BUFF_SIZE 8

using namespace std;

//Kiritow's General class BigNumber. Written on Nov. 14th, 2015
//BigNumber Build 1
//All rights reserved. Nobody is allowed to copy without permission.
class bign
{
private:
int* data;
int data_max;
public:
bign()
{
try
{
data=new int[DEFAULT_BUFF_SIZE];
data_max=DEFAULT_BUFF_SIZE;
set_empty();
}
catch(...)
{
cerr<<"Class BIGN : "<<(void*)this<<" : Cannot get enough memory"<<endl;
data_max=0;
data=nullptr;
}
}
bign(const bign& inc)
{
data_max=inc.data_max;
try
{
data=new int[data_max];
set_empty();
memcpy(data,inc.data,sizeof(int)*data_max);
}
catch(...)
{
cerr<<"Class BIGN : "<<(void*)this<<" : Cannot get enough memory"<<endl;
data_max=0;
data=nullptr;
}
}
bign(long inc)
{
char buff[32];
try
{
//This is a BIG try module, which is not recommend.
//But here we have to write it like this as we don't want any exceptions thrown in constructor
//and we don't know which type to return.
data=new int[DEFAULT_BUFF_SIZE];
data_max=DEFAULT_BUFF_SIZE;
set_empty();
memset(buff,0,32);
sprintf(buff,"%ld",inc);
int len=strlen(buff);
for(int i=len-1;i>=0;i--)
{
data[len-1-i]=buff[i]-'0';
}
}
catch(...)
{
cerr<<"Class BIGN : "<<(void*)this<<" : Cannot get enough memory"<<endl;
data=nullptr;
data_max=0;
}
}
bign(const char* inc)
{
int len=strlen(inc);
data=nullptr;
if(len>DEFAULT_BUFF_SIZE)
{
data_max=len+SECURITY_BUFF_SIZE;
}
else
{
data_max=DEFAULT_BUFF_SIZE;
}
reset_buff_size(data_max);
if(data!=nullptr)
{
for(int i=len-1; i>=0; i--)
{
data[len-1-i]=inc[i]-'0';
}
}
}
~bign()
{
if(data!=nullptr)
{
delete[] data;
}
}
char* convert_to_str(char* str)
{
int lenx=len();
for(int i=lenx-1;i>=0;i--)
{
str[lenx-i-1]=data[i]+'0';
}
return str;
}
bool can_be_converted_to_long()
{
int thislen=len();
if(thislen<10)
{
return true;
}
else if (thislen==10)
{
bign tmp(2147438647);
return *this<tmp;
}
else
{
return false;
}
}
operator long() throw(const char*)
{
if(can_be_converted_to_long())
{
char buff[32];
memset(buff,0,32);
int length=len();
for(int i=0;i<length;i++)
{
buff[length-i-1]=data[i];
}
long tmp;
sscanf(buff,"%ld",&tmp);
return tmp;
}
else
{
throw "BIGN::operator long int() : cannot convert to type long";
}
}
void set_empty()
{
memset(data,0,sizeof(int)*data_max);
}
int len() const
{
for(int i=data_max-1;i>=0;i--)
{
if(data[i]!=0)
{
return i+1;
}
}
return 0;
}
int reset_buff_size(int NewBuffSize)
{
if(data!=nullptr) delete[] data;
try
{
data=new int[NewBuffSize];
data_max=NewBuffSize;
set_empty();
}
catch(...)
{
data=nullptr;
data_max=0;
cerr<<"Class BIGN : "<<(void*)this<<" : Cannot get enough memory"<<endl;
return -1;
}
return 0;
}
bool operator < (const bign& inc) const
{
if(this==&inc) return false;
int lena=len();
int lenb=inc.len();
if(lena<lenb)
{
return true;
}
else if(lenb>lena)
{
return false;
}
else
{
for(int i=lena-1;i>=0;i--)
{
if(data[i]<inc.data[i])
{
return true;
}
else if(data[i]>inc.data[i])
{
return false;
}
}
return false;
}
}
bool operator > (const bign& inc) const
{
if(this==&inc) return false;
int lena=len();
int lenb=inc.len();
if(lena<lenb)
{
return false;
}
else if(lenb>lena)
{
return true;
}
else
{
for(int i=lena-1;i>=0;i--)
{
if(data[i]<inc.data[i])
{
return false;
}
else if(data[i]>inc.data[i])
{
return true;
}
}
return false;
}
}
bool operator == (const bign& inc) const
{
if(this==&inc) return true;
int lena=len();
int lenb=inc.len();
if(lena!=lenb)
{
return false;
}
else
{
for(int i=lena-1;i>=0;i--)
{
if(data[i]!=inc.data[i])
{
return false;
}
}
return true;
}
}
bool operator >= (const bign& inc) const
{
return (*this>inc)&&(*this==inc);
}
bool operator <= (const bign& inc) const
{
return (*this<inc)&&(*this==inc);
}
bign& operator = (const bign& inc)
{
if(this==&inc) return *this;
if(inc.data_max>data_max)
{
reset_buff_size(inc.data_max);
}
for(int i=0;i<inc.data_max;i++)
{
data[i]=inc.data[i];
}
return *this;
}
friend bign operator + (const bign& a,const bign& b);
friend bign operator - (const bign& a,const bign& b);
friend bign operator * (const bign& a,const bign& b);
};

bign operator + (const bign& a ,const bign& b)
{
bign tmp;
int maxsize=(a.data_max>b.data_max)?(a.data_max):(b.data_max);
if(maxsize>tmp.data_max)
{
tmp.reset_buff_size(maxsize);
}
int maxlen=(a.len()>b.len())?(a.len()):(b.len());
for(int i=0;i<maxlen;i++)
{
tmp.data[i]+=a.data[i]+b.data[i];
if(tmp.data[i]>9)
{
tmp.data[i]-=10;
tmp.data[i+1]++;
}
}
return tmp;
}

bign operator - (const bign& a,const bign& b)
{
bign tmp;
int maxsize=(a.data_max>b.data_max)?(a.data_max):(b.data_max);
if(maxsize>tmp.data_max)
{
tmp.reset_buff_size(maxsize);
}
int maxlen=(a.len()>b.len())?(a.len()):(b.len());
if(a>b)
{
for(int i=0;i<maxlen;i++)
{
tmp.data[i]+=a.data[i]-b.data[i];
if(tmp.data[i]<0)
{
tmp.data[i]+=10;
tmp.data[i+1]--;
}
}
}
else if(a<b)
{
for(int i=0;i<maxlen;i++)
{
tmp.data[i]+=b.data[i]-a.data[i];
if(tmp.data[i]<0)
{
tmp.data[i]+=10;
tmp.data[i+1]--;
}
}
}
return tmp;
}

bign operator * (const bign& a,const bign& b)
{
bign tmp;
int lena=a.len();
int lenb=b.len();
if(tmp.data_max<lena+lenb+1)
{
tmp.reset_buff_size(lena+lenb+1);
}
for(int i=0;i<lena;i++)
{
for(int j=0;j<lenb;j++)
{
tmp.data[i+j]+=a.data[i]*b.data[j];
}
}
for(int i=0;i<tmp.data_max-1;i++)
{
if(tmp.data[i]>9)
{
tmp.data[i+1]+=tmp.data[i]/10;
tmp.data[i]=tmp.data[i]%10;
}
}
return tmp;
}

char buff[3000];
int main()
{
bign a("1234"),b("5678");
a=a+b;
printf("%s\n",a.convert_to_str(buff));
return 0;
}

在写的时候参考了一下string的设计,之前operator都是以成员函数重载的,所以这样的操作

bign a,b;
a+b;

a+b的时候就会直接把结果存入a中,然而并不一定符合本意。
所以就改成了友元函数,并且返回值而不是引用。

文章转自: https://blog.csdn.net/Kiritow/article/details/49851521

文章作者: QUST-Coder
文章链接: http://qustkx.com/2019/08/17/blog6/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 青岛科技大学信息学院科技创新协会