Link to home
Start Free TrialLog in
Avatar of tonitop
tonitop

asked on

Large numbers (20 digits)

I have string like "12345678901234567890" and I would have to convert
it to number, add one to it and convert it back to text. I can
probably come up with a complex way of doing it, but since someone has
already invented the wheel, I would like to know how this should be
done.

Somehow I had the imprsession that e.g. Roguewave's class library
(Tools.h) offers some kind of solution to this, but I couldn't find
anything from it. I believe it could be in Math++, but as I couldn't find any good documentation from Roguewave's site, I don't know if it is in it and if it is, what method it would be.
Avatar of ntdragon
ntdragon

you can do two thing

1)you can use strings as numbers what i mean is you"ll have to write all the operations for strings like <+,-,*,/,%>
and then you"ll have large numbers

2)you can take your string breake it to ints then do the opertion on them and build back the string

i would use the frist one i did it in the past in pascal it's not that hard
Avatar of tonitop

ASKER

Thanks.

Those were the options I already know that can be done (not sure how), but I'm looking easier way :) I'm not making class library for handling large numbers and I only need this functionality in one function, so I'm not really interested in how it is implemented as long as it works.

IMO in OO Programming one should try to use code written by others and not spend time reinventing the wheel (which would become quite angulate :), especially if the problem is just a small part of the whole program. Of course doing something yourself teaches you, but I have to draw the line somewhere and try to learn things that are more important to my current projects.
If you want only to add one (or two :) ) to this number, you can add it to charcode of last digit in it. And then if last digit bigger than '9', set it to zero and add 1 to previous digit.
Something like this:

last = strlen(str_number);
str_number[last]++;
while( str_number[last] > '9' )
{
  str_number[last] = '0';
  last--;
  str_number[last]++;
}
 
i think you still can use my first idea
if you want to use only operator + it very easy to do

it like:

 123
+
 999
----
1122

you sum the last entry and move 1 or 0 to the next entry
make that_i64toa()- sorry...
oops - looks like the original comment got dropped -

If you have __int64 support (Microsoft specific), you could use _atoi64() and _i64toa() to do it...
Avatar of tonitop

ASKER

No Microsoft in HP-UX 11 :)
HP-UX has LARGE_INTEGER, though, I believe... it's a quadword type of struct. As for API support, you would check the OS docs for that. Various flavors of UNIX treat this differently... check into that and let me know if that helps...
ASKER CERTIFIED SOLUTION
Avatar of inpras
inpras

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial