2012-01-17 16 views
15

bfで任意の桁数の数字を読みたいと思います。brainfuckで複数桁の数字を読み取る方法

,>,>, 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に番号を設定することができるだろうし、自動的用回の右の数を掛け:私はこのように、手動で設定する場合の桁の正しい数で読み方を知っています各桁。私は何をするのが最善だろうか?

答えて

1

は、このリンクは非常に助けになるはずです:http://esolangs.org/wiki/brainfuck_algorithms

それはどうかをチェックする(乗算ともIF条件だけでなく、ブール比較するためのアルゴリズムが含まれ、例えば、ユーザーが終了し、[文字10]入力を押します入力します。

これは何ですか(私はいくつかの擬似コードを記述し、そこに記述されているアルゴリズムを使用して実装します)。私はあなたにも、そのページには含まれていないので、whileループを実装する方法について擬似コードを与えることを教えてくれるでしょう(それにもかかわらず、かなり単純です)。それぞれのキャラクターが何をしているのかを正確に理解すると、間違いなく驚くでしょう。D。とにかく、ここに行く:

次の2つのセル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}。私は以前あなたに与えたリンクを使って条件のコードを実装することができたと思います。あなたは、私はこのプログラムは、n桁の数字を読み、そのように印刷することです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 ] 
0

と呼ぶことにしますこれは、条件の結果を格納するセルを必要としています。 常にn桁の数字を保持する最善の方法は、アスキーをテープにシーケンスとして保存することです。

> + 
[ - >,>+< 
    ----- ----- ; 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 
+0

私はhttps://copy.sh/brainfuck/でかつhttps://sange.fi/esoteric/brainfuck/impl/interp/i.htmlでこのオンラインを試験した場合、両方のサイトが停止することができませんでした。 – JSideris

関連する問題