I am trying to implement my own version of BigInteger which can hold an arbitrary big integer. I have posted my BigInteger.h file here and I need your suggestions if there is any better design for this.
1. std::string private member will hold the BigInteger in the form of string.
2. Addition :
a. take each character from right
b. convert it to int.
d. Add both. hold the sum in one variable and carry over in another.
e. convert sum to string and append to the result string.
f. carry over variable to be used in the next set of addition.
g. repeat till the left most end of the string.
3. This is very much a preliminary version. In the future, I also plan to overload "=" operator so that I can assign values directly without using constructors.
I'm a bit skeptical about the 1st point. Is there any better way to implement this other than using strings? Also 2.b and 2.e. Is there any other solution other than converting a string to int, adding and then converting back to string?
#include <iostream>
#include <string>
#include <sstream>
class BigInteger
{
private:
////////////////////////////////////////////////////////////////////////
// The big integer will be stored as a string
// in this private member.
////////////////////////////////////////////////////////////////////////
std::string _number;
public:
////////////////////////////////////////////////////////////////////////
// Default constructor to initialize _number.
// Sets the value of _number to "0".
////////////////////////////////////////////////////////////////////////
// Parameters : none
////////////////////////////////////////////////////////////////////////
BigInteger():_number("0"){}
////////////////////////////////////////////////////////////////////////
// Constructor to initialize _number.
// The integer which represents the big integer
// to be passed as argument. Integer will be converted
// to string and stored in _number.
////////////////////////////////////////////////////////////////////////
// Parameters : integer
////////////////////////////////////////////////////////////////////////
BigInteger(int integer)
{
std::stringstream stream;
stream << integer;
_number = stream.str();
}
////////////////////////////////////////////////////////////////////////
// Constructor to initialize _number.
// The string which represents the integer
// to be passed as argument.
////////////////////////////////////////////////////////////////////////
// Parameters : std::string which represents the integer
////////////////////////////////////////////////////////////////////////
BigInteger(std::string str):_number(str){}
////////////////////////////////////////////////////////////////////////
// Prints the number to console
////////////////////////////////////////////////////////////////////////
void print(){std::cout<<_number<<std::endl;}
////////////////////////////////////////////////////////////////////////
// Overloads "+" operator for addition
////////////////////////////////////////////////////////////////////////
// Returns : BigInteger object after addition
// Parameters : BigInteger object r_value (explicit)
// BigInteger object l_value (implicit)
////////////////////////////////////////////////////////////////////////
BigInteger operator + (const BigInteger &r_val);
////////////////////////////////////////////////////////////////////////
// Overloads "-" operator for subtraction
////////////////////////////////////////////////////////////////////////
// Returns : BigInteger object after subtraction
// Parameters : BigInteger object r_value (explicit)
// BigInteger object l_value (implicit)
////////////////////////////////////////////////////////////////////////
BigInteger operator - (const BigInteger &r_val);
////////////////////////////////////////////////////////////////////////
// Overloads "*" operator for multiplication
////////////////////////////////////////////////////////////////////////
// Returns : BigInteger object after multiplication
// Parameters : BigInteger object r_value (explicit)
// BigInteger object l_value (implicit)
////////////////////////////////////////////////////////////////////////
BigInteger operator * (const BigInteger &r_val);
////////////////////////////////////////////////////////////////////////
// Overloads "/" operator for division
////////////////////////////////////////////////////////////////////////
// Returns : BigInteger object after division
// Parameters : BigInteger object r_value (explicit)
// BigInteger object l_value (implicit)
////////////////////////////////////////////////////////////////////////
BigInteger operator / (const BigInteger &r_val);
////////////////////////////////////////////////////////////////////////
// Overloads "%" operator for modulo
////////////////////////////////////////////////////////////////////////
// Returns : BigInteger object after modulo operation
// Parameters : BigInteger object r_value (explicit)
// BigInteger object l_value (implicit)
////////////////////////////////////////////////////////////////////////
BigInteger operator % (const BigInteger &r_val);
};