User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,600 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,438 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 4786 | Replies: 10
Reply
Join Date: Aug 2004
Posts: 5
Reputation: Mr A is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Mr A Mr A is offline Offline
Newbie Poster

Help storing large numbers

  #1  
Aug 5th, 2004
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2004
Posts: 5
Reputation: The Code Poet is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
The Code Poet The Code Poet is offline Offline
Newbie Poster

Re: storing large numbers

  #2  
Aug 5th, 2004
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.

#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.
Reply With Quote  
Join Date: Aug 2004
Posts: 5
Reputation: Mr A is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Mr A Mr A is offline Offline
Newbie Poster

Re: storing large numbers

  #3  
Aug 5th, 2004
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
Reply With Quote  
Join Date: Aug 2004
Posts: 5
Reputation: The Code Poet is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
The Code Poet The Code Poet is offline Offline
Newbie Poster

Re: storing large numbers

  #4  
Aug 5th, 2004
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.
Reply With Quote  
Join Date: Aug 2004
Posts: 5
Reputation: Mr A is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Mr A Mr A is offline Offline
Newbie Poster

Re: storing large numbers

  #5  
Aug 5th, 2004
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?
Reply With Quote  
Join Date: Aug 2004
Posts: 5
Reputation: The Code Poet is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
The Code Poet The Code Poet is offline Offline
Newbie Poster

Re: storing large numbers

  #6  
Aug 5th, 2004
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.
Reply With Quote  
Join Date: Aug 2004
Posts: 5
Reputation: Mr A is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Mr A Mr A is offline Offline
Newbie Poster

Re: storing large numbers

  #7  
Aug 5th, 2004
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?
Reply With Quote  
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation: Chainsaw is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: storing large numbers

  #8  
Aug 5th, 2004
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.
Reply With Quote  
Join Date: Aug 2004
Posts: 5
Reputation: Mr A is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Mr A Mr A is offline Offline
Newbie Poster

Re: storing large numbers

  #9  
Aug 5th, 2004
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
Reply With Quote  
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation: Chainsaw is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: storing large numbers

  #10  
Aug 5th, 2004
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:02 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC