| | |
storing large numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2004
Posts: 5
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Aug 2004
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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 workhave 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.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> #include <cstring> using namespace std; #include <fstream.h> void Simulation(); void Reverse( int * ); void PrintNums( int * ); void NullifyMemory( char *, char * , char *, int * ); void FillArrays( char *, char *, char * ); void ComputeSum( char *, char *, int * ); enum Restrictions { FRestriction = 50, SRestriction, TRestriction = 101 }; const int ModFactor = 10; ifstream infile("E:\\largenum.txt"); int main() { Simulation(); system("PAUSE"); return 0; } void Simulation() { char nums[ TRestriction ] = { '\0' }; char num1[ FRestriction ] = { '\0' }; char num2[ FRestriction ] = { '\0' }; int sum[ SRestriction ] = { 0 }; while ( infile.getline( nums, TRestriction, '\n' ) ) { FillArrays( nums, num1, num2 ); ComputeSum( num1, num2, sum ); Reverse( sum ); PrintNums( sum ); NullifyMemory( nums, num1, num2, sum ); } } void Reverse( int sArr[] ) { int temp = 0; int limit = FRestriction; for ( int i = 0; i < SRestriction / 2; ++i ) { temp = sArr[ i ]; sArr[ i ] = sArr[ limit ]; sArr[ limit-- ] = temp; } } void PrintNums( int outArr[] ) { bool FoundNonZero = false; for ( int i = 0; i < SRestriction; i++ ) { if ( outArr[ i ] > 0 ) FoundNonZero = true; if ( FoundNonZero ) cout << outArr[ i ]; } cout << endl; } void NullifyMemory( char _nums[], char _num1[], char _num2[], int _sum[] ) { for ( int i = 0; i < TRestriction; i++ ) _nums[ i ] = '\0'; for ( int i = 0; i < FRestriction; i++ ) _num1[ i ] = _num2[ i ] = '\0'; for ( int i = 0; i < SRestriction; i++ ) _sum[ i ] = 0; } void FillArrays( char _nums[], char _num1[], char _num2[] ) { int sizeString = strlen( _nums ); int i = 0; for ( i = 0; i < FRestriction && _nums[ i ] != ','; i++ ) _num1[ i ] = _nums[ i ]; i++; int j = 0; while ( i <= sizeString && _nums[ i ] != '\n' ) _num2[ j++ ] = _nums[ i++ ]; } void ComputeSum( char _num1[], char _num2[], int _sum[] ) { int num1Counter = strlen( _num1 ); int num2Counter = strlen( _num2 ); int MaxSize = (num1Counter > num2Counter ) ? num1Counter :num2Counter; bool CarryOver = false; int tempSum = 0; int walker = 0; int s1 = 0; int s2 = 0; for ( walker = 0; walker < MaxSize; walker++ ) { if ( ( num1Counter - 1 ) >= 0 ) s1 = _num1[ num1Counter-- - 1 ] - '0'; //Convert string to int else s1 = 0; if ( ( num2Counter - 1 ) >= 0 ) s2 = _num2[ num2Counter-- - 1 ] - '0'; //Convert string to int else s2 = 0; tempSum = s1 + s2; if ( CarryOver ) { _sum[ walker ] = ( ( tempSum % ModFactor ) + 1 ) % ModFactor; if ( tempSum != 9 ) CarryOver = !CarryOver; } else _sum[ walker ] = ( tempSum % ModFactor ); CarryOver = ( tempSum > 9 ) ? true : CarryOver; } if ( CarryOver ) _sum[ walker ] = 1; }
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.
•
•
Join Date: Aug 2004
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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.
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.
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.
•
•
Join Date: Aug 2004
Posts: 5
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- help, large numbers (C++)
- Store large numbers in C (C++)
Other Threads in the C++ Forum
- Previous Thread: ambigious part 5
- Next Thread: counting comparisons when sorting
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





