Home CPSC 340

Assignment 2 - Big Ints

Due: February 22

Objective

To gain experience using pointers, dynamic memory allocation and operator overloading.

Task

In C++, integers have size limits, which are dependent on the machine and compiler. On cinnamon, an integer is 4 bytes. The biggest integer type, long long, is 8 bytes or 64 bits. This type can hold values up to 2^64 - 1, or 18446744073709551615. If we want to store values larger than this, we can't use any built in type to do it.

Your task for this assignment is to write a class, called BigInt that can store non-negative numbers of any size, from 0 all the way up without limit (although, at some point, a computer can run out of memory). Your class will store the digits of the number in a dynamic array, and provide a number of operators to allow users of the class to use it like a built-in int.

The use of this class is demonstrated in this main program which you can use to test your BigInt class. For this program, you aren't writing your own main function, you can simply use the one provided.


Details

  1. The BigInt class should store the number as a dynamic array of digits. Your class must allocate and de-allocate memory correctly, without memory leaks or errors. You also should not allocate more memory than is necessary.
  2. Your class should have a default constructor (one with no parameters) that sets the BigInt to the value of 0.
  3. There must be another constructor that takes an unsigned int as a parameter and sets the BigInt to the value of the parameter.
  4. A third constructor must take a C-String as a parameter and set the BigInt to the value of the string (you can assume that the string contains only digits). This will allow us to create BigInts that won't fit in standard integers such as:
    BigInt large("10000000000000000000000000");
  5. Your class should have a copy constructor, destructor and assignment operator.
  6. In order to print BigInt objects, you must overload the output operator <<. This should print the number itself as if it were an int. Don't output any extra text.
  7. Likewise, your class should overload the input operator >> to allow for inputting BigInt objects. To do this, you can assume the user will enter fewer than 100 digits and will not enter any non-digits. Your operator should initially read in the number as a string and convert it into the BigInt.
  8. You should overload each of the six comparison operators (<, <=, >, >=, ==, !=). Each of these should return a boolean value, and use the usual meaning of comparison for numbers.
  9. You must overload the addition operator, to add two BigInt objects. The operator should return a BigInt object for the sum. You should use the same algorithm that you use to add numbers using pencil and paper - and remember to carry!
  10. All BigInts will be non-negative!

Tips


Extra Credit

For extra credit, also provide an overload of the multiplication operator to multiply two BigInt objects and return the product as a BigInt object.

General Requirements


Submitting

At the top of your program, please include a comment that includes your name, the date and the assignment number. Then submit your program by emailing your code to ifinlay@umw.edu.

Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.