|
From: Jason H. <jas...@bt...> - 2002-03-12 18:01:36
|
Hi Samuel,
Looking at your code it shows you understand what is happening to the
numbers when you try to reverse them, but I thought I'd show you a shorter
example that does the same thing. I'm not doing it to be harsh, I just hope
you can learn from it in some way:
// This loop performs a mathematical reverse in specified base
while(inputNumber > 0)
{
editNumber *= base;
editNumber += inputNumber % base;
inputNumber /= base;
}
In your example base would be set equal to 10, but it does work in any other
base assuming you have a way to input and output numbers in that base to the
screen. Which I do in the attached program.
In effect I am treating the two numbers as stacks, if you know anything
about those containers. I am popping a number off the end of input number
with modulus base. Then shifting the next number down into the units
possition with the divide by base. I am also pushing that number onto the
edit number (adding) and shifting it up with the multiplication by base. The
first time around editNumber is 0, so multiplying it by base doesn't do
anything, so is redundant but negligable.
Jason.
|