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
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.
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.
>>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.
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.
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.
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.