high digit numbers

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

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

high digit numbers

 
0
  #1
Aug 15th, 2008
hey all;
is there a conversion from "char" to "int" or to
"double" possible ? If yes, how? and if not, how do
we work with numbers that have more than 32 digits, for instance
a 500 digit number?

thanks
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
  #2
Aug 15th, 2008
C++ places hard limits on the size of the built in types. If you want something larger you need to use a library that supports big numbers, like GMP.
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
  #3
Aug 15th, 2008
thanks
but how do u think u'd try to answer this problem:

Problem code: ARITH
One part of the new WAP portal is also a calculator computing expressions with very long numbers.
To make the output look better, the result is formated the same way as is it usually used with manual
calculations.
Your task is to write the core part of this calculator. Given two numbers and the requested operation,
you are to compute the result and print it in the form specified below. With addition and subtraction,
the numbers are written below each other. Multiplication is a little bit more complex: first of all, we
make a partial result for every digit of one of the numbers, and then sum the results together.
Input
There is a single positive integer T on the first line of input (equal to about 1000). It stands for the
number of expressions to follow. Each expression consists of a single line containing a positive integer
number, an operator (one of +, - and *) and the second positive integer number. Every number has at
most 500 digits. There are no spaces on the line. If the operation is subtraction, the second number is
always lower than the first one. No number will begin with zero.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: high digit numbers

 
0
  #4
Aug 15th, 2008
>but how do u think u'd try to answer this problem:

By first, trying to do it yourself...
*Voted best profile in the world*
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
  #5
Aug 15th, 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.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 264
Lerner Lerner is offline Offline
Posting Virtuoso

Re: high digit numbers

 
0
  #6
Aug 15th, 2008
>>is there a conversion from "char" to "int"? If yes, how?

Yes, you can either go from char to int or from string to int.

One of the easiest way to get the numeric value of a char representing a digit is to substract the ASCII (or Unicode or whatever) value of the of zero from the corresponding value of the desired digit. This, of course, relies on the facts that a char is just an int in disguise, the applicable int value for a given char being encoded in the applicable conversion table--- ASCII, Unicode, whatever; and that the char values for numerical digits are always arranged in sequential, contiguous order within the table, meaning that the value for the char '0' is always 1 less than the value of '1' which is always 1 less than the value of '2', etc.

To convert an entire string to a numerical value you hve a variety of opitons including stringstreams, one of the strtox() functions such as strtol() or one of the atox() functions, such as atoi(), atol(), etc, in addition to others. As long as the resulting numerical value is within the acceptable range of values for C/C++ any of these options will work, though some are prefered to others.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: high digit numbers

 
0
  #7
Aug 16th, 2008
I'm trying to convert from char* to int. Is there an easy way to do that?
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
  #8
Aug 16th, 2008
One of your options is the stringstream family of classes. It's not the easiest way in Edward's opinion, but it's probably the best with standard C++:
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. int ToInt(const char *s)
  5. {
  6. std::istringstream is(s);
  7. int result;
  8.  
  9. is >> result;
  10.  
  11. return result;
  12. }
  13.  
  14. int main()
  15. {
  16. int x = ToInt("2");
  17.  
  18. std::cout << x * x << '\n';
  19. }
The good thing about this is that it can be generalized to work with other types so you don't have a ton of overloads each doing the same thing but with a different type.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 8
Reputation: Gel is an unknown quantity at this point 
Solved Threads: 2
Gel's Avatar
Gel Gel is offline Offline
Newbie Poster

high digit numbers

 
0
  #9
Aug 16th, 2008
atoi( char to int)

or

itoa ( int to char)
People will accept your ideas much more readily if you tell them Benjamin Franklin said it first.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: high digit numbers

 
0
  #10
Aug 16th, 2008
itoa is non standard and atoi is a much maligned function. However, the use of atoi is not an issue (since this is c++) you should just use stringstreams.
Last edited by iamthwee; Aug 16th, 2008 at 10:43 am.
*Voted best profile in the world*
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