(Back to the main page)

Homework 4: Arithmetic Overload (100 pts, due Monday, February 23rd by 11:59pm)

For this homework assignment, you will write a library to enable arithmetic using big numbers -- up to 100 digits long. In the process, you will have to write a class with heavy use of operator overloading, a feature particular to C++.

Part 1: BigInt

Download and view the file bigint.hpp. It is the header file for the class you will have to write. Write the accompanying bigint.cpp file, implementing all of the member functions listed in the header file. We have provided the output operator for you so that you can print integers to the screen, in case you need to debug your code. We have also provided a Makefile that will compile your code. Here are some pointers to help you along the way:

Part 2: Testing

We have provided a sample test program (main.cpp) that implements a few simple tests on the BigInt class. You should add additional tests to ensure that your code works as intended. Your tests must cover every operator at least once, and you should think about ways that combinations of arguments can cause problems. (For example, does your subtraction algorithm deal with a larger number subtracted from a smaller?) There is no standard testing library for C++, so you will have to work without a unit-testing framework. The breadth of the tests is a graded part of this assignment. We will, of course, test your BigInt implementation with our own unit tests.

Challenge (5 extra-credit points)

The design above for BigInt is very wasteful of space -- each element in the digits array takes up 4 bytes (on most systems) but holds a number only between 0 and 9. Instead, update your implementation to use unsigned ints and use their full range of 0 to 4294967295. This is trickier than it seems; you will have a hard time testing if you should carry during addition when your underlying addition overflows internally.

If you complete this challenge, submit both the original, digit-based implementation and your enhanced one.

Deliverables