| | |
high digit numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
•
•
Join Date: Mar 2008
Posts: 31
Reputation:
Solved Threads: 0
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.
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.
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.
•
•
Join Date: Jul 2005
Posts: 1,692
Reputation:
Solved Threads: 267
>>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.
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.
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++:
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <sstream> int ToInt(const char *s) { std::istringstream is(s); int result; is >> result; return result; } int main() { int x = ToInt("2"); std::cout << x * x << '\n'; }
If at first you don't succeed, keep on sucking until you do succeed.
![]() |
Similar Threads
- beginner in mips, stuck on program please help! (Assembly)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- array help (C)
- Game ON (Geeks' Lounge)
- Psycho Hose Beast IP question (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: sending bitmap
- Next Thread: Drawing Text in OpenGL
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg simple sorting string strings temperature template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






