(HugeInteger Class) Create a class HugeInteger that uses a 40-element array of digits to store integers as large as 40 digits each. Provide member functions input, output, add and sub- stract. For comparing HugeInteger objects, provide functions isEqualTo, isNotEqualTo, isGrea- terThan, isLessThan, isGreaterThanOrEqualTo and isLessThanOrEqualTo—each of these is a “predicate” function that
simply returns true if the relationship holds between the two Huge- Integers and returns false if the relationship does not hold. Also, provide a predicate function is- Zero. If you feel ambitious, provide member functions multiply, divide and modulus.
What will be an ideal response?
```
// HugeInteger class definition.
#ifndef HUGEINTEGER_H
#define HUGEINTEGER_H
#include
using namespace std;
class HugeInteger
{
public:
HugeInteger( long = 0 ); // conversion/default constructor
HugeInteger( const string & ); // copy constructor
// addition operator; HugeInteger + HugeInteger
HugeInteger add( HugeInteger & );
// addition operator; HugeInteger + int
HugeInteger add( int );
// addition operator;
// HugeInteger + string that represents large integer value
HugeInteger add( const string & );
// subtraction operator; HugeInteger - HugeInteger
HugeInteger subtract( HugeInteger & );
// subtraction operator; HugeInteger - int
HugeInteger subtract( int );
// subtraction operator;
// HugeInteger - string that represents large integer value
HugeInteger subtract( const string & );
bool isEqualTo( HugeInteger & ); // is equal to
bool isNotEqualTo( HugeInteger & ); // not equal to
bool isGreaterThan(HugeInteger & ); // greater than
bool isLessThan( HugeInteger & ); // less than
bool isGreaterThanOrEqualTo( HugeInteger & ); // greater than
// or equal to
bool isLessThanOrEqualTo( HugeInteger & ); // less than or equal
bool isZero(); // is zero
void input( const string & ); // input
void output(); // output
private:
short integer[ 40 ]; // 40 element array
}; // end class HugeInteger
#endif
```
```
// Member-function definitions for class HugeInteger.
#include
#include "HugeInteger.h" // include definition of class HugeInteger
using namespace std;
// default constructor; conversion constructor that converts
// a long integer into a HugeInteger object
HugeInteger::HugeInteger( long value )
{
// initialize array to zero
for ( int i = 0; i < 40; i++ )
integer[ i ] = 0;
// place digits of argument into array
for ( int j = 39; value != 0 && j >= 0; j-- )
{
integer[ j ] = static_cast< short >( value % 10 );
value /= 10;
} // end inner for
} // end HugeInteger constructor
// copy constructor;
// converts a char string representing a large integer into a HugeInteger
HugeInteger::HugeInteger( const string &string )
{
input( string ); // call input to initialize HugeInteger
} // end HugeInteger constructor
// addition operator; HugeInteger + HugeInteger
HugeInteger HugeInteger::add( HugeInteger &op2 )
{
HugeInteger temp; // temporary result
int carry = 0;
// iterate through HugeInteger
for ( int i = 39; i >= 0; i-- )
{
temp.integer[ i ] =
integer[ i ] + op2.integer[ i ] + carry;
// determine whether to carry a 1
if ( temp.integer[ i ] > 9 )
{
temp.integer[ i ] %= 10; // reduce to 0-9
carry = 1;
} // end if
else // no carry
carry = 0;
} // end for
return temp; // return the sum
} // end function add
// addition operator; HugeInteger + int
HugeInteger HugeInteger::add( int op2 )
{
// convert op2 to a HugeInteger, then invoke add
return add( HugeInteger( op2 ) );
} // end function add
// HugeInteger + string that represents large integer value
HugeInteger HugeInteger::add( const string &op2 )
{
// convert op2 to a HugeInteger, then invoke add
return add( HugeInteger( op2 ) );
} // end function add
// subtraction operator, subtract op2 from (*this)
HugeInteger HugeInteger::subtract( HugeInteger &op2 )
{
// return if first value is smaller; we are not handling negatives
if ( isLessThan( op2 ) )
{
cout << "Error: Tried to subtract larger value from smaller value."
<< endl;
return HugeInteger( "0" );
} // end if
HugeInteger result( "0" ); // final result currently 0
// used to determine if previous digit had 10 added to it;
// if true, current digit needs to be reduced by 1
bool minusOne = false;
// for each digit in both arrays,
// subtract digit of smaller int from digit of larger int
for ( int i = 39; i >= 0; i-- )
{
// find digits we will currently be subtracting
int topValue = integer[ i ];
int bottomValue = op2.integer[ i ];
// if previous topValue had 10 added to it;
// subtract one from current topValue
if ( minusOne )
{
if ( topValue == 0 ) // topValue cannot become -1
// set to 9 but keep minusOne true for next digit
topValue = 9;
else
{
topValue -= 1; // subtract from top value
minusOne = false; // minusOne is handled, return to false
} // end else
} // end outer if
// if topValue larger, perform subtraction and store result
if ( topValue >= bottomValue )
result.integer[ i ] = topValue - bottomValue;
else
{
topValue += 10; // if bottomValue larger, add 10 to topValue
minusOne = true; // next digit must be decreased
// topValue is now larger, perform subtraction and store result
result.integer[ i ] = topValue - bottomValue;
} // end else
} // end for
return result; // return final result
} // end function subtract
// function to subtract an integer from a HugeInteger
HugeInteger HugeInteger::subtract( int op2 )
{
// convert op2 to a HugeInteger, then invoke subtract
return subtract( HugeInteger( op2 ) );
} // end function subtract
// function that takes string representing a number
// and subtracts it from a HugeInteger
HugeInteger HugeInteger::subtract( const string &op2 )
{
// convert op2 to a HugeInteger, then invoke subtract
return subtract( HugeInteger( op2 ) );
} // end function subtract
// function that tests if two HugeIntegers are equal
bool HugeInteger::isEqualTo( HugeInteger &x )
{
for ( int i = 39; i >= 0; i-- )
if ( integer[ i ] != x.integer[ i ] )
return false;
return true;
} // end function isEqualTo
// function that tests if two HugeIntegers are not equal
bool HugeInteger::isNotEqualTo( HugeInteger &x )
{
return !( isEqualTo( x ) );
} // end function isNotEqualTo
// function to test if one HugeInteger is greater than another
bool HugeInteger::isGreaterThan( HugeInteger &x )
{
return ( x.isLessThan( *this ) );
} // end function isGreaterThan
// function that tests if one HugeInteger is less than another
bool HugeInteger::isLessThan( HugeInteger &x )
{
for ( int i = 0; i < 40; i++ )
if ( integer[ i ] > x.integer[ i ] )
return false;
else if ( integer[ i ] < x.integer[ i ] )
return true;
return false;
} // end function isLessThan
// function that tests if one HugeInteger is greater than
// or equal to another
bool HugeInteger::isGreaterThanOrEqualTo( HugeInteger &x )
{
return ( !isLessThan( x ) );
} // end function isGreaterThanOrEqualTo
// function that tests if one HugeInteger is less than or
// equal to another
bool HugeInteger::isLessThanOrEqualTo( HugeInteger &x )
{
return ( isEqualTo( x ) || isLessThan( x ) );
} // end function isLessThanOrEqualTo
// function that tests if a HugeInteger is zero
bool HugeInteger::isZero()
{
for (int i = 0; i < 40; i++ )
if ( integer[ i ] != 0 )
return false;
return true;
} // end function isZero
// converts a string representing a large integer into a HugeInteger
void HugeInteger::input( const string &val )
{
// initialize array to zero
for ( int i = 0; i < 40; i++ )
integer[ i ] = 0;
// place digits of argument into array
int length = val.size();
for ( int j = 40 - length, k = 0; j < 40; j++, k++ )
if ( isdigit( val[ k ] ) )
integer[ j ] = val[ k ] - '0';
} // end function input
// overloaded output operator
void HugeInteger::output()
{
int i; // used for looping
for ( i = 0; ( integer[ i ] == 0 ) && ( i <= 39 ); i++ )
; // skip leading zeros
if ( i == 40 )
cout << 0;
else
for ( ; i <= 39; i++ ) // display the HugeInteger
cout << integer[ i ];
} // end function output
```
```
// HugeInteger test program.
#include
#include "HugeInteger.h" // include definition of class HugeInteger
using namespace std;
int main()
{
HugeInteger n1( 7654321 ); // HugeInteger object n1
HugeInteger n2( "100000000000000" ); // HugeInteger object n2
HugeInteger n3; // HugeInteger object n3
HugeInteger n4( 5 ); // HugeInteger object n4
HugeInteger n5; // HugeInteger object n5
// outputs the sum of n1 and n2
n5 = n1.add( n2 );
n1.output();
cout << " + ";
n2.output();
cout << " = ";
n5.output();
cout << "\n\n";
// assigns the difference of n2 and n4 to n5 then outputs n5
n5 = n2.subtract( n4 );
n2.output();
cout<< " - ";
n4.output();
cout << " = ";
n5.output();
cout << "\n\n";
// checks for equality between n2 and n2
if ( n2.isEqualTo( n2 ) == true )
{
n2.output();
cout << " is equal to ";
n2.output();
cout << "\n\n";
} // end if
// checks for inequality between n1 and n2
if ( n1.isNotEqualTo( n2 ) == true )
{
n1.output();
cout << " is not equal to ";
n2.output();
cout << "\n\n";
} // end if
// tests for greater number between n2 and n1
if ( n2.isGreaterThan( n1 ) == true )
{
n2.output();
cout << " is greater than ";
n1.output();
cout << "\n\n";
} // end if
// tests for smaller number between n4 and n2
if ( n4.isLessThan( n2 ) == true )
{
n4.output();
cout << " is less than ";
n2.output();
cout << "\n\n";
} // end if
// tests for smaller or equal number between n4 and n4
if ( n4.isLessThanOrEqualTo( n4 ) == true )
{
n4.output();
cout << " is less than or equal to ";
n4.output();
cout << "\n\n";
} // end if
// tests for smaller or equal number between n4 and n2
if ( n4.isLessThanOrEqualTo( n2 ) == true )
{
n4.output();
cout << " is less than or equal to ";
n2.output();
cout << "\n\n";
} // end if
// tests for greater or equal number between n3 and n3
if ( n3.isGreaterThanOrEqualTo( n3 ) == true )
{
n3.output();
cout << " is greater than or equal to ";
n3.output();
cout << "\n\n";
} // end if
// tests for greater or equal number between n2 and n3
if ( n2.isGreaterThanOrEqualTo( n3 ) == true )
{
n2.output();
cout << " is greater than or equal to ";
n3.output();
cout << "\n\n";
} // end if
// tests for zero at n3
if ( n3.isZero() == true )
{
cout << "n3 contains ";
n3.output();
cout << "\n\n";
} // end if statement
} // end main
```
7654321 + 100000000000000 = 100000007654321
100000000000000 - 5 = 99999999999995
100000000000000 is equal to 100000000000000
7654321 is not equal to 100000000000000
100000000000000 is greater than 7654321
5 is less than 100000000000000
5 is less than or equal to 5
5 is less than or equal to 100000000000000
0 is greater than or equal to 0
100000000000000 is greater than or equal to 0
n3 contains 0
You might also like to view...
Case-Based Critical Thinking QuestionsCase 2-1Megan is having a lot of fun with Adobe Flash CS6 because she is an excellent artist with a knack for quickly learning new software and applying her considerable artistic skills to her multimedia work. Megan has drawn a series of lines that together constitute the outlines of a piece of stained glass. She is concerned that the lines might move apart accidentally. After selecting the individual lines, what keys does she press on the keyboard?
A. Ctrl+A B. Alt+S C. Ctrl+S D. Ctrl+G
A(n) ________ server is used to host an Internet site.
Fill in the blank(s) with the appropriate word(s).
On-the-job training can result in substandard work performance while the trainee gets up to speed.
Answer the following statement true (T) or false (F)
Which part of an email message typically contains the sender's name, title, and contact information?
A. Attachment B. Signature C. Subject D. Cc