10

BrainFuck を使用して、最大 9 までの 2 つの数値を読み取り、それらの合計を計算して結果を出力できるプログラムを作成しようとしています。

BF 言語を理解しようとしているところですが、思ったよりも難しそうです。

4

7 に答える 7

33

言語を巨大なテープ (長さ 30K バイト) と考えてください。読み取り、書き込み、前後への移動、一度に 1 つのセルの増減が可能です (各セルは 1 バイトなので、実質的に 30K セルになります)。必要に応じて、バイト ストリームが (ASCII 形式で) 保持するものを読み書きできます。基本的な演算子を知っていると仮定すると、2 つの数値を合計するプログラムは次のようになります。

,       ; read character and store it in p1
>       ; move pointer to p2 (second byte)
,       ; read character and store it in p2
[           ; enter loop
    <       ; move to p1
    +       ; increment p1
    >       ; move to p2
    -       ; decrement p2
]           ; we exit the loop when the last cell is empty
<       ; go back to p1
------------------------------------------------ ; subtract 48 (ie ASCII char code of '0')
.       ; print p1
于 2012-05-20T03:24:43.510 に答える
0

また、1 桁のエントリと回答のみを処理できるプログラムも作成しました。

#Make the first cell (Cell 0) hold a value of 48 
>++++ ++++
[
<++++ ++
>-
]

#Get inputs and minus 48 from each to get Decimal 

,>,
<<
[
>-
>-
<<-
]

#Adds the contents of Cells 1 and 2


>
[
>+
<-
]

#Moves answer to Cell 0
>
[
<+
>-
]
<
[
<+
>-
 ]

#Converts answer to ASCII
>++++ ++++
[
<++++ ++
>-
]
<

[
<+
>-
]
<
#Print answer
.
于 2016-11-08T00:25:01.797 に答える