Assignment
4

This
assignment is a continuation of the previous one. You will take the HugeInteger structure and repackage it as a C++ class with
the same name. Since many students still have problems with assignment 3, I will
give you some hints on how to implement the operations (+, -, *) more
elegantly.

First,
create a class called HugeInteger with the same data
members, sign and a[30]. You want to make them
private, of course. Then add two constructors, one with no argument and the
other with a string argument, p. The former initializes the HugeInteger
to 0; the latter initializes the HugeInteger to the
number represented by the string p.

Next
convert the HI_set, HI_add,
HI_subtract, HI_multiply,
and HI_output to member functions of the class. Note
that you always eliminate the first argument when you do the conversion. For
example, HI_set(HugeInteger &i, char* p) becomes a member function called set(char *p);
and HI_add(const HugeInteger
&x, const HugeInteger &y, HugeInteger&
z) becomes add(const HugeInteger &y, HugeInteger& z). To call these member functions on a HugeInteger x, you would use

x.set("9876543210987654321");
// set x to 9876543210987654321

and

x.add(y, z); // z = x + y. Yes, I know this is ugly. We’ll
do something about it later.

Finally,
operations on HugeIntegers will be a lot easier if we
allow a temporary form of storing the HugeInteger in
array a. For example, all three examples below represent the same HugeInteger 1:

1)

a: |
1 |
0 |
0 |
0 |
. . . |
0 |
0 |

2)

a: |
-9 |
1 |
0 |
0 |
. . . |
0 |
0 |

(Note
that -9 + 10 = 1.)

3)

a: |
-319 |
12 |
2 |
0 |
. . . |
0 |
0 |

(Note
that -319 + 120 + 200 = 1.)

Let’s
say that representations such as 2) and 3) are un-normalized. After
normalization, they should look like 1). To simplify the arithmetic operations
on HugeInteger, first write a private member function
normalize( ) that convert an un-normalized HugeInteger into the correct form. Then, the member
functions add( ), subtract( ) and multiply( ) can
compute an un-normalized result relatively easily and then call normalize( ) to
get the final result.

This is
not the end of HugeInteger yet. We will do more stuff
on this class, so make sure you get it right as early as possible.