Declare and define the following in the .cpp and .h files . Modify the BigInt cl
ID: 663090 • Letter: D
Question
Declare and define the following in the .cpp and .h files .
Modify the BigInt class by
adding the > operation to determine whether one BigInt object is bigger than another.
adding the subtraction operation - to class BigInt: int1 - int2 which should return 0 if int1 is less than int2.
(Bonus) adding the multiplication operation * to class BigInt.
---------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------
The output should be like --
Enter a big integer:
Enter 3-digit blocks, separated by spaces.
Enter a negative integer in last block to signal the end of input.
347 965 434 213 -1
Enter another big integer:
298 432 678 984 -1
The sum of
347,965,434,213 + 298,432,678,984
is
646,398,113,197
The bigger number of
347,965,434,213
and
298,432,678,984
is
347,965,434,213
The subtraction of
347,965,434,213 - 298,432,678,984
is
49,532,755,229
BONUS part:
The multiplication of
347,965,434,213 * 298,432,678,984
is
103,844,256,726,016,399,679,592
Add more integers (Y or N)? Y
Enter a big integer:
453 213 345 -1
Enter another big integer:
892 -1
The sum of
453,213,345 + 892
is
453,214,237
The bigger number of
453,213,345
and
892
is
453,213,345
The subtraction of
453,213,345 - 892
is
453,212,453
BONUS part:
The multiplication of
453,213,345 * 892
is
404,266,303,740
Add more integers (Y or N)? N
Press any key to continue
Explanation / Answer
1. adding the > operation to determine whether one BigInt object is bigger than another.
bool BigInt::operator >(BigInt number2)
{
BigInt sum;
short int first, // a block of 1st addend (this object)
second, // a block of 2nd addend (addend2)
result, // a block in their sum
carry = 0; // the carry in adding two blocks
bool res = false;
list<short int>::const_iterator // to iterate right to left
it1 = myList.begin(), // through 1st list, and
it2 = number2.myList.begin(); // through 2nd list
while (it1 != myList.end() || it2 != number2.myList.end())
{
if (it1 != myList.end())
{
first = *it1;
it1++ ;
}
else
first = 0;
if (it2 != number2.myList.end())
{
second = *it2;
it2++ ;
}
else
second = 0;
if(first > second)
return true;
else if (first < second)
return false;
else
res = false;
}
}
2. adding the subtraction operation - to class BigInt: int1 - int2 which should return 0 if int1 is less than int2
int BigInt::operator -(BigInt number2)
{
BigInt sum;
short int first, // a block of 1st addend (this object)
second, // a block of 2nd addend (addend2)
result, // a block in their sum
carry = 0; // the carry in adding two blocks
bool res = false;
list<short int>::const_iterator // to iterate right to left
it1 = myList.begin(), // through 1st list, and
it2 = number2.myList.begin(); // through 2nd list
while (it1 != myList.end() || it2 != number2.myList.end())
{
if (it1 != myList.end())
{
first = *it1;
it1++ ;
}
else
first = 0;
if (it2 != number2.myList.end())
{
second = *it2;
it2++ ;
}
else
second = 0;
if(first < second)
return 0;
else if (first > second)
return (first - second);
else
res = 0;
}
}
3. adding the multiplication operation * to class BigInt
1. adding the > operation to determine whether one BigInt object is bigger than another.
bool BigInt::operator >(BigInt number2)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.