-
Notifications
You must be signed in to change notification settings - Fork 0
/
NaturalNum.cpp
573 lines (405 loc) · 11.1 KB
/
NaturalNum.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#include "NaturalNum.h"
///////////////////////////////////////
// Constructors + Destructor (below) //
NaturalNum::NaturalNum()
{
Allocate(0);
}
NaturalNum::~NaturalNum()
{
DeallocateAll();
}
NaturalNum::NaturalNum(unsigned int iNaturalNum)
{
Allocate(sizeof(iNaturalNum));
for (unsigned int i=0; i<m_iLength; i++)
m_aValues[i]=iNaturalNum>>(i*8);
RemoveRedundantZeros();
}
NaturalNum::NaturalNum(const NaturalNum& cNaturalNum)
{
Allocate(0);
*this=cNaturalNum;
}
// Constructors + Destructor (above) //
///////////////////////////////////////
//////////////////////////////////
// Assignment Operators (below) //
const NaturalNum& NaturalNum::operator=(const NaturalNum& cNaturalNum)
{
if (this==&cNaturalNum)
return *this;
DeallocateAll();
Allocate(cNaturalNum.m_iLength);
for (unsigned int i=0; i<m_iLength; i++)
m_aValues[i]=cNaturalNum.m_aValues[i];
return *this;
}
const NaturalNum& NaturalNum::operator+=(const NaturalNum& cNaturalNum)
{
return *this=*this+cNaturalNum;
}
const NaturalNum& NaturalNum::operator-=(const NaturalNum& cNaturalNum)
{
return *this=*this-cNaturalNum;
}
const NaturalNum& NaturalNum::operator*=(const NaturalNum& cNaturalNum)
{
return *this=*this*cNaturalNum;
}
const NaturalNum& NaturalNum::operator/=(const NaturalNum& cNaturalNum)
{
return *this=*this/cNaturalNum;
}
const NaturalNum& NaturalNum::operator%=(const NaturalNum& cNaturalNum)
{
return *this=*this%cNaturalNum;
}
const NaturalNum& NaturalNum::operator|=(const NaturalNum& cNaturalNum)
{
return *this=*this|cNaturalNum;
}
const NaturalNum& NaturalNum::operator&=(const NaturalNum& cNaturalNum)
{
return *this=*this&cNaturalNum;
}
const NaturalNum& NaturalNum::operator^=(const NaturalNum& cNaturalNum)
{
return *this=*this^cNaturalNum;
}
const NaturalNum& NaturalNum::operator<<=(unsigned int iShift)
{
return *this=*this<<iShift;
}
const NaturalNum& NaturalNum::operator>>=(unsigned int iShift)
{
return *this=*this>>iShift;
}
// Assignment Operators (above) //
//////////////////////////////////
/////////////////////////////
// Unary Operators (below) //
NaturalNum NaturalNum::operator~() const
{
NaturalNum cRes;
cRes.Allocate(m_iLength);
for (unsigned int i=0; i<m_iLength; i++)
cRes.m_aValues[i]=~m_aValues[i];
return cRes;
}
const NaturalNum& NaturalNum::operator++()
{
*this+=1;
return *this;
}
const NaturalNum& NaturalNum::operator--()
{
*this-=1;
return *this;
}
NaturalNum NaturalNum::operator++(int dummy)
{
NaturalNum cRes=*this;
*this+=1;
return cRes;
}
NaturalNum NaturalNum::operator--(int dummy)
{
NaturalNum cRes=*this;
*this-=1;
return cRes;
}
// Unary Operators (above) //
/////////////////////////////
//////////////////////////////
// Binary Operators (below) //
NaturalNum operator+(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2)
{
if (cNaturalNum2.m_iLength<cNaturalNum1.m_iLength)
return cNaturalNum2+cNaturalNum1;
NaturalNum cRes;
cRes.Allocate(cNaturalNum2.m_iLength+1);
unsigned int i;
unsigned short iSum;
unsigned char iRem=0;
for (i=0; i<cNaturalNum1.m_iLength; i++)
{
iSum=iRem+cNaturalNum1.m_aValues[i]+cNaturalNum2.m_aValues[i];
cRes.m_aValues[i]=iSum&0xFF;
iRem=iSum>>8;
}
for (; i<cNaturalNum2.m_iLength; i++)
{
iSum=iRem+cNaturalNum2.m_aValues[i];
cRes.m_aValues[i]=iSum&0xFF;
iRem=iSum>>8;
}
cRes.m_aValues[i]=iRem;
cRes.RemoveRedundantZeros();
return cRes;
}
NaturalNum operator-(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2)
{
if (cNaturalNum2>cNaturalNum1)
throw "Negative Result";
NaturalNum cRes;
cRes.Allocate(cNaturalNum1.m_iLength);
unsigned int i;
signed short iDif;
signed char iRem=0;
for (i=0; i<cNaturalNum2.m_iLength; i++)
{
iDif=iRem+cNaturalNum1.m_aValues[i]-cNaturalNum2.m_aValues[i];
cRes.m_aValues[i]=iDif&0xFF;
iRem=iDif>>8;
}
for (; i<cNaturalNum1.m_iLength; i++)
{
iDif=iRem+cNaturalNum1.m_aValues[i];
cRes.m_aValues[i]=iDif&0xFF;
iRem=iDif>>8;
}
cRes.RemoveRedundantZeros();
return cRes;
}
NaturalNum operator*(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2)
{
NaturalNum cRes=0;
for (unsigned int i=0; i<cNaturalNum1.m_iLength; i++)
for (unsigned int j=0; j<cNaturalNum2.m_iLength; j++)
cRes+=(NaturalNum)(cNaturalNum1.m_aValues[i]*cNaturalNum2.m_aValues[j])<<((i+j)*8);
return cRes;
}
NaturalNum operator/(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2)
{
if (cNaturalNum2==0)
throw "Infinite Result";
NaturalNum cRes=0;
NaturalNum cTmp1=cNaturalNum1;
unsigned int iTmp1Len=cTmp1.BitCount();
unsigned int iNaturalNum2Len=cNaturalNum2.BitCount();
while (iTmp1Len>iNaturalNum2Len)
{
cRes+=(NaturalNum)1<<(iTmp1Len-iNaturalNum2Len-1);
cTmp1-=cNaturalNum2<<(iTmp1Len-iNaturalNum2Len-1);
iTmp1Len=cTmp1.BitCount();
}
if (cTmp1>=cNaturalNum2)
return cRes+1;
return cRes;
}
NaturalNum operator%(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2)
{
return cNaturalNum1-cNaturalNum1/cNaturalNum2*cNaturalNum2;
}
NaturalNum operator|(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2)
{
if (cNaturalNum2.m_iLength<cNaturalNum1.m_iLength)
return cNaturalNum2|cNaturalNum1;
NaturalNum cRes;
cRes.Allocate(cNaturalNum2.m_iLength);
unsigned int i;
for (i=0; i<cNaturalNum1.m_iLength; i++)
cRes.m_aValues[i]=cNaturalNum1.m_aValues[i]|cNaturalNum2.m_aValues[i];
for (; i<cNaturalNum2.m_iLength; i++)
cRes.m_aValues[i]=0|cNaturalNum2.m_aValues[i];
cRes.RemoveRedundantZeros();
return cRes;
}
NaturalNum operator&(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2)
{
if (cNaturalNum2.m_iLength<cNaturalNum1.m_iLength)
return cNaturalNum2&cNaturalNum1;
NaturalNum cRes;
cRes.Allocate(cNaturalNum2.m_iLength);
unsigned int i;
for (i=0; i<cNaturalNum1.m_iLength; i++)
cRes.m_aValues[i]=cNaturalNum1.m_aValues[i]&cNaturalNum2.m_aValues[i];
for (; i<cNaturalNum2.m_iLength; i++)
cRes.m_aValues[i]=0&cNaturalNum2.m_aValues[i];
cRes.RemoveRedundantZeros();
return cRes;
}
NaturalNum operator^(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2)
{
if (cNaturalNum2.m_iLength<cNaturalNum1.m_iLength)
return cNaturalNum2^cNaturalNum1;
NaturalNum cRes;
cRes.Allocate(cNaturalNum2.m_iLength);
unsigned int i;
for (i=0; i<cNaturalNum1.m_iLength; i++)
cRes.m_aValues[i]=cNaturalNum1.m_aValues[i]^cNaturalNum2.m_aValues[i];
for (; i<cNaturalNum2.m_iLength; i++)
cRes.m_aValues[i]=0^cNaturalNum2.m_aValues[i];
cRes.RemoveRedundantZeros();
return cRes;
}
NaturalNum NaturalNum::operator<<(unsigned int iShift) const
{
unsigned int iByteShift=iShift/8;
unsigned int iBitsShift=iShift%8;
if (m_iLength==0)
return 0;
NaturalNum cRes;
cRes.Allocate(m_iLength+iByteShift+1);
for (unsigned int n=0; n<iByteShift; n++)
cRes.m_aValues[n]=0;
unsigned int i;
unsigned char iRem=0;
for (i=iByteShift; i<m_iLength+iByteShift; i++)
{
cRes.m_aValues[i]=(m_aValues[i-iByteShift]<<iBitsShift)|iRem;
iRem=m_aValues[i-iByteShift]>>(8-iBitsShift);
}
cRes.m_aValues[i]=iRem;
cRes.RemoveRedundantZeros();
return cRes;
}
NaturalNum NaturalNum::operator>>(unsigned int iShift) const
{
unsigned int iByteShift=iShift/8;
unsigned int iBitsShift=iShift%8;
if (m_iLength<=iByteShift)
return 0;
NaturalNum cRes;
cRes.Allocate(m_iLength-iByteShift);
unsigned int i;
unsigned char iRem;
for (i=0; i<m_iLength-iByteShift-1; i++)
{
iRem=m_aValues[i+iByteShift+1]<<(8-iBitsShift);
cRes.m_aValues[i]=(m_aValues[i+iByteShift]>>iBitsShift)|iRem;
}
iRem=m_aValues[i+iByteShift]>>iBitsShift;
cRes.m_aValues[i]=iRem;
cRes.RemoveRedundantZeros();
return cRes;
}
// Binary Operators (above) //
//////////////////////////////
////////////////////////////////
// Binary Comperators (below) //
bool operator<(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2)
{
if (cNaturalNum1.m_iLength!=cNaturalNum2.m_iLength)
return cNaturalNum1.m_iLength<cNaturalNum2.m_iLength;
for (unsigned int i=cNaturalNum1.m_iLength; i>0; i--)
if (cNaturalNum1.m_aValues[i-1]!=cNaturalNum2.m_aValues[i-1])
return cNaturalNum1.m_aValues[i-1]<cNaturalNum2.m_aValues[i-1];
return false;
}
bool operator>(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2)
{
if (cNaturalNum1.m_iLength!=cNaturalNum2.m_iLength)
return cNaturalNum1.m_iLength>cNaturalNum2.m_iLength;
for (unsigned int i=cNaturalNum1.m_iLength; i>0; i--)
if (cNaturalNum1.m_aValues[i-1]!=cNaturalNum2.m_aValues[i-1])
return cNaturalNum1.m_aValues[i-1]>cNaturalNum2.m_aValues[i-1];
return false;
}
bool operator==(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2)
{
if (cNaturalNum1.m_iLength!=cNaturalNum2.m_iLength)
return false;
for (unsigned int i=cNaturalNum1.m_iLength; i>0; i--)
if (cNaturalNum1.m_aValues[i-1]!=cNaturalNum2.m_aValues[i-1])
return false;
return true;
}
bool operator<=(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2)
{
return !(cNaturalNum1>cNaturalNum2);
}
bool operator>=(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2)
{
return !(cNaturalNum1<cNaturalNum2);
}
bool operator!=(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2)
{
return !(cNaturalNum1==cNaturalNum2);
}
// Binary Comperators (above) //
////////////////////////////////
////////////////////////////////
// Interface Routines (below) //
unsigned int NaturalNum::BitCount() const
{
if (m_iLength==0)
return 0;
unsigned int iBitCount=(m_iLength-1)*8;
for (unsigned int i=1; i<=m_aValues[m_iLength-1]; i<<=1)
iBitCount++;
return iBitCount;
}
void NaturalNum::ToBuffer(unsigned char aBuffer[]) const
{
for (unsigned int i=0; i<m_iLength; i++)
aBuffer[i]=m_aValues[i];
}
void NaturalNum::ToString(char aString[],unsigned int iBase) const
{
unsigned int iIndex=0;
for (NaturalNum cTmp=*this; cTmp>0; cTmp/=iBase)
{
NaturalNum cDigit=cTmp%iBase;
if (cDigit==0)
aString[iIndex]='0';
else if (cDigit<10)
aString[iIndex]='0'+cDigit.m_aValues[0];
else
aString[iIndex]='A'+cDigit.m_aValues[0]-10;
iIndex++;
}
for (unsigned int i=0; i<iIndex/2; i++)
{
char iDigit=aString[i];
aString[i]=aString[iIndex-1-i];
aString[iIndex-1-i]=iDigit;
}
if (iIndex==0)
aString[iIndex++]='0';
aString[iIndex]=0;
}
NaturalNum StringToNaturalNum(const char aString[],unsigned int iBase)
{
NaturalNum cRes=0;
for (unsigned int i=0; aString[i]!=0; i++)
{
NaturalNum cDigit;
if ('0'<=aString[i] && aString[i]<='9')
cDigit=aString[i]-'0';
else if ('A'<=aString[i] && aString[i]<='Z')
cDigit=aString[i]-'A'+10;
else if ('a'<=aString[i] && aString[i]<='z')
cDigit=aString[i]-'a'+10;
else
break;
cRes*=iBase;
cRes+=cDigit;
}
return cRes;
}
// Interface Routines (above) //
////////////////////////////////
///////////////////////////////
// Internal Routines (below) //
void NaturalNum::Allocate(unsigned int iBytes)
{
m_iLength=iBytes;
if (m_iLength>0)
m_aValues=new unsigned char[m_iLength];
}
void NaturalNum::DeallocateAll()
{
if (m_iLength>0)
delete[] m_aValues;
m_iLength=0;
}
void NaturalNum::RemoveRedundantZeros()
{
while (m_iLength>0 && m_aValues[m_iLength-1]==0)
if (--m_iLength==0)
delete[] m_aValues;
}
// Internal Routines (above) //
///////////////////////////////