Re: [問題] 1-9位數不重複印出來 (brainfuck, 附詳解)
※ 引述《mikemagic88 (Mikemagic88)》之銘言:
: 使用者輸入1 印1-9
: 使用者輸入2 印1-98 (11, 22, 33等重複的不印)
: 使用者輸入3 印1-987 (121, 988, 667等有重複的不印)
>+>,------------------------------------------------
>>+++++++++++++++++++++++++++++++++++++++++++++++++
<<<[->>>[>]<[><<[>><[->+>+<<]<[->+>-<<]>>>>+<<[[-]>>-<<]
<<>>>[-<<<+>>>]>[-<+>]<<<[->>>+<<<]<<]>>>>>[<<<<[->+<]>>>>
[-<<<<+>>>>]<[->+<]<>>>]<<<>>[-<+>]<<<[->>>+<<<]>>>[->+<]<<<<]
>>>>>[[-<<<<+>>>>]<<[->+<]>>>]<<<<>+>[[-]<->]<[-<<[.<]++++++++++
.---------->[>]>]<<[[->>+<<]<]>><+[->>+[-<+<+>>]<[->+<]+
<----------------------------------------------------------
[[-]>-<]>[-<+>>----------<]<[->+<]>>[-<<+>>]<]>>
[[-<<+>>]>]<<<[->+>+<<]>>[-<<+>>]+<-[[-]>-<]>
[-<<++++++++++++++++++++++++++++++++++++++++++++++++>>]
<<[[->>+<<]>+[-<+>]<<]>[->+<]>>[[-<<+>>]<[->+<]>>]<<<[<]
<[->>[>]>>+>+<<<<[<]<]>>[>]>>>[-<<<<[<]<+>>[>]>>>]<<[->-<]
>+[-<<<[<]<<[-]+>>>[>]>>]<<<<[<]<<]>>
最下面是說明, 嗯我寫的時候也是這樣一塊一塊想的.
# 後面的是註解,
然後混有數字的部分表示接下來的符號要重複 N 遍,
因為一長串同樣的符號會比較不好讀..
執行器是自己寫的 (用 haskell)
https://gist.github.com/CindyLinz/241f8296041ee3c9034a92ec1b5ac136
如果有裝 ghc 的話, 應該 ghc --make bf 就可以生出執行檔
用法是從 stdin 倒給它:
第一行輸入一個整數, 表示最大想用到的 memory 長度
第二行輸入的東西是 brainfuck 程式要讀的 input
(嗯我沒有考慮如果有換行符號要怎麼輸入, 反正這次不用 ^^|)
第三行開始就是 brainfuck 程式.. 可以輸入標準的 brainfuck 語法,
也可以混有 空白, #號, 數字.. 也就是下面這個說明版的語法也可以吃..
執行的時候會先印出符合 brainfuck 語法的程式再開始執行.
額外地, 如果程式碼裡面混有 @ 符號的話,
遇到的時候就會把 memory 和磁頭位置 dump 出來, 方便 debug
(可以在程式段落的開頭與結尾各放一個 @ 以觀察記憶體變化)
brainfuck 真是超難寫的, 我覺得比 Turing machine 還難搞 ~_~
它沒有 if, 只有 while, 而且沒有 break, continue 這些可以配合...
想弄個 && 或 || 或 < 都很麻煩..
寫到一半發現空間沒有預留夠, 要把整段資料往旁邊搬就要改好多好多 code.. orz
I've fucked my brain. I wish you would fuck yours too. XD
# format:
# zero continue_bit input_length zero target*(digits) zero
>+>,48->>49+<
# main loop
<<[->>
>[>]
# ne-cmp all diff:
# zero target* [zero] zero same_count(0) zero
# zero target* [zero] zero same_count zero zero
<[>
# ne-cmp left to all:
# zero target* answer [zero] zero same_count zero
# zero target* [zero] zero same_count answer zero
<<[>>
# ne-cmp left twin:
# target answer [zero] zero same_count
# answer [zero] zero same_count target
<[->+>+<<]
<[->+>-<<]
>>>>+<<[[-]>>-<<]<<
>>>[-<<<+>>>]
>[-<+>]
<<<[->>>+<<<]
# ne-cmp left twin (end)
<<]>>
# (current format) zero answer [zero] zero same_count target* zero
>>>[<<<
<[->+<]
>>>>[-<<<<+>>>>]
<[->+<]
<
>>>]<<<
>>[-<+>]
<<<[->>>+<<<]
# ne-cmp left to all (end)
>>>[->+<]<<<
<]>
# (current format) zero [zero] zero same_count zero target* zero
>>>>[
[-<<<<+>>>>]
<<[->+<]>>>
]<<<<
# ne-cmp all diff (end)
# then print if cmp ne = 0
>+>[[-]<->] # not
<[-
<<[.<]
10+.10-
>[>]>
]<
# inc:
# zero target* [zero] zero
# zero target*(+1) [zero] zero
# shift target right (2 bits)
<[ [->>+<<]< ]>>
# zero zero [zero] target* zero
<+[->
#<+->
>+
[-<+<+>>]
<[->+<]
+< 58- [[-]>-<]
# (current format) [zero] continue_inc target*
>[-<+>>10-<]<[->+<]
>>[-<<+>>]
# (current format) target' continue_inc [zero] target*
<]>
>[ [-<<+>>]> ]
<<< [->+>+<<] >>[-<<+>>]
+<- [[-]>-<]
# (current format) target* [zero] overflow_flag
>[-<<48+>>]<
# inc (end)
# count length:
# target* [zero] zero zero
# target* [zero] length zero
<[
[->>+<<]
>+[-<+>]<
<
]>
[->+<]>
>[
[-<<+>>]
<[->+<]>
>
]<<
# count length (end)
# copy input length
<[<]< [->>[>]>>+>+<<<<[<]<]
>>[>]>>> [-<<<<[<]<+>>[>]>>>]
# cmp curr length <= input length
<<[->-<]>
+ [-<<<[<]<<[-]+>>>[>]>>]
<<< <[<]
<<]>>
# main loop (end)
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 112.121.78.5
※ 文章網址: https://www.ptt.cc/bbs/Programming/M.1481287760.A.ECC.html
→
12/09 20:50, , 1F
12/09 20:50, 1F
→
12/09 20:51, , 2F
12/09 20:51, 2F
※ 編輯: CindyLinz (112.121.78.5), 12/09/2016 21:01:07
推
12/09 22:36, , 3F
12/09 22:36, 3F
推
12/09 22:44, , 4F
12/09 22:44, 4F
推
12/09 23:09, , 5F
12/09 23:09, 5F
推
12/09 23:40, , 6F
12/09 23:40, 6F
推
12/09 23:44, , 7F
12/09 23:44, 7F
→
12/10 01:17, , 8F
12/10 01:17, 8F
推
12/10 03:57, , 9F
12/10 03:57, 9F
推
12/10 07:57, , 10F
12/10 07:57, 10F
推
12/10 11:57, , 11F
12/10 11:57, 11F
推
12/10 14:42, , 12F
12/10 14:42, 12F
→
12/10 14:42, , 13F
12/10 14:42, 13F
推
12/10 14:53, , 14F
12/10 14:53, 14F
推
12/11 00:17, , 15F
12/11 00:17, 15F
推
12/14 03:15, , 16F
12/14 03:15, 16F
推
12/14 08:32, , 17F
12/14 08:32, 17F
推
12/14 09:14, , 18F
12/14 09:14, 18F
推
12/15 10:22, , 19F
12/15 10:22, 19F
推
12/20 14:45, , 20F
12/20 14:45, 20F
Programming 近期熱門文章
PTT數位生活區 即時熱門文章