math - 如何在brainfuck中读取多位数字

标签 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

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

最佳答案

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

它包含乘法算法和 IF 条件以及 bool 比较(例如,检查用户是否按下了 Enter [character 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

这里有一些关于如何创建 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 ]

关于math - 如何在brainfuck中读取多位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8898680/

相关文章:

php - 查找 "best fit"方程

c++ - 3d 相机 - 如何改变相机方向?

javascript - 视差算法不适用于固定元素

add - 我的初学者 Brainfuck 添加程序有什么问题?

C# Hashtable.cs 没有所有素数

javascript - PHP 的 sprintf 函数的 JavaScript 等价物是什么?

brainfuck - Brainfuck Hello World 实际上是如何工作的?

c - 这将更快地编译和/或计算斐波那契数列的前 100 个数字 : C or Brainfuck

Brainf 中的 GOTO 指令***

javascript - 如何制作一个功能齐全的 Brainf*ck 解释器?