There’s a limit to how large an integer one can represent with C++'s primitive types. For this assignment, we are going to circumvent this limit in a project called BigNums. This will introduce you to a class that uses dynamic memory allocation.
You should start by copying the header file BigNum.h, the stubs file BigNum.cxx, and the TestBigNum.cxx code to your own directory (you may want to create a subdirectory for this project in your 2270 directory). Then read it over. The private (internal) representation of a BigNum keeps track of the digits and the sign, and imposes no limit on the size of the number. The digits array is somewhat analogous to the bag’s item array. We’ll actually store the digits in this array backwards, since it makes the rest of the implementation a bit simpler.
Several of these functions use dynamic memory allocation (new and delete). The first of these functions to write are the four constructors. You can make a BigNum up from scratch (think about what might be a good way to initialize a BigNum), or from a string that the user has typed in representing a decimal number, or from a regular integer number, or from another BigNum. You’ll also need a destructor to free up memory when you’re done using a particular BigNum variable. Finally you’ll also need to overload the assignment operator =, which uses a private helper function called resize(). In Part 1 of this assignment, you will write all the memory managing code:
BigNum(); BigNum(int num); BigNum(const string& strin); BigNum(const BigNum& anotherBigNum); ~BigNum(); BigNum& operator=(const BigNum& anotherBigNum); void resize(size_t n); You’ll also need to write the relational operators: ==, !=, , =, <, and <=. Again, once you have written the code for some of them, you can probably use those to handle the others.
friend bool operator(const BigNum& a, const BigNum& b); friend bool operator=(const BigNum& a, const BigNum& b); friend bool operator<(const BigNum& a, const BigNum& b); friend bool operator<=(const BigNum& a, const BigNum& b); friend bool operator==(const BigNum& a, const BigNum& b); friend bool operator!=(const BigNum& a, const BigNum& b);
You’ll also write input and output operators for BigNums.
friend std::ostream& operator<<(std::ostream& os, const BigNum& bignum); friend std::istream& operator(std::istream& is, BigNum& bignum);
Part 2.
It is true that the arithmetic functions are plentiful here, but the list shouldn’t worry you too much; many of these functions can be written in terms of other ones. The hard part is usually getting addition, subtraction, and multiplication down. You should end up with a set of simple arithmetic functions for BigNums, like +, +=, -, -=, *, *=, ++, and --. For extra credit, we’ll add the factorial function, to get some really big BigNums, and integer division and remainder functions as well. I solved the initial addition and subtraction work by writing two helper functions, called sum() and diff(). If your operator* function is fast, you should program the factorial function for extra credit.
BigNum& operator+=(const BigNum& addend); BigNum& operator-=(const BigNum& subtractand); BigNum& operator*=(const BigNum& multiplicand); BigNum& operator/=(const BigNum& divisor); BigNum& operator%=(const BigNum& divisor); BigNum& operator++(); // overload prefix increment BigNum& operator--(); // overload prefix decrement
friend BigNum operator+(const BigNum& addend); friend BigNum operator-(const BigNum& subtractand); friend BigNum operator*(const BigNum& multiplicand); friend BigNum operator/(const BigNum& divisor); friend BigNum operator%(const BigNum& divisor); friend BigNum factorial();