-
Notifications
You must be signed in to change notification settings - Fork 0
/
NaturalNum.h
88 lines (82 loc) · 4.93 KB
/
NaturalNum.h
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
#ifndef NATURAL_NUM_H
#define NATURAL_NUM_H
//////////////////////////////////////////////////////////////////////////
// //
// CLASS NAME: NaturalNum //
// //
// DESCRIPTION: Stores a natural number of any conceivable size. //
// Supports any arithmetic operation provided by C++. //
// Although the number is maintained as little-endian, //
// the code is compatible with both types of processors. //
// //
// REMARKS: An exception is thrown when the result is either //
// negative (X-Y and X<Y) or infinite (X/Y and Y==0). //
// //
// AUTHOR: Barakman ([email protected]) //
// //
//////////////////////////////////////////////////////////////////////////
class NaturalNum
{
public: //Constructors + Destructor
NaturalNum();
virtual ~NaturalNum();
NaturalNum(unsigned int iNaturalNum);
NaturalNum(const NaturalNum& cNaturalNum);
//---------------------------------------------------------------------------------------------
public: //Assignment Operators
virtual const NaturalNum& operator=(const NaturalNum& cNaturalNum);
virtual const NaturalNum& operator+=(const NaturalNum& cNaturalNum);
virtual const NaturalNum& operator-=(const NaturalNum& cNaturalNum);
virtual const NaturalNum& operator*=(const NaturalNum& cNaturalNum);
virtual const NaturalNum& operator/=(const NaturalNum& cNaturalNum);
virtual const NaturalNum& operator%=(const NaturalNum& cNaturalNum);
virtual const NaturalNum& operator|=(const NaturalNum& cNaturalNum);
virtual const NaturalNum& operator&=(const NaturalNum& cNaturalNum);
virtual const NaturalNum& operator^=(const NaturalNum& cNaturalNum);
virtual const NaturalNum& operator<<=(unsigned int iShift);
virtual const NaturalNum& operator>>=(unsigned int iShift);
//---------------------------------------------------------------------------------------------
public: //Unary Operators
virtual NaturalNum operator~() const;
virtual const NaturalNum& operator++();
virtual const NaturalNum& operator--();
virtual NaturalNum operator++(int dummy);
virtual NaturalNum operator--(int dummy);
//---------------------------------------------------------------------------------------------
public: //Binary Operators
friend NaturalNum operator+(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2);
friend NaturalNum operator-(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2);
friend NaturalNum operator*(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2);
friend NaturalNum operator/(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2);
friend NaturalNum operator%(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2);
friend NaturalNum operator|(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2);
friend NaturalNum operator&(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2);
friend NaturalNum operator^(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2);
virtual NaturalNum operator<<(unsigned int iShift) const;
virtual NaturalNum operator>>(unsigned int iShift) const;
//---------------------------------------------------------------------------------------------
public: //Binary Comperators
friend bool operator<(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2);
friend bool operator>(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2);
friend bool operator==(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2);
friend bool operator<=(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2);
friend bool operator>=(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2);
friend bool operator!=(const NaturalNum& cNaturalNum1,const NaturalNum& cNaturalNum2);
//---------------------------------------------------------------------------------------------
public: //Interface Routines
unsigned int BitCount() const;
void ToBuffer(unsigned char aBuffer[]) const;
void ToString(char aString[],unsigned int iBase) const;
friend NaturalNum StringToNaturalNum(const char aString[],unsigned int iBase);
//---------------------------------------------------------------------------------------------
protected: //Internal Routines
void Allocate(unsigned int iBytes);
void DeallocateAll();
void RemoveRedundantZeros();
//---------------------------------------------------------------------------------------------
protected: //Object Attributes
unsigned int m_iLength;
unsigned char* m_aValues;
//---------------------------------------------------------------------------------------------
};
#endif