You are on page 1of 2

apcard Badd( apcard augend, apcard addend )

// IN IN
// Pre: augend and addend are C++ strings representing
// valid non-negative decimal integer values.
// Post: evaluates to an apcard representing the
// sum of augend + addend.
{
if (augend.length() > addend.length())
swap(augend, addend);

string str = "";


int n1 = augend.length(), n2 = addend.length();
reverse(augend.begin(), augend.end());
reverse(addend.begin(), addend.end());
int carry = 0;
for (int i=0; i<n1; i++)
{
int sum = ((augend[i]-'0')+(addend[i]-'0')+carry);
str.push_back(sum%10 + '0');
carry = sum/10;
}
for (int i=n1; i<n2; i++)
{
int sum = ((addend[i]-'0')+carry);
str.push_back(sum%10 + '0');
carry = sum/10;
}
if (carry)
str.push_back(carry+'0');
reverse(str.begin(), str.end());

return str;
} // end Badd

apcard Bsub( apcard minuend, apcard subtrahend )


// IN IN
// Pre: minuend and subtrahend are C++ strings representing
// valid non-negative decimal integer values and
// minuend >= subtrahend.
// Post: evaluates to an apcard representing the (cardinal number)
// difference of minuend - subtrahend.
{
assert( Bcmp(minuend,subtrahend)>=0 );

// Bsub stub code.


int minuendValue = stoi(minuend);
int subtrahendValue = stoi(subtrahend);
apcard difference = to_string(minuendValue - subtrahendValue);
return difference;
} // end Bsub

apcard Bmult( apcard multiplicand, apcard multiplier )


// IN IN
// Pre: multiplicand and multiplier are C++ strings representing
// valid non-negative decimal integer values.
// Post: evaluates to an apcard representing the
// product of multiplicand * multiplier.
{
// Bmult stub code.
int multiplicandValue = stoi(multiplicand);
int multiplierValue = stoi(multiplier);
apcard product = to_string(multiplicandValue * multiplierValue);
return product;
} // end Bmult

You might also like