3

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.

Cam
  • 14,930
  • 16
  • 77
  • 128
  • 1
    you're writing brainfuck code and are concerned about user-friendliness?? – msw Jun 24 '10 at 19:23
  • Wouldn't enter be a reasonable char to identify the end of input? – Mau Jun 24 '10 at 19:29
  • @Mau: That's a good idea. Not sure why that never came to mind. What about this? http://en.wikipedia.org/wiki/Brainfuck#End-of-file_behavior ...just came accross that. I'm looking at the part that says you should set the cell to 0 before taking in input, and that if the cell is still 0, that means end of input on most implementations. Does that seem like a good way to go about this? – Cam Jun 24 '10 at 19:41
  • @incrediman: Umm, EOF in console input is less user-friendly because it requires the user to press Ctrl-Z or Ctrl-D. – Mau Jun 24 '10 at 20:13
  • @Mau yeah, nvm. I think you're right; newline's probably the best way to go about this. Thanks! – Cam Jun 24 '10 at 20:41
  • Can the downvoter please explain their reasoning? Thanks! – Cam Jun 25 '10 at 21:26

2 Answers2

2

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)?

Mau
  • 14,234
  • 2
  • 31
  • 52
0

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
Community
  • 1
  • 1
Dheeraj Ram
  • 537
  • 6
  • 13