如何在brainfuck中读取多位数字

ann*_*sly 15 math brainfuck

我想用bf读取任意数字的数字.如果我手动设置,我知道如何读取正确的位数,如下所示:

,>,>, 2 Read in 3 digits
<< 0
--------
--------
--------
--------
--------
-------- 45 decrements
> 1
--------
--------
--------
--------
--------
--------
> 2
--------
--------
--------
--------
--------
--------

[>+<-]< 1 Copy digit 3 to cell 3

[>>++++++++++<<-]< Copy 10 * digit 2 to cell 3

Copy 100 * digit 1 to cell 3
[>>>>++++++++++ 4
    [<++++++++++>-] 4
<<<<-]>>> 3

>++++++++++..< Add 2 line breaks

., Print and Pause
Run Code Online (Sandbox Code Playgroud)

但我宁愿能够设置一个数字cell 0然后自动乘以每个数字的正确次数.我最好做什么?

Ced*_*amo 1

这个链接应该很有帮助:http ://esolangs.org/wiki/brainfuck_algorithms

它包含乘法算法、IF 条件以及布尔比较(例如,检查用户是否按下 Enter [字符 10] 来结束输入。)

然后你要做的就是这样(我将编写一些伪代码,然后由你使用那里描述的算法来实现它)。我会告诉您还给出了如何在最后实现 while 循环的伪代码,因为该循环不包含在该页面中(但仍然非常简单......相对而言)。当您能够准确理解每个角色在做什么时,您一定会感到惊讶:D。无论如何,这里是:

你需要两个单元格 A 和 B

move to B
input a character
while B is not equal to 10 (the newline character) then
    subtract 48 from B ('0' is character 48, so if we subtract 48 from any digit entered we should get its value. Of course this assumes that the user only presses digit keys or enter. I'll leave it as an exercise to you to do error checking)
    multiply A by 10
    add B to A (you can just move B to A like this [<+>-] since you will not need B's value anymore)
    move to B
    input a character
Run Code Online (Sandbox Code Playgroud)

这里有一些关于如何创建 while 循环的信息。假设您有以下代码while (condition) {body}:我假设您设法使用我之前给您的链接来实现该条件的代码。您需要一个单元格来存储条件结果,我将其称为C

execute condition and store result in C
start loop using [[-] (start the loop and immediately clear C)
    execute loop body
    execute condition and store result in C
end loop using ]
Run Code Online (Sandbox Code Playgroud)