How can I work with VeryLongIntegers with 100 or less digits (positive) in Microsoft Visual C++ without using external classes?
Thank you
Actually:
Input
The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).
The final input line will contain a single zero on a line by itself.
Output
Your program should output the sum of the VeryLongIntegers given in the input.
Sample Input
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output
370370367037037036703703703670
Please help, How can I work with VeryLongIntegers with 100 or less digits (positive) in C++?
Danny, you can't work directly with the "very long int." The longest integer in VC is unsigned long long which is only 64 bits and much much less than your max 100 decimal value.
What the assignment is asking for you to do is to store your values as character strings and write your own addition routine--place by place. This means looking at the ones, tens, hundreds, thousands, etc. place in each string and performing addition and carries.
This shouldn't be too difficult; however, it'll take some thought. Try to come up with an algorithm and edit your question or ask another if you have any problems. Good luck.
Reply:I think you can use long keyword to handle long integers.
but i am not too sure about the range of long.
eg : {
long int k=10921083083018301803810830;
or you can use a long integer array to store all digits of the number as separate digits then find the sum.
bye
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment