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.

Recommended Answers

All 10 Replies

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.

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

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.

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? :-/

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.

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?

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.

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

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.

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

Just write a big # library using the STL string type. Don't rely on silly intrinsic features.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.