Solved

Large numbers (20 digits)

Posted on 2000-04-09
9
197 Views
Last Modified: 2010-04-02
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.
0
Comment
Question by:tonitop
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 1

Expert Comment

by:ntdragon
ID: 2699190
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
0
 

Author Comment

by:tonitop
ID: 2699203
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.
0
 
LVL 2

Expert Comment

by:Serega
ID: 2699391
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]++;
}
 
0
 
LVL 1

Expert Comment

by:ntdragon
ID: 2699609
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:captainkirk
ID: 2701394
make that_i64toa()- sorry...
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2701404
oops - looks like the original comment got dropped -

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

Author Comment

by:tonitop
ID: 2702787
No Microsoft in HP-UX 11 :)
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2704096
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...
0
 
LVL 4

Accepted Solution

by:
inpras earned 100 total points
ID: 2705237
Hi check this out this works fine for me
      unsigned __int64 i;
      char mystring[100];
      ZeroMemory(mystring, 100);
      strcpy(mystring, "12345678901234567890");
      i = _atoi64(mystring);
      i = i+1;
      _ui64toa(i, mystring, 10);

Hope this helps
inpras
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now