Sorry, I started this response earlier (after helios) but had to do some stuff...
This is an "AAAHHH HELP!" question.
Lines 1 and 109
You don't need C stuff in your C++ program. Just
return 0;
. (Personally, I
like the EXIT_SUCCESS macro, but it is really wholly redundant. A successful C or C++ program almost always returns zero, and that much is understood by C and C++ programmers everywhere -- to the point that if your program does not return zero you have to explain why.)
Line 9
They are
not global variables. They are the data encapsulated by your class. As they are (correctly) listed as
private fields, only your class's methods may access them.
Line 10
This should be an array of
char -- you don't need more space per digit than that.
Before you get too much further, you need to decide
explicitly how your number is stored in the array. The first thing to consider is that your array is supposed to be an array of
digits - that is: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
Here is my recommendation: store it as an array of integer values. That is, don't store it as the character values '0'..'9', but as the actual numbers 0..9.
1 2
|
char a_number = 4;
char a_character = '4';
|
See the difference? The choice between characters and integers affects how easy it is to do other operations on the number. If you choose characters, then every time you wish to add, subtract, multiply, etc then you must convert each digit between its character value and its integer meaning. If you choose integers, then the only time you need to do the conversion is when reading or writing the number.
At the moment, you are treating it both ways. Remember, the
character '2' is a number, but it is
not the
number 2.
The next consideration is how the number is arrayed:
■ most significant digit to least (reading order) - 123 is stored as ['1', '2', '3']
■ least significant digit to most - 123 is stored as ['3', '2', '1']
I recommend you keep your digits in the second order. This makes your operations that much
easier, since you don't have to line-up the powers between two different
HugeInts - it is already done for you. The one's place is always at index zero. The ten's place is always at index one. Etc.
The final thing to remember is that an integer number may be negative. If we restrict our array to positive values (a reasonable thing to do to keep the code simple), then we need an extra field to track the sign.
With all that, I would write my private section as:
8 9 10 11
|
private:
char digits[40]; // the digits of the number, least significant to most
int count; // number of digits in the number
bool negative; // the sign of the number
|
Methods
Your instructor has
not told you to overload the C++ binary operators for
HugeInts. Instead, you have been told to create specifically-named methods.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
public:
// I/O. Make your life easy now and require a stream to operate over as argument
void input( istream& ins );
void output( ostream& outs );
// Addition
void add( const HugeInt& that );
void subtract( const HugeInt& that );
// Comparisons
bool isEqualto( const HugeInt& that ) const;
bool isNotEqualTo( const HugeInt& that ) const;
bool isGreaterThan( const HugeInt& that ) const;
bool isLessThan( const HugeInt& that ) const;
bool isGreaterThanOrEqualTo( const HugeInt& that ) const;
bool isLessThanOrEqualTo( const HugeInt& that ) const;
// Additional predicate
bool isZero() const;
// Extra credit: Multiplication
void multiply( const HugeInt& that );
void divide( const HugeInt& that ); // (not "devide")
void modulo( const HugeInt& that ); // (not "modulus")
|
Notice that that additive and multiplicative methods
modify the
HugeInt. In C++, essentially, the following operations on a
HugeInt are the same:
■
this.add( that );
■
this += that;
The comparative methods, in contrast,
do not modify the
HugeInt. Hence, they are
const methods.
I have a return type of
void on your arithmetic operations, but if I were writing this assignment myself I would return a reference to *this. This is a personal preference, however:
17 18
|
// Addition
HugeInt& add( const HugeInt& that );
|
I would also recommend you have an
assign() method, that copies the value of another
HugeInt to this one.
Now in your CPP file, you can implement the methods you defined in the class above (listed in a HPP file).
At this point, you need to decide how to handle things like
overflow.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
HugeInt& HugeInt::add( const HugeInt& that )
{
if (negative != that.negative)
{
... // Think about how to handle this using what other methods there are.
}
else
{
int index = 0;
char carry = 0;
... // Loop through each digit in this.digits[] and that.digits[] and add everything
}
return *this;
}
bool HugeInt::isEqual( const HugeInt& that ) const
{
if (count != that.count)
return false;
if (negative != that.negative)
return false;
for (int n = 0; n < count; n++)
if (digits[n] != that.digits[n])
return false;
return true;
}
|
Well, that's enough for now. Hope this helps.