The full code of the class is contained in the article's associated project LargeNumber.zip. Here I present only the interface of the class:
class CLargeNumber
{
public:
CLargeNumber() : m_cSign(1) { m_oNumber.push_back(0); }
CLargeNumber(int iNumber);
CLargeNumber(string const& rostrNumber);
CLargeNumber(char cSign, vector<char> const& rcoNumber) :
m_cSign(cSign), m_oNumber(rcoNumber) {}
protected:
static void Build(unsigned uN, vector<char>& rvN);
static void Build(string const& rostrNumber, vector<char>& rvN);
static void Clean(vector<char>& rvN);
static int Compare(vector<char> const& rcvN1, vector<char> const& rcvN2);
static void Add(vector<char> const& rcvN1, vector<char> const& rcvN2,
vector<char>& rvNRes);
static void Subtract(vector<char> const& rcvN1, vector<char> const& rcvN2,
vector<char>& rvNRes);
static void Multiply(vector<char> const& rcvN, char c, vector<char>& rvNRes);
static void ShiftLeft(vector<char>& rvN, int iLeft);
static void Multiply(vector<char> const& rcvN1, vector<char> const& rcvN2,
vector<char>& rvNRes);
static int Position(vector<char> const& rcvN);
static void Pow10(unsigned uPow, vector<char>& rvNRes);
static void Divide(vector<char> const& rcvN1, vector<char> const& rcvN2,
vector<char>& rvQ, vector<char>& rvR);
public:
string ToString() const;
bool operator==(CLargeNumber const& roLN);
bool operator!=(CLargeNumber const& roLN);
CLargeNumber& operator-();
bool operator<(CLargeNumber const& roLN) const;
bool operator>(CLargeNumber const& roLN) const;
bool operator<=(CLargeNumber const& roLN) const;
bool operator>=(CLargeNumber const& roLN) const;
CLargeNumber operator+(CLargeNumber const& roLN) const;
CLargeNumber operator-(CLargeNumber const& roLN) const;
CLargeNumber operator*(CLargeNumber const& roLN) const;
CLargeNumber operator/(CLargeNumber const& roLN) const;
CLargeNumber operator%(CLargeNumber const& roLN) const;
CLargeNumber& operator+=(CLargeNumber const& roLN);
CLargeNumber& operator-=(CLargeNumber const& roLN);
CLargeNumber& operator*=(CLargeNumber const& roLN);
CLargeNumber& operator/=(CLargeNumber const& roLN);
CLargeNumber& operator%=(CLargeNumber const& roLN);
operator int() const;
CLargeNumber SquareRoot() const;
private:
char m_cSign;
vector<char> m_oNumber;
};
I am using the STL vector<char>
container m_oNumber
to keep the decimal digits of the number. The digits are stored in the order from lowest to highest. The sign of the number is kept in the char m_cSign
field. The operations for positive large numbers are implemented in some auxiliary static functions. I prefered to use static functions because these auxiliary functions are not dependent on the class's field members. The operations for signed large numbers are implemented using extensive operator overloading and are using internally the auxiliary static functions. Some examples of how to use the class:
Addition
CLargeNumber oLN1("1111111434311111");
CLargeNumber oLN2("2222222233422222");
cout << (oLN1+oLN2).ToString() << endl;
Subtraction
CLargeNumber oLN1("12323523664");
CLargeNumber oLN2("325454361234");
cout << (oLN1-oLN2).ToString() << endl;
Multiplication
CLargeNumber oLN1("123456834333466");
CLargeNumber oLN2(1000);
cout << (oLN1*oLN2).ToString() << endl;
Division
This will throw an exception for division by 0
try
{
CLargeNumber oLN1("1234655123667");
CLargeNumber oLN2(500);
cout << (oLN1/oLN2).ToString() << endl;
cout << (oLN1%oLN2).ToString() << endl;
}
catch(exception& roEx)
{
cout << roEx.what() << endl;
}
Square Root
This will throw an exception for negative numbers
try
{
CLargeNumber oLN1("1000000000000000000");
cout << oLN1.SquareRoot().ToString() << endl;
}
catch(exception& roEx)
{
cout << roEx.what() << endl;
}
Factorial
In the testing program I also have implemented a small function for factorial calculation:
CLargeNumber Factorial(int iNumber)
{
CLargeNumber oLN("1");
if(iNumber > 1)
{
for(int i=2; i<=iNumber; i++)
oLN *= i;
}
return oLN;
}
Example
As an example I give the 158 digits of 100!:
9332621544394415268169923885626670049071596826438162146859296389521759999322991
5608941463976156518286253697920827223758251185210916864000000000000000000000000
I hope you will have some fun playing with this class!