Tuesday, February 10th at 11:59pm
Now that we've explored the limits of the built-in long
int
and unsigned long int
types, how can we do
better? For this assignment, you'll define a class called
BigInt
that will hold positive integers up to 20 digits
in length.
Your BigInt
class should meet the following
requirements:
BigInt
class should be defined in two files,
BigInt.h
and BigInt.cc
. The header file
should contain the description of your class, and the code file should
contain the implementation of the functions. See the class
notes for the third lecture for an example of how to define a
class this way.main.cc
file that
contains a main
function, so you can compile and test
your BigInt
class. You wouldn't write code without
testing it, would you? (You don't need to turn in your
main.cc
.)cd
to
the directory with your code and type make
. The
make
program will use the Makefile to compile your three
files (BigInt.cc
, BigInt.h
, and
main.cc
) into an executable called
main
.BigInt
class should store its data using an
integer array of size 20. Each element of the array should hold one
digit of the number. For example, if the number you want to store is
658
, then one element in your 20-digit array should be
6
, one should be 5
, and one should be
8
. It's up to you to decide exactly where those elements
should go, and what the other 17 elements should be.unsigned int
and creates a new BigInt
out of
it:
BigInt b1( 0 ); BigInt b2( 314159 ); BigInt b3( 123456789 );
print
function that displays
the number stored in your class to the screen:
BigInt b1( 0 ); b1.print(); // should print '0' (without the quotes) BigInt b2( 314159 ); b2.print(); // ...and '314159' BigInt b3( 123456789 ); b3.print(); // ...and '123456789'You shouldn't display any leading zeros or whitespace when printing your number.
After you've saved the Makefile and created
BigInt.cc
, BigInt.h
, and
main.cc
, you should be able to compile your code by
typing:
If all goes well, you shouldn't see any error messages. Then you can run your code by typing:
Whatever you've included in your main
function should
run, and any output that is generated should appear on your
screen.
When you're happy with your code, use the
turnin program to submit your BigInt.cc
and
BigInt.h
files. Use assignment2
as the
assignment name.
BigInt.cc
BigInt.h
Easily done, you say?
Add the following public utility functions to your
BigInt
class:
isZero
function that takes no arguments, and returns a bool
describing if the number is zero or notgetLength
function that takes no arguments, and returns an unsigned int
describing the number of digits in the number