Your code doesn't properly propagate the carry that may occur here
Quote:
if (c != '0')
{
this->AddChars(t, c, &l, &c);
}
Moreover, it incorrectly always sums
t
with
b
(it should sum l with b when carry occurs).
In order to avoid cumbersome carry propagation, I would suggest to define
char NumberString::AddCharsWithCarry( char first, char second, char & carry)
{
char r = first - '0' + second - '0' + carry;
if ( r > '9')
{
r -= 10;
carry = '1';
}
else
{
carry = '0';
}
return r;
}
This method sums
first
,
second
and carry
, returning the result (and updating the
carry
parameter). This way the loop inside your
Add
method would become very simple:
for (size_t i = 0; i < n; i++)
{
t = top.at(n - i - 1);
b = i < m ? bottom.at(m -i -1) : '0';
l = AddCharsWithCarry(t, b, c);
sum.insert(0, 1, l);
}