1

可能重复:
如何用 BrainFuck 计算 2 个数字的总和

有谁知道如何编写一个简单的 BF 程序,将两个一位数相加?我是这门语言的新手,需要一些帮助来掌握这些概念。

4

1 回答 1

6

如果您有两个单元格,每个单元格的值都为 0 到 9,您可以将一个单元格添加到另一个单元格中。假设您有两个单元格 A 和 B。A 位于位置 0,B 位于位置 1。您可以像这样将 B 添加到 A(假设指针从 A 开始)。我将 A 设置为 4,B 设置为 8,然后将 B 添加到 A:

setting A and B
++++>++++++++

remember the pointer is at B now so we can add B to A like this
[<+>-]
and now the pointer is still at B but B contains 0 and A contains 12

如果您想让用户输入这些单个数字,请记住,当您使用 , 字符时,该字符的 ASCII 代码将放在当前单元格中。所以你首先需要从数字中减去 48(48 是字符 '0' 的 ASCII 码)。这是一个用键盘上的两个字符填充 A 和 B 的示例(我假设用户只按下任何数字键,而不是字母或符号)

Pointer starts at A so we have the user press a number key
,

we then subtract 48 from it so that it contains the actual value
------------------------------------------------

we move to B and do the same
>,------------------------------------------------

from here on it's the same as the last example
[<+>-]
于 2012-11-30T13:58:24.137 回答