Also, your reasoning is a little long-winded.
────────────────────────
You only want to switch (from addition to subtraction) or (from subtraction to addition) if the
sign of the numbers are different. A little algebra:
(+a) + (-b) == (+a) - (+b)
(-a) + (+b) == (-a) - (-b)
You are used to this from elementary school: 4 + -5 is the same as 4 - 5.
Notice that only the sign of
b changed when we switch from addition to subtraction? The sign of
a did not matter. (Well, except that it was different from the sign of
b.)
────────────────────────
If the signs of the numbers are
the same, then you are set to continue. The only thing left to consider is the
magnitude of
a must be greater than or equal to the magnitude of
b.
Be careful when checking magnitude. A gross indicator of magnitude is the
length of the number, but if the lengths are equal you must compare the most significant digits to see which is greater. For example:
123 ≥ 79 True because '123' is longer than '79'
941 ≥ 284 True because '9' is greater than or equal to '2'
For subtraction, if this condition does not hold, then you need to reorder your numbers and compute the answer recursively, then adjust the sign of the result appropriately. For example, since:
28 < 93
In order to compute 28 - 93 you must reorder it to -(93 - 28). Algebra saves the day again. :-)
────────────────────────
BTW, once you've got it figured out, check out my
Bignum Challenge. I'd like to see your
LongInt class there.
http://www.cplusplus.com/forum/lounge/32041/
Hope this helps.