high digit numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: high digit numbers

 
0
  #11
Aug 16th, 2008
Some remarks:
The C library is a standard part of the C++ library.
In particular, the atoi() function is declared in <cstdlib> C++ library header.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 31
Reputation: scream2ice is an unknown quantity at this point 
Solved Threads: 0
scream2ice scream2ice is offline Offline
Light Poster

Re: high digit numbers

 
0
  #12
Aug 17th, 2008
can u plz tell me how many parametres this atoi() function takes?
how do u use it?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: high digit numbers

 
0
  #13
Aug 17th, 2008
This is how atoi() is used, basically.
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. int main()
  5. {
  6. using namespace std;
  7.  
  8. int a = atoi("10");
  9.  
  10. cout << a << " squared is " << a * a << '\n';
  11. }
The basic use isn't enough unless you can guarantee that the string represents both an integer value and an integer value that fits in the range of int. If you can't guarantee that, atoi() doesn't do a good job of handling those kinds of errors.

One problem with atoi() is that it returns 0 if an error occurs. 0 is a valid integer so unless you want to ban 0 in your program, you can't reliably use it as an error indicator.

Another problem is that if the number can't be represented by int, the behavior of atoi() is undefined and there's no way around it without pre-validating the string.

The end result is that a robust use of atoi() looks more like this:
  1. int main()
  2. {
  3. using namespace std;
  4.  
  5. char *s;
  6.  
  7. // Initialize s and fill it with a string
  8.  
  9. if (IsInt(s)) {
  10. int a = atoi(s);
  11.  
  12. cout << a << '\n';
  13. }
  14. }
Where IsInt() is a function you write that validates the string to make sure the represented value fits in an int. The other ways of converting a char* to int may be more verbose, but they're also more user friendly when it comes to error handling.

Edward recommends that you only use atoi() when there's a 100% guarantee that the string represents a valid int and no error handling will ever be necessary.
Last edited by Radical Edward; Aug 17th, 2008 at 9:01 am.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 31
Reputation: scream2ice is an unknown quantity at this point 
Solved Threads: 0
scream2ice scream2ice is offline Offline
Light Poster

Re: high digit numbers

 
0
  #14
Aug 17th, 2008
thanks alot for all the replies
but i'm still not convinced that even with this function i'd be able to work with high digit numbers!
i guess it's just the c++ weakness in this case!
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: high digit numbers

 
1
  #15
Aug 17th, 2008
By the looks of it, you're supposed to manually manage the numbers. Edward's first attempt would probably use strings to store the numbers and then handle the arithmetic just like it's taught in elementary school with digit by digit calculations and saving of the carry or borrow values.

That's the simple and easy to write solution, but it's not optimal in terms of speed or memory footprint.
Do as what Edward has said and you will able to solve your problem easily. You don't actually need integer to store number, you can just use string to represent the number. First of all, you create one string to store the input which is in the format you have mentioned before:
  1. first_number[+ or -]second_number
Then create 2 other strings: the first string stores the first number, and the second string stores the second number. Then calculate it like what you have taught in your elementary school.

For example:
User's Input:  162823+562369
-> First String = 162823
-> Second String = 562369

Calculate it manaully like you would calculate by hand.

 1     1   1
 1 6 2 8 2 3  
                         +
 5 6 2 3 6 9
---------------
 7 2 5 1 9 2
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC