Cam
Cam

Reputation: 15234

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

I know that performing arithmetic on large integers in brainfuck, while perhaps quite tedious at times, is entirely possible.

However what I'm wondering about is what the generally acceptd best-practices are for taking in large integers (or even strings, I suppose) as input.

Most compilers/interpreters allow you to provide full strings at once as input (and then each character is read in individually with a ,). But what I'm wondering is this - how can you read one in if you don't know when the input stream is going to stop? I suppose one way is to tell the user to append a certain character/string of characters to their number to indicate that it's over, but that seems a bit non-user-friendly.

I'd prefer an answer that keeps portability in mind (implementation-specific solutions are of interest, but are not the primary focus of this question). If there is no completely implementation-agnostic way to do this, one that will work on most implementations and fail gracefully otherwise would be the next best thing.

Thanks in advance.

Upvotes: 4

Views: 281

Answers (2)

Dheeraj Ram
Dheeraj Ram

Reputation: 567

Actually I had posted the same code for a different question for a different purpose. Here following code will keep on accepting the ASCII of whatever you type unless a newline character is met.Then prints what you typed.

Don't worry about the portability; I've already implemented addition of two n-digit numbers with this strategy of reading numbers, 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

Upvotes: 1

Mau
Mau

Reputation: 14478

Most languages let you 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 line (i.e. separated by enter)?

Upvotes: 2

Related Questions