Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

how to fix add function based on the test cases and output. BigInteger BigInteger::add ( const BigInteger& N ) const { / / Initializing variables

how to fix add function based on the test cases and output. BigInteger BigInteger::add(const BigInteger& N) const {
// Initializing variables to store the result and carry
BigInteger result;
int carry =0;
// Creating copies of the List objects to avoid modifying the originals
List thisDigits = digits;
List NDigits = N.digits;
// Initialize cursors for both BigIntegers
thisDigits.moveBack(); // Move to the back to start from the end
NDigits.moveBack();
// For finding out the sign of the result
int resultSign = signum;
// Scroll through the digits of the two BigInteger numbers one by one
while (thisDigits.position()!=0|| NDigits.position()!=0|| carry !=0){
// Calculating sum of digits plus carry
int sum = carry;
if (thisDigits.position()!=0){
sum += thisDigits.peekPrev();
thisDigits.movePrev();
}
if (NDigits.position()!=0){
sum += NDigits.peekPrev();
NDigits.movePrev();
}
// Update the report and add the total to the result
carry = sum / base;
result.digits.insertAfter(abs(sum)% base);
}
// Set the sign of the result
result.signum = resultSign;
// Changing the sign based on result
if (resultSign ==0){
result.signum =(carry <0)?-1 : 1;
} else if (carry <0 && result.signum >0){
result.signum =-result.signum;
} else if (carry >0 && result.signum <0){
result.signum =-result.signum;
}
return result;
}
These are the test cases: case Add_test: {
/*
* Adding numbers fall into one of 4 cases, denote pos = positive number,
* neg = negative number
*
* pos + pos = pos
*
* pos + neg =0
*<0
*>0
*
* neg + pos =0
*<0
*>0
*
* neg + neg = neg
*/
A = BigInteger("+111122223333");
B = BigInteger("+222211110000");
// pos + pos = pos
D = BigInteger("+333333333333");
C = A + B;
if (!(C == D))
return 1;
// add a positive and a negative integer
//-> pos + neg =0
B = BigInteger("-111122223333");
C = A + B;
if (C.sign()!=0)
return 2;
//-> pos + neg >0
B = BigInteger("-110122223333");
D = BigInteger("1000000000");
C = A + B;
if (C.sign()!=1)
return 31;
if (!(C == D))
return 32;
//-> pos + neg <0
B = BigInteger("-112122223333");
D = BigInteger("-1000000000");
C = A + B;
if (C.sign()!=-1)
return 41;
if (!(C == D))
return 42;
//-> neg + neg = neg
A = BigInteger("-221211110000");
D = BigInteger("-333333333333");
C = A + B;
if (!(C == D))
return 5;
return 0;
}
This is teh output: A =91287346670000043892345634563400005619187236478
B =9876545439000000345634560000000002000034565430000000006543654365346534
C =98765454390000034563456191872363456345619187236000456456345756780000065436543
Killed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Domain Transfer Learning With 3q Data Processing

Authors: Ahmed Atif Hussain

1st Edition

B0CQS1NSHF, 979-8869061805

More Books

Students also viewed these Databases questions