Splitting out large numbers

This is a discussion on Splitting out large numbers within the C++ Programming forums, part of the General Programming Boards category; So I ran into a problem the other day and need to separate out a large number into small number ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    20

    Splitting out large numbers

    So I ran into a problem the other day and need to separate out a large number into small number groups for example:

    945689234989910

    needs to be split into

    9456|8923|498|9910

    and each group needs to be stored

    Anyone have any ideas?
    Thanks in advance

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    No code on your part means no code on my part.

    However You could look into:
    the string class
    the substr method
    and a simple for loop

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Read it as text then convert the strings into integer values as needed.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    Could it be more easily done with division and modulus?

  5. #5
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Yes substr method would be the easiest way to do it. Read input as text, then use substr method of string. then convert evry slice using atoi function(or is there a C++ version?).

  6. #6
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Could it be more easily done with division and modulus?
    Yes, very easily.

    Code:
    #include <iostream>
    
    using namespace std;
    int main()
    {
    
    	int test;
    	test = 12345;
    
    	cout<< endl<< test%10 << " Last digit";
    	cout<< endl<< test%100 << " Last two digits";
    	cout<< endl<< test%1000 << " Last three digits" << endl;
    	//ect
    
    return 0;
    }

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yes, very easily.
    Ok give me the middle the 3rd set of 3 digits in:
    945689234989910
    probably won't be as easy as: str.substr(6,3);

    The biggest concern I have about trying to keep everything as integral types is that:
    945689234989910 is a lot larger than a 32 bit int can handle. So if you are on a system with that limitation then using the int datatype isn't going to work.

  8. #8
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Bah, I did not even see the "more" part of what I quoted! Just showing it could be done.
    It would not be very hard to pick the different parts out with that method; but your method is easier. I was just confirming with him that it could be done how he suggested.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    So Enahs how could you pick the different parts out?

    Thanks everyone for all the help

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Using division / modulus is easist done when working from low values to high values:
    Code:
    int main()
    {
      unsigned num = 123456789;
      int i = 1000; // We want to print them in sets of 3 digits
      while ( num > 0 )
      {
        cout << (num % i) << endl;
        num /= i;
      }
    }
    I'll leave it to you to figure out how to go from high to low (there are several methods)

  11. #11
    Registered User rogue's Avatar
    Join Date
    Oct 2005
    Posts
    9

    Array?

    You could use a double char **. Sigh, it's a thought

    Rogue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to deal wth large numbers
    By bvgsrs in forum C++ Programming
    Replies: 2
    Last Post: 06-18-2007, 06:16 AM
  2. Handling Large Numbers
    By Xzyx987X in forum C Programming
    Replies: 2
    Last Post: 05-03-2004, 02:33 PM
  3. Using really big numbers!
    By Machewy in forum C++ Programming
    Replies: 11
    Last Post: 02-26-2004, 09:49 AM
  4. Help needed with VERY large numbers.
    By jwarner in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2004, 11:01 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21