question archive Create class HugeInteger that uses a 30-element array of digits to store integers as large as 30 digits each

Create class HugeInteger that uses a 30-element array of digits to store integers as large as 30 digits each

Subject:Computer SciencePrice:5.86 Bought12

Create class HugeInteger that uses a 30-element array of digits to store integers as large as 30 digits each. Provide member functions input, output, add and substract. For comparing HugeInteger objects, provide functions isEqualTo, isNotEqualTo, isGreaterThan, isLessThan, isGreaterThanOrEqualTo and isLessThanOrEqualToeach of these is a "predicate" function that simply returns TRue if the relationship holds between the two HugeIntegers and returns false if the relationship does not hold. Also, provide a predicate function isZero.

b) If you feel ambitious, provide member functions multiply, divide and modulus.

 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Create class HugeInteger that uses a 30-element array of digits to store integers as large as 30 digits each. Provide member functions input, output, add and substract. 

Step-by-step explanation

Here is your Answer:

--------=====-------

 Solution ::

OUTPUT:

main.cpp:

 

#include <iostream>

#include <string>

#include "HugeInteger.h"

using namespace std;

int main()

{

//begin variable declarations

int opr1[ ARRAY_SIZE ] = { 0,1,2,3,4,5,6,7,8,9 };

int opr2[ ARRAY_SIZE ] = { 0,1,2,3,4,5,6,7,8,9 };

int opr3[ ARRAY_SIZE ] = { 0,0,2,3,4,5,6,7,8,9 };

int opr4[ ARRAY_SIZE ] = { 0,0,0,0,0,0,6,7,8,9 };

int opr5[ ARRAY_SIZE ] = { 0,0,0,0,0,0,1,2,3,4 };

int opr6[ ARRAY_SIZE ] = { 0 };

HugeInteger operand1 = opr1;

HugeInteger operand2 = opr2;

HugeInteger operand3 = opr3;

HugeInteger operand4 = opr4;

HugeInteger operand5 = opr5;

HugeInteger operand6 = opr6;

bool operandFlag;

//end variable declarations

  

//function prototypes

void boolTest( bool oprFlag );

//test output function

cout << "The value of operand1 is: ";

operand1.output();

cout << "The value of operand2 is: ";

operand2.output();

cout << "The value of operand3 is: ";

operand3.output();

cout << "The value of operand4 is: ";

operand4.output();

cout << "The value of operand5 is: ";

operand5.output();

cout << "The value of operand6 is: ";

operand6.output();

//test addition operation

cout << "The sum of operand1 and operand2 is: " << endl;

HugeInteger sum = operand1.addition( operand2 );

sum.output();

 

cout << "The subtraction of operand1 and operand2 is: " << endl;

HugeInteger sub = operand1.subtract( operand2 );

sub.output();

//test isZero

cout << "Test isZero on operand5 ";

operandFlag = operand5.isZero();

boolTest( operandFlag );

cout << "Test isZero on operand6 ";

operandFlag = operand6.isZero();

boolTest( operandFlag );

  

//test isEqual

cout << "Test isEqual on operand1 and operand2 ";

operandFlag = operand1.isEqualTo( operand2 );

boolTest( operandFlag );

cout << "Test isEqual on operand3 and operand4 ";

operandFlag = operand3.isEqualTo( operand4 );

boolTest( operandFlag );

  

//test isNotEqualTo

cout << "Test isNotEqualTo on operand1 and operand2";

operandFlag = operand1.isNotEqualTo( operand2 );

boolTest( operandFlag );

cout << "Test isNotEqualTo on operand3 and operand4";

operandFlag = operand3.isNotEqualTo( operand4 );

boolTest( operandFlag );

  

//test isGreaterThanOrEqualTo

cout << "Test isGreaterThanOrEqualTo on operand1 and operand2 ";

operandFlag = operand1.isGreaterThanOrEqualTo( operand2 );

boolTest( operandFlag );

cout << "Test isGreaterThanOrEqualTo on operand2 and operand1 ";

operandFlag = operand2.isGreaterThanOrEqualTo( operand1 );

boolTest( operandFlag );

cout << "Test isGreaterThanOrEqualTo on operand3 and operand4 ";

operandFlag = operand3.isGreaterThanOrEqualTo( operand4 );

boolTest( operandFlag );

cout << "Test isGreaterThanOrEqualTo on operand4 and operand3 ";

operandFlag = operand4.isGreaterThanOrEqualTo( operand3 );

boolTest( operandFlag );

  

//test isLessThanOrEqualTo

cout << "Test isLessThanOrEqualTo on operand1 and operand2 ";

operandFlag = operand1.isLessThanOrEqualTo( operand2 );

boolTest( operandFlag );

cout << "Test isLessThanOrEqualTo on operand2 and operand1 ";

operandFlag = operand2.isLessThanOrEqualTo( operand1 );

boolTest( operandFlag );

cout << "Test isLessThanOrEqualTo on operand5 and operand6 ";

operandFlag = operand5.isLessThanOrEqualTo( operand6 );

boolTest( operandFlag );

cout << "Test isLessThanOrEqualTo on operand6 and operand5 ";

operandFlag = operand6.isLessThanOrEqualTo( operand5 );

boolTest( operandFlag );

  

 

}

void boolTest( bool oprFlag ) //output results of boolean test

{

if ( oprFlag == true )

{

cout << "The value is true." << endl;

}

else

{

cout << "The value is false." << endl;

}

}

 

HughInteger.h

 

#ifndef HUGEINTEGER_H

#define HUGEINTEGER_H

#include <iostream>

using namespace std;

static const int ARRAY_SIZE = char32_t0;

class HugeInteger

{

public:

HugeInteger(); //default constructor

HugeInteger ( int operand[] ); //constructor with value

void input( int operand[] ); //input operand to array

HugeInteger addition( HugeInteger opr2 ); //function to add operands

HugeInteger subtract( HugeInteger opr2 );

bool isZero(); //test for a zero set

bool isEqualTo( HugeInteger opr2 ); //test for opr and opr2 being equal

bool isNotEqualTo( HugeInteger opr2 ); //test for opr and opr2 not being equal

bool isGreaterThanOrEqualTo( HugeInteger opr2 ); //test for opr >= opr2

bool isLessThanOrEqualTo( HugeInteger opr2 ); //test for opr <= opr2

void output();

private:

int opr[ ARRAY_SIZE ]; //integer array holding the huge integer

}; //end class HugeInteger

#endif

 

HughInteger.cpp

 

#include <iostream>

#include <string>

#include "HugeInteger.h"

using namespace std;

//default constructor

HugeInteger::HugeInteger()

{

for ( int i = 0; i < ARRAY_SIZE; i++ )

{

opr[ i ] = 0; //initialize opr to 0

} //end for

} //end default constructor

//constructor with integer array

HugeInteger::HugeInteger( int operand[] )

{

for ( int i = 0; i < ARRAY_SIZE; i++ )

{

opr[ i ] = operand[ i ]; //assign passed array to data member

} //end for

} //end constructor

//input operand to array

void HugeInteger::input( int operand[] )

{

for ( int i = 0; i < ARRAY_SIZE; i++ )

{

opr[ i ] = operand[ i ]; //assign passed array to data member

} //end for

} //end function input

//a function to add operands

HugeInteger HugeInteger::addition( HugeInteger opr2 )

{

int carryover[ ARRAY_SIZE ] = { 0 }; //array to catch the carryover from addition

HugeInteger sum; //instantiate HugeInteger sum object to hold sum

  

for ( int i = ARRAY_SIZE - 1; i >= 0; i-- )

{

int j = i - 1 ; //advance counter to set the carryover in the next addition line

int result = opr[ i ] + opr2.opr[ i ] + carryover[ i ];

carryover[ i ] = result / 10;

sum.opr[ i ] = result % 10;

} //end for

return sum;

} //end function addition

HugeInteger HugeInteger::subtract( HugeInteger opr2 )

{

int carryover[ ARRAY_SIZE ] = { 0 }; //array to catch the carryover from addition

HugeInteger sum;

  

for ( int i = ARRAY_SIZE - 1; i >= 0; i-- )

{

int j = i - 1 ; //advance counter to set the carryover in the next addition line

int result = opr[ i ] - opr2.opr[ i ] - carryover[ i ];

carryover[ i ] = result / 10;

sum.opr[ i ] = result % 10;

} //end for

return sum;

} //end function subtract

 

//a function to test for a zero set

bool HugeInteger::isZero()

{

int i = 0; //index counter

while ( opr[ i ] == 0 && i < ARRAY_SIZE )

{

i++;

} //end while

if ( i == ARRAY_SIZE )

{

return true; //array is a zero array

}

else

{

return false; //array is not a zero array

} //end if

} //end function isZero

//function to test for two operands being equal

bool HugeInteger::isEqualTo( HugeInteger opr2 )

{

int i = 0; //index counter

  

while ( opr[ i ] == opr2.opr[ i ] ) //compare operands sequentially

{

i++;

} //end while when either reach end of array or mismatch between elements

if ( i >= ARRAY_SIZE ) //test for positive case

{

return true;

}

else //negative clase

{

return false;

} //end if

} //end function isEqual

bool HugeInteger::isNotEqualTo( HugeInteger opr2 )

{

int oprIndex = 0;

int opr2Index = 0;

  

//find the start of the operands

//while i1 is not at head of operand1  

while ( opr[ oprIndex ] == 0 )

{

oprIndex++;

} //end while when start of number is found

//while i2 is not at head of operand2

while ( opr2.opr[ opr2Index ] == 0 )

{

opr2Index++;

}//end while

//test for mismatch length implying inequality

if ( oprIndex != opr2Index )

{

return true;

}

else

{

//compare element to element for equality

int i = 0; //index counter

while ( opr[ i ] == opr2.opr[ i ] && i < ARRAY_SIZE )

//loop until inequality found or reach end of array

{

i++;

} //end while either at point of inequality or end of array

  

//test for i >= ARRAY_SIZE

if ( i >= ARRAY_SIZE )

{

return false;

}

else

{

return true;

} //end if

} //end if

} //end function isNotEqualTo

bool HugeInteger::isGreaterThanOrEqualTo( HugeInteger opr2 )

{

int oprIndex = 0;

int opr2Index = 0;

//find the start of the operands

//while i1 is not at head of operand1  

while ( opr[ oprIndex ] == 0 )

{

oprIndex++;

} //end while when start of number is found

//while i2 is not at head of operand2

while ( opr2.opr[ opr2Index ] == 0 )

{

opr2Index++;

}//end while

  

//test for mismatch length implying inequality

if ( oprIndex < opr2Index )

{

return true; //opr is a larger number than opr2

}

else if ( oprIndex > opr2Index )

{

return false; //opr is a smaller number than opr2

}

else //enter when the length of opr and opr2 are the same

{

//compare element to element for equality (oprIndex == oprIndex)

int i = 0; //index counter set to start of number

while ( opr[ i ] == opr2.opr[ i ] && i < ARRAY_SIZE )

//loop digits starting at most significant to find inequality to test

{

i++;

} //end while at point of inequality or end of array

  

//test for i >= ARRAY_SIZE

if ( opr[ i ] > opr2.opr[ i ] || i >= ARRAY_SIZE )

{

return true; //opr >= opr2

}

else

{

return false; //opr < opr2

} //end if

} //end if

} //end function isGreaterThanOrEqualTo

bool HugeInteger::isLessThanOrEqualTo( HugeInteger opr2 )

{

int oprIndex = 0;

int opr2Index = 0;

//find the start of the operands

//while i1 is not at head of operand1  

while ( opr[ oprIndex ] == 0 )

{

oprIndex++;

} //end while when start of number is found

//while i2 is not at head of operand2

while ( opr2.opr[ opr2Index ] == 0 )

{

opr2Index++;

}//end while

  

//test for mismatch length implying inequality

if ( oprIndex > opr2Index )

{

return true; //opr is a smaller number than opr2

}

else if ( oprIndex < opr2Index )

{

return false; //opr is a larger number than opr2

}

else //enter when the length of opr and opr2 are the same

{

//compare element to element for equality (oprIndex == oprIndex)

int i = 0; //index counter set to start of number

while ( opr[ i ] == opr2.opr[ i ] && i < ARRAY_SIZE )

//loop digits starting at most significant to find inequality to test

{

i++;

} //end while at point of inequality or end of array

  

//test for i >= ARRAY_SIZE

if ( opr[ i ] < opr2.opr[ i ] || i >= ARRAY_SIZE )

{

return true; //opr < opr2

}

else

{

return false; //opr > opr2

} //end if

} //end if

} //end function isLessThanOrEqualTo

 

//function to display integer array

void HugeInteger::output()

{

int i = 0;

  

while ( opr[ i ] == 0 && i < (ARRAY_SIZE - 1) )

//loop to start of number with second condition to ensure last digit is displayed

{

i++;

} //end while

  

while ( i < ARRAY_SIZE )

{

cout << opr[ i ];

i++; //increment counter

} //end while

cout << endl;

Please see the attached file for the complete solution