# a brainfuck problem

Yesterday evening, 0xSuu sent me a problem but which requires to write in brainfuck language. The complete problem is here.

The input will be a newline-terminated string. The first character
will be an ASCII digit. You need to output the entire line this
number of times, followed by a blank line.

For example, if the input string is "3 is the magic number" then
3 is the magic number
3 is the magic number
3 is the magic number

Yeah, you're working for these points.

I tried nearly 2 hours to solve it. But my mind was a mess that time, so I rewrote it just now. And it works.

Here is the brainfuck program. It's not the best practice, but it works.

>                              cell 0 is reserved
>                              cell 1 is reserved for count
,                              the first character# ASCII digit
[
[                          swap this cell with its next cell
-
>
+
<
]
after this swap# original cell value should be 0
>                          move to the next cell
----------                 decrease 10# '\n' in brainfuck is 10 (in most case)
[                          if the cell is not 0
++++++++++                 increase 10# reset its original value
<                      move to the original cell
+                      increase 1 manually# just for while loop
>                      move to the next cell
-                      decrease 1# because we just add 1 to original cell
[                      loop until the next cell is 0
<
+
>
-
]
after this while loop# we reset the original cell to its original value
]
<                          but the ptr points to the next cell# we should move it to its left cell
]
<                              read completed# we are going to move to the count cell# 1
[
<
]
>                              now move ptr to cell 2# the start of the string
[                              cut the value of cell2# paste to cell 1 and cell 0
-
<
+
<
+
>>
]
after the while statement# ptr points to cell 2
<
<
move ptr to cell 0 and restore cell 2# because cell 2 is start of the string
[
-
>
>
+
<
<
]
ptr points to cell 0 after while loop
>                              move it to cell 1
convert ASCII code to integer# cell 1 is used for count
------------------------
------------------------
start print
[
>                          move to cell 2# the start of the string
[                          while cell has value# print it
.
>
]
the cell after the last cell of the string
we use it for printing '\n'
++++++++++
.
----------

<                          goto cell 0
[
<
]
>                          move to cell 1# count
-                          decrease count
]
声明: 本文为 Cocoa 原创, 转载注明出处喵～