How to get a large number as input and store it in memory

I know that doing arithmetic on large integers to the brain, although perhaps quite tedious at times, is quite possible.

However, what I'm interested in is that generally acceptable methods are best suited for inputting large integers (or even strings, as I suppose) as input.

Most compilers / interpreters allow you to immediately enter full lines as input (and then each character is read individually with ,). But I wonder how this can be done if you do not know when the input stream will stop? I guess one way is to tell the user to add a specific character / character string to their number to indicate that it has ended, but this seems a bit unacceptable.

I would prefer an answer that retains mobility in mind (specific solutions for implementation are of interest, but are not the main focus of this issue). If there is no fully realizable - agnostic method for this, then the one that will work on most implementations and unsuccessfully elegantly will be the next best.

Thanks in advance.

+3
source share
2 answers

Most languages ​​allow you to read a line from input (e.g. gets () in C, ReadLine () in C #, etc.). Why not ask the user to enter each value as a string (i.e., Separate by input)?

+2
source

In fact, I posted the same code for another question for a different purpose. Here, the following code will continue to accept ASCII of what you type if a newline is not found. Then prints what you typed.

Do not worry about portability ; I have already implemented adding two n-digit numbers with this number reading strategy, you can find here .

> +
[ - >,>+< 
  ----- -----    ; minus 10
  [              ; if enters means it is not a \n
    +++++ +++++  ; restore prev value
    < 
  ] >>           ; moving forward
]
                 ; numbers are 0 0 49 0 50 0 51
                 ; for input 123
<<<<[<<]         ; moving to the beginning
>>               ; reaching first char
[.>>]            ; just printing till end
0
source

Source: https://habr.com/ru/post/1751644/


All Articles