16

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、各桁に適切な回数を自動的に乗算できるようにしたいと思います。私は何をするのが最善でしょうか?

4

2 に答える 2

1

このリンクは非常に役立つはずです: http://esolangs.org/wiki/brainfuck_algorithms

これには、乗算のアルゴリズムと IF 条件、およびブール比較 (たとえば、ユーザーが [文字 10] を押して入力を終了したかどうかを確認するため) が含まれています。

次に、あなたがすることはこれです(私はいくつかの疑似コードを書きます。そこで説明されているアルゴリズムを使用してそれを実装するのはあなた次第です)。そのページには含まれていないため、最後にwhileループを実装する方法に関する擬似コードも提供します(ただし、それでもかなり単純です...比較的)。それぞれのキャラクターが何をしているかを正確に理解できたら、きっと驚くことでしょう :D. とにかく、ここに行きます:

AとBの2つのセルが必要です

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 ]
于 2012-11-27T12:09:50.777 に答える
0

このプログラムは n 桁の数字を読み取り、それをそのまま出力します。n 桁の数値を維持するための最良の方法は、ascii をシーケンスとしてテープに保存することです。

> +
[ - >,>+< 
  ----- -----    ; 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
于 2013-10-18T16:00:12.653 に答える