943,568 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7613
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 5th, 2004
0

storing large numbers

Expand Post »
I need some help, heh, i have to store a twelve digit number somewhere... as i've understood it nothing like unsigned int or long double or anything won't work have i missed something? or can anyone point me to anything which i can use to be able to do this?... i have to be able to performe calculations on that number too...
i'm using c++ by the way.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Mr A is offline Offline
5 posts
since Aug 2004
Aug 5th, 2004
1

Re: storing large numbers

Quote originally posted by Mr A ...
I need some help, heh, i have to store a twelve digit number somewhere... as i've understood it nothing like unsigned int or long double or anything won't work have i missed something? or can anyone point me to anything which i can use to be able to do this?... i have to be able to performe calculations on that number too...
i'm using c++ by the way.
Not the most trivial program. We had a similiar problem like this in a programming content many years ago. I still have the source, so take a long, and it should help. It stores big numbers, up to 100 numbers if I remember correctly. There is no type for what you want to do. You gotta use strings.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstring>
  4. using namespace std;
  5. #include <fstream.h>
  6.  
  7. void Simulation();
  8. void Reverse( int * );
  9. void PrintNums( int * );
  10. void NullifyMemory( char *, char * , char *, int * );
  11. void FillArrays( char *, char *, char * );
  12. void ComputeSum( char *, char *, int * );
  13.  
  14. enum Restrictions { FRestriction = 50, SRestriction, TRestriction = 101 };
  15. const int ModFactor = 10;
  16. ifstream infile("E:\\largenum.txt");
  17.  
  18. int main()
  19. {
  20. Simulation();
  21.  
  22. system("PAUSE");
  23. return 0;
  24. }
  25.  
  26. void Simulation()
  27. {
  28. char nums[ TRestriction ] = { '\0' };
  29. char num1[ FRestriction ] = { '\0' };
  30. char num2[ FRestriction ] = { '\0' };
  31. int sum[ SRestriction ] = { 0 };
  32.  
  33. while ( infile.getline( nums, TRestriction, '\n' ) )
  34. {
  35. FillArrays( nums, num1, num2 );
  36. ComputeSum( num1, num2, sum );
  37. Reverse( sum );
  38. PrintNums( sum );
  39. NullifyMemory( nums, num1, num2, sum );
  40. }
  41. }
  42.  
  43. void Reverse( int sArr[] )
  44. {
  45. int temp = 0;
  46. int limit = FRestriction;
  47. for ( int i = 0; i < SRestriction / 2; ++i )
  48. {
  49. temp = sArr[ i ];
  50. sArr[ i ] = sArr[ limit ];
  51. sArr[ limit-- ] = temp;
  52. }
  53. }
  54.  
  55. void PrintNums( int outArr[] )
  56. {
  57. bool FoundNonZero = false;
  58.  
  59. for ( int i = 0; i < SRestriction; i++ )
  60. {
  61. if ( outArr[ i ] > 0 )
  62. FoundNonZero = true;
  63. if ( FoundNonZero )
  64. cout << outArr[ i ];
  65. }
  66. cout << endl;
  67. }
  68.  
  69. void NullifyMemory( char _nums[], char _num1[], char _num2[], int _sum[] )
  70. {
  71. for ( int i = 0; i < TRestriction; i++ )
  72. _nums[ i ] = '\0';
  73. for ( int i = 0; i < FRestriction; i++ )
  74. _num1[ i ] = _num2[ i ] = '\0';
  75. for ( int i = 0; i < SRestriction; i++ )
  76. _sum[ i ] = 0;
  77. }
  78.  
  79. void FillArrays( char _nums[], char _num1[], char _num2[] )
  80. {
  81. int sizeString = strlen( _nums );
  82. int i = 0;
  83. for ( i = 0; i < FRestriction && _nums[ i ] != ','; i++ )
  84. _num1[ i ] = _nums[ i ];
  85. i++;
  86.  
  87. int j = 0;
  88. while ( i <= sizeString && _nums[ i ] != '\n' )
  89. _num2[ j++ ] = _nums[ i++ ];
  90. }
  91.  
  92. void ComputeSum( char _num1[], char _num2[], int _sum[] )
  93. {
  94.  
  95. int num1Counter = strlen( _num1 );
  96. int num2Counter = strlen( _num2 );
  97. int MaxSize = (num1Counter > num2Counter ) ? num1Counter :num2Counter;
  98. bool CarryOver = false;
  99. int tempSum = 0;
  100. int walker = 0;
  101. int s1 = 0;
  102. int s2 = 0;
  103.  
  104. for ( walker = 0; walker < MaxSize; walker++ )
  105. {
  106. if ( ( num1Counter - 1 ) >= 0 )
  107. s1 = _num1[ num1Counter-- - 1 ] - '0'; //Convert string to int
  108. else
  109. s1 = 0;
  110.  
  111. if ( ( num2Counter - 1 ) >= 0 )
  112. s2 = _num2[ num2Counter-- - 1 ] - '0'; //Convert string to int
  113. else
  114. s2 = 0;
  115.  
  116. tempSum = s1 + s2;
  117.  
  118. if ( CarryOver )
  119. {
  120. _sum[ walker ] = ( ( tempSum % ModFactor ) + 1 ) % ModFactor;
  121. if ( tempSum != 9 )
  122. CarryOver = !CarryOver;
  123. }
  124. else
  125. _sum[ walker ] = ( tempSum % ModFactor );
  126.  
  127. CarryOver = ( tempSum > 9 ) ? true : CarryOver;
  128. }
  129. if ( CarryOver )
  130. _sum[ walker ] = 1;
  131. }

The input file had the following format:

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX, XXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXX, XXXXXXXXXXXXXXXXXXXXXXXXXXXX

where X was any number. The two large numbers are delimited by a comma.
Have fun.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
The Code Poet is offline Offline
5 posts
since Aug 2004
Aug 5th, 2004
1

Re: storing large numbers

okay thanks for the help :-) i don't understand half of what that program does but i'll give it a try and try to find out :-) tho i thought there was some kind of file or something you could include for larger numbers support... bu really i have no idea :-) thanks again
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Mr A is offline Offline
5 posts
since Aug 2004
Aug 5th, 2004
0

Re: storing large numbers

Quote originally posted by Mr A ...
okay thanks for the help :-) i don't understand half of what that program does but i'll give it a try and try to find out :-) tho i thought there was some kind of file or something you could include for larger numbers support... bu really i have no idea :-) thanks again

It's possible that there is a 3rd party library file. My friend wrote one. But no such file is built into the language that I'm aware of.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
The Code Poet is offline Offline
5 posts
since Aug 2004
Aug 5th, 2004
0

Re: storing large numbers

well, heh, i need some more help :o what does that program really do? :-) i mean i put my large number into that file and tried to run it and it printed out the number.... but how do i perform calculations on that number?
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Mr A is offline Offline
5 posts
since Aug 2004
Aug 5th, 2004
0

Re: storing large numbers

Quote originally posted by Mr A ...
well, heh, i need some more help :o what does that program really do? :-) i mean i put my large number into that file and tried to run it and it printed out the number.... but how do i perform calculations on that number?
It adds two large numbers together.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
The Code Poet is offline Offline
5 posts
since Aug 2004
Aug 5th, 2004
0

Re: storing large numbers

ok thanks but that was not what i wanted to do ... i just need to store it so i can do calculations with it, such as sqrt and so on... anyone know how to do that?
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Mr A is offline Offline
5 posts
since Aug 2004
Aug 5th, 2004
0

Re: storing large numbers

Why can't you use a double?

double n = 123456789012.0;

A double on most machines is at least 64 bits, so there should be plenty of accuracy.

If you must use an integer, some systems (MS VC++ for example) support 64-bit integers. In VC it goes like this:

__int64 n = 123456789012i64;

note the i64 suffix on the literal value; if you left that off the compiler would complain about the number being to large for an integer.

Once you have your number in this format, you can use it in integer math.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Aug 5th, 2004
0

Re: storing large numbers

hmm... when i make it a double it says "error: integer constant is too large
for "long" type" are you sure about 64bits? well it doesn't work i'm running MinGW if that helps anything...

btw the __int64 gave the same errormessage
but hey it must be doable? ... i mean some kindof file i can include?
thanks for all the help anyways
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Mr A is offline Offline
5 posts
since Aug 2004
Aug 5th, 2004
0

Re: storing large numbers

Never tried mingw. You might look in your help files for '64' or something and see if they support 64 bit integers or floating point numbers.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: ambigious part 5
Next Thread in C++ Forum Timeline: counting comparisons when sorting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC