3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ずんだの1桁足し算問題 Brainf**k版

Last updated at Posted at 2018-10-07

てぃーびーさんずんだの1桁足し算問題をBrainf**kで実装しました。
ループの制御、複数桁の数字管理等、Brainf**kの初~中級として手頃な問題ではないかと思います。

実装(367文字)
** ずんだの1桁足し算問題
** prepare symbol characters
+++[->+++++++<]
>[->P++>E+++>C++++++<<<]
>P+>E-->>

** double loop
i+++++++++[-
 >>j+++++++++[-
  ** main
  * memory layout: PECi_j
  *  P E C: character code for PLUS EQUAL TILDE respectively
  *  i j: loop counters from 8 downto 0

  ** create several numbers from i j
  <<i[->+>>S->>I->>S-<<<<<<<]>[-<+>]
  >j[-<+>>S->>>J->S-<<<<<]
  ++++++++++[->S++>>I+>J+>S++<<<<<]

  * memory layout: PECij_S_IJS
  *  I J: 10 minus i/j
  *  S  : I plus J

  ** divide by 10
  >>>>>>>R++++++++++<<S--[->>R-[<r+>>>>]<[r+[->R+<]>>q+>]<<<<]

  * memory layout: PECij_S_IJ_rRq
  *  r q: remainder and quotient ( result of the division )
  *  R: 10 minus r

  ** digit compaction and replacement
  >>>q[+[-<<<+>>>]<R[-]<r+>>]  ( if q is not zero )
  <R[[-]<r+[-<+>]>]            ( else             )

  * memory layout: PECij_S_IJD(D)
  *  D: created digit(s) of S minus 2 through the division
  *  notice: I J D are all larger by 1 than they are meant to be

  ** convert numbers to characters and print
  <<<<<<S<++++++[->>>I[>]<[++++++++<]<<]
  >>>I-.<<<<<<<<P.>>>>>>>>>J-.<<<<<<<<E.>>>>>S--[-<<<<T.>>>>]>>>>D[-.>]

  ** print newline and clear temporary characters I J D then move j back
  ++++++++++.[[-]<]<<<[->+<]
 >j]
<<i]

実行例はideoneでの実行例を、Brainf**kの組み方についてはBrainf**k講座(リンク)をご参照ください。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?