943,527 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2819
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 15th, 2008
0

high digit numbers

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
scream2ice is offline Offline
33 posts
since Mar 2008
Aug 15th, 2008
0

Re: high digit numbers

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.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008
Aug 15th, 2008
0

Re: high digit numbers

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
scream2ice is offline Offline
33 posts
since Mar 2008
Aug 15th, 2008
0

Re: high digit numbers

>but how do u think u'd try to answer this problem:

By first, trying to do it yourself...
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 15th, 2008
0

Re: high digit numbers

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.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008
Aug 15th, 2008
0

Re: high digit numbers

>>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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Aug 16th, 2008
0

Re: high digit numbers

I'm trying to convert from char* to int. Is there an easy way to do that?
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008
Aug 16th, 2008
0

Re: high digit numbers

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++:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008
Aug 16th, 2008
0

high digit numbers

atoi( char to int)

or

itoa ( int to char)
Gel
Reputation Points: 14
Solved Threads: 3
Newbie Poster
Gel is offline Offline
19 posts
since Dec 2006
Aug 16th, 2008
0

Re: high digit numbers

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: sending bitmap
Next Thread in C++ Forum Timeline: Introducing myself and a n00b help question. ;)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC