138k views
7 votes
Create a class HugeInteger which uses a vector of digits to store huge integers. A HugeInteger object has a sign that indicates if the represented integer is non-negative (0) or negative (1). Provide methods parse, toString, add and subtract. Method parse should receive a String, extract each digit using method charAt and place the integer equivalent of each digit into the integer vector. For comparing HugeInteger objects, provide the following methods: isEqualTo, isNotEqualTo, isGreaterThan, isLessThan, isGreaterThanOrEqualTo, and isLessThanOrEqualTo. Each of these is a predicate method that returns true if the relationship holds between the two HugeInteger objects and returns false if the relationship does not hold. Provide a rpedicate method isZero.

User Qxn
by
5.3k points

1 Answer

4 votes

Answer:

#include "hugeint1.h"

#include

using namespace std;

int main()

{

HugeInteger n1( 7654321 );

HugeInteger n2( 7891234 );

HugeInteger n3;

HugeInteger n4( 5 );

HugeInteger n5;

n5 = n1.add( n2 );

n1.output();

cout << " + "; n2.output();

cout << " = "; n5.output();

cout << "\\\\";

n5 = n2.subtract( n4 );

n2.output();

cout<< " - "; n4.output();

cout << " = "; n5.output();

cout << "\\\\";

if ( n1.isEqualTo( n1 ) == true )

{

n1.output();cout << " is equal "; n1.output(); cout << "\\\\";

}

if ( n1.isNotEqualTo( n2 ) == true )

{

n1.output();cout << " is not equal to ";n2.output();cout << "\\\\";

}

if ( n2.isGreaterThan( n1 ) == true )

{

n2.output();cout << " is greater than ";n1.output();cout <<"\\\\";

}

if ( n2.isLessThan( n4 ) == true )

{

n4.output();cout << " is less than ";n2.output();cout << "\\\\";

}

if( n4.isLessThanOrEqualTo( n4 ) == true )

{

n4.output();cout << " is less than or equal to ";n4.output();

cout << "\\\\";

}

if ( n3.isGreaterThanOrEqualTo( n3 ) == true )

{

n3.output();cout << " is greater than or equal to ";n3.output();

cout << "\\\\";

}

if ( n3.isZero() != true )

{

cout << "n3 contains value ";n3.output();cout << "\\\\";

}

return 0;

}

//class definitions

#ifndef HUGEINT1_H

#define HUGEINT1_H

class HugeInteger {

public:

HugeInteger( long = 0 );

HugeInteger add( const HugeInteger & );//addition operator; HugeInt + HugeInt

HugeInteger subtract( const HugeInteger & );//subtraction operator; HugeInt - HugeInt

bool isEqualTo( HugeInteger & );

bool isNotEqualTo( HugeInteger & );

bool isGreaterThan(HugeInteger & );

bool isLessThan( HugeInteger & );

bool isGreaterThanOrEqualTo( HugeInteger & );

bool isLessThanOrEqualTo( HugeInteger & );

bool isZero();

void output();

short* getInteger()

{

return integer;

}

private:

short integer[ 40 ];

};

#endif

//implemetations

#include using std::cout;

#include "hugeint1.h"

HugeInteger::HugeInteger( long value )

{

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

integer[ i ] = 0;

for ( int j = 39; value != 0 && j >= 0; j-- )

{

integer[ j ] = value % 10;

value /= 10;

}

}

HugeInteger HugeInteger::add( const HugeInteger &op2 )

{

HugeInteger temp;

int carry = 0;

for ( int i = 39; i >= 0; i-- ) {

temp.integer[ i ]=integer[ i ] + op2.integer[ i ] + carry;

if ( temp.integer[ i ] > 9 ){

temp.integer[ i ] %= 10; // reduce to 0-9

carry = 1;

} else{

carry = 0;

}

return temp;

}

void HugeInteger::output()

{

int i;

for ( i = 0; ( integer[ i ] == 0 ) && ( i <= 39 ); i++ );

if ( i == 40 )

cout << 0;

else

for ( ; i <= 39; i++ )

cout << integer[ i ];

}

HugeInteger HugeInteger::subtract( const HugeInteger &op2 )

{

HugeInteger temp;

int borrow = 0;

for ( int i = 39; i >= 0; i-- ){

if ( integer[i] < op2.integer[i] ){

temp.integer[ i ]=(integer[i]+10)-op2.integer[i]- borrow;

borrow = 1;

} else {

temp.integer[ i ]=integer[i] - op2.integer[ i ] - borrow;

borrow = 0;

}

}

return temp;

}

bool HugeInteger::isEqualTo( HugeInteger &x ){

return integer == x.getInteger();

}

bool HugeInteger::isNotEqualTo( HugeInteger &x )

{ return !( this->isEqualTo( x ) ); }

bool HugeInteger::isGreaterThan( HugeInteger &x )

{ return integer < x.getInteger(); }

bool HugeInteger::isLessThan( HugeInteger &x )

{ return integer > x.getInteger(); }

bool HugeInteger::isGreaterThanOrEqualTo( HugeInteger &x )

{ return integer <= x.getInteger(); }

bool HugeInteger::isLessThanOrEqualTo( HugeInteger &x )

{ return integer >= x.getInteger(); }

bool HugeInteger::isZero()

{ return ( getInteger() == 0 ); }

//main file

#include

using std::cout; using std::endl;

#include "hugeint1.h"

int main()

{

HugeInteger n1( 7654321 );

HugeInteger n2( 7891234 );

HugeInteger n3;

HugeInteger n4( 5 );

HugeInteger n5;

n5 = n1.add( n2 );

n1.output();

cout << " + "; n2.output();

cout << " = "; n5.output();

cout << "\\\\";

n5 = n2.subtract( n4 );

n2.output();

cout<< " - "; n4.output();

cout << " = "; n5.output();

cout << "\\\\";

if ( n1.isEqualTo( n1 ) == true )

{n1.output();cout << " is equal "; n1.output(); cout << "\\\\";}

if ( n1.isNotEqualTo( n2 ) == true )

{n1.output();cout << " is not equal to ";n2.output();cout << "\\\\";}

if ( n2.isGreaterThan( n1 ) == true )

{n2.output();cout << " is greater than ";n1.output();cout <<"\\\\";}

if ( n2.isLessThan( n4 ) == true )

{n4.output();cout << " is less than ";n2.output();cout << "\\\\";}

if( n4.isLessThanOrEqualTo( n4 ) == true )

{n4.output();cout << " is less than or equal to ";n4.output();

cout << "\\\\";}

if ( n3.isGreaterThanOrEqualTo( n3 ) == true )

{n3.output();cout << " is greater than or equal to ";n3.output();

cout << "\\\\";}

if ( n3.isZero() != true )

{cout << "n3 contains value ";n3.output();cout << "\\\\";}

return 0;

}

Step-by-step explanation:

The HughInteger class is a C++ class created to instantiate a vector object of both negative and positive integer values. It has the aforementioned methods in the code above that is used to interact with the several vector objects created from the class.

User Vextasy
by
5.5k points