如何用BrainFuck计算2个数字的总和

时间:2012-05-20 03:10:26

标签: brainfuck

我正在尝试用BrainFuck编写一个程序,该程序可以读取最多9个数字,计算它们的总和,然后输出结果,例如3& 5给出结果8。

我只是想了解BF语言,但它看起来比我想象的要难得多。

7 个答案:

答案 0 :(得分:27)

将语言视为一个巨大的磁带(30K字节长),您可以在其中读取,写入和向前或向后移动,一次递增/递减一个单元格(每个单元格为1个字节,因此您实际上有30K个单元格) 。或者,您可以读入和写出字节流保存的内容(以ASCII格式)。假设您了解基本运算符,那么对两个数字求和的程序应该遵循以下几行:

,       ; read character and store it in p1
>       ; move pointer to p2 (second byte)
,       ; read character and store it in p2
[           ; enter loop
    <       ; move to p1
    +       ; increment p1
    >       ; move to p2
    -       ; decrement p2
]           ; we exit the loop when the last cell is empty
<       ; go back to p1
------------------------------------------------ ; subtract 48 (ie ASCII char code of '0')
.       ; print p1

答案 1 :(得分:10)

我在2-3天前看过这篇文章并且我已经完成了它,现在我有一个多位数添加的解决方案。首先,我认为这个PL的名称有些令人反感,但现在我知道,如果我被授权命名这种编程语言,我会选择相同的。

现在,我会告诉你如何使用我的代码

$ bf sum.bf
199+997=
1196
$

我的代码中只能添加+ ve号码。并且确保在两个输入中使用相同的位数。即如果你想添加57与3,然后输入像57 + 03 =或03 + 57 =。 现在的代码。我已经记录了一个例子。我仍然不想查看我的代码,因为自己设计比研究或解决bf中的代码更容易。首先,您需要知道如何比较两个数字。我在this问题中的答案是一个解决方案。 在文档中,我使用'plus'而不是+,因为+是bf中的有效操作。

    >> +
    [- >,>+< 
    ----- ----- ----- -----    ; checking with ascii 43 ie plus symbol
    ----- ----- ----- -----
    ---
    [
    +++++ +++++ +++++ +++++
    +++++ +++++ +++++ +++++
    +++
    < ] >>
    ]
    ; first input is over and terminated by a 'plus' symbol
    <->>>>>+
    [- >,>+<
    ----- ----- ----- -----   ; checking with ascii 61 ie = symbol
    ----- ----- ----- -----
    ----- ----- ----- ------
    [
    +++++ +++++ +++++ +++++
    +++++ +++++ +++++ +++++
    +++++ +++++ +++++ ++++++
    < ] >>
    ]
        ; second input is over and terminated by an = symbol
        ; now the array looks like 0 0 0 49 0 50 0 0 0 0 0 0 0 0 49 0 53 0 0 1 0
        ; for an input 12'plus'15=
    <<<<
    [<+<]
                ; filled with 1's in between
    + [<+>-<<[>-]>] ; This is a special loop to traverse LEFT through indefinite no of 0s
                ; Lets call it left traverse
    <<
    [<+<]
    >[>]<
               ; now the array looks like
               ; 0 0 1 49 1 50 0 0 0 0 0 0 0 1 49 1 53 0 0 1 for eg:12plus15
    [
    [->+>   + [>+<->>[<-]<]  ; Right traverse
        >>[>]<+ [<]
        + [<+>-<<[>-]>]  ; Left traverse
        <<-<
    ] 
    + [>+<->>[<-]<] 
    >> [>] <<-<[<]
    + [<+>-<<[>-]>]
    <<-<
    ]
             ; now actual addition took place
             ; ie array is 00000000000000 98 0 103 0 0 1
    + [>+<->>[<-]<]
    >>
    [ 
    ----- ----- ----- -----
    ----- ----- ----- -----
    ----- ---
    >>]
                ; minus 48 to get the addition correct as we add 2 ascii numbers
    >-<         ; well an undesired 1 was there 2 place after 103 right ? just to kill it
            ; now the array is 00000 00000 0000 50 0 55
            ; now comes the biggest task Carry shifting
    <<
    [<<]
    +++++ +++++ +++++ +++++
    +++++ +++++ +++++ +++++
    +++++ +++
    [>>]
        ; we added a 48 before all the digits in case there is an overall carry
        ; to make the size n plus 1
        ; array : 00000 00000 00 48 0 50 0 55
    <<
    <<
    [
    [>>->[>]>+>>>> >>>+<<<< <<<<<[<]><<]
    >+[>]>-
    [-<<[<]>+[>]>]
    >>>>>+>>>
    +++++ +++++ +++++ +++++ +++++
    +++++ +++++ +++++ +++++ +++++
    +++++ +++
    <
                ; comparison loop:  0   1   0   a      b  0
                ;                  (q) (p)    (num)  (58)
    [->-[>]<<]  ; comparison loop to check each digit with 58: greater means 
                ; we need to minus 10 and add 1 to next significant digit
    <[-
            ; n greater than or equal to 58 (at p)
            <<<< <<<
            [<]+
            >
            ----- ----- ; minus 10 to that digit
            <<+         ; plus 1 to next digit
            >
            [>]
            >>>>>>
    ]
    < [-<
            ; n less than 58 (at q)
            <<<<<<
            [<]+
            [>]
            >>>>>
      ]
        ; at (q)
        >>>[-]>[-]
        <<<<< <<<<<
        [<]>
        <<
    ]
        ; Its all over now : something like 0 48 0 52 0 66 ( ie 0 4 18 )
        ; will turn into 0 48 0 53 0 56 (ie 0 5 8)
    >>
    ----- ----- ----- -----
    ----- ----- ----- -----
    ----- ---
            ; here we are just checking first digit is 48 or not
            ; its weird to print 0 ahead but it is defenitely needed
            ; if it is 49 ie 1
    [
    +++++ +++++ +++++ +++++
    +++++ +++++ +++++ +++++
    +++++ +++
    .
    [-]
    ]
    >>
    [.>>]
    +++++ +++++
    .           ; to print nextline : ascii 10

我知道它有点冗长的代码,可能有更好的解决方案。 但仍然值得一试。

答案 2 :(得分:3)

这就是我所知道的

,                           ;read character and store it in p1
------------------------------------------------   ;return ascii to Dec
<                           ;move pointer to p2 (second byte)
,                           ;read character and store it in p2
------------------------------------------------ ;return ascii to Dec
[                           ; enter loop
-                           ; decrement p2
>                           ; move to p1
+                           ; increment p1
<                           ; move to p2
]                           ; we exit the loop when the last cell is empty
>                           ;go back to p1
++++++++++++++++++++++++++++++++++++++++++++++++     ;return Dec to ascii
.                           ;print p1

<强>输入

12

<强>输出:

3

你应该使用10以下的数字,结果是10

答案 3 :(得分:0)

我还制作了一个只能处理单个数字输入和答案的程序:

#Make the first cell (Cell 0) hold a value of 48 
>++++ ++++
[
<++++ ++
>-
]

#Get inputs and minus 48 from each to get Decimal 

,>,
<<
[
>-
>-
<<-
]

#Adds the contents of Cells 1 and 2


>
[
>+
<-
]

#Moves answer to Cell 0
>
[
<+
>-
]
<
[
<+
>-
 ]

#Converts answer to ASCII
>++++ ++++
[
<++++ ++
>-
]
<

[
<+
>-
]
<
#Print answer
.

答案 4 :(得分:0)

许多人已经回答了这个问题,但由于每个解决方案都有所不同,我也只是添加了我的解决方案。

我的解决方案会进行单位数添加(如果结果为&gt; 9也是如此)。所以例如输入&#34; 89&#34;它重新回归&#34; 17&#34;。我添加了很多评论,所以它应该相对容易理解。

GitHub link

[
  A brainfuck program for doing a single digit addition.

  Ex. input: '13' -> output: '4'
      input: '99' -> output: '18'

  Author: Florian Baierl
]

initialize #0 with 48 (ASCII char for '0')
>++++ ++++
[
<++++ ++
>-
]

save input to #1 and #2
,>,

substract value from #0 from #1 and #2
<<
[
>-
>-
<<-
]

move to #1
>

substract from #1 and add to #2; now the answer is in #2
[
 ->+<
]

since we need to modify the answer afterwards write it to #3 and #6
as well
>
[>+>>>+<<<<-]


Is the answer bigger than 9?
to continue the memory tape should look like this:
0 1 0 (a) b 0
with the pointer pointing to a

<+
>>>+++++ +++++ b is 10
<              point to a

+>+<           necessary in case a and b are zero

loop to determine whether a or b reach 0 first
[->-[>]<<]

<[-
   a was bigger or equals b
   the answer is still stored in #6

   subtract 10 from #6 (the answer)
   >>>>> ----- -----

   write 48 to #4
   <++++ ++++
   [
   <++++ ++
   >-
   ]
   add 48 to #5 and #6
   <
   [->+>+<<]

   print out the results
   >+.>.

   leave loop (back to #2)
   <<<<
 ]
 <[-
   a was samller so we can simply print the answer out after adding 48 to it
   the answer is still stored in #6

   >>
   ++++ ++++
   [
   <++++ ++
   >-
   ]

   <
   [
    ->>>>+<<<<
   ]

   print #2
   >>>>.

   we want to leave the loop so go somewhere with the value '0'
   >
]

答案 5 :(得分:0)

我构建了一个代码,可以使用无限量的输入数字,但它只能输出1或2位数,因此数字总和的范围可以是0-99

>,                     go to cell #1 and read input
[                      loop while input is not null
  >+++++++[<------->-] subtract 49 from input
  <+                   add 1 to input so we have the value of the input numeral in cell #1
  [-<+>]               add cell #1 to cell #0
  ,                    read next numeral
]                      repeat until no number is being input (the input number can be longer than two digits)
>-                     go to cell #2 and set it to minus 1 so the loop will run (i will explain that at the end of the loop)
[                      loop while number in cell #0 is greater than 9
  +                    set the value of #2 to 0
  <[->+>+<<]           copy the value of #1 to #2 and #3
  >>[-<<+>>]           copy the value of #3 back to #1
  <<<                  go to #0
  [->>>+>+<<<<]        copy it to #3 and #4
  >>>>[-<<<<+>>>>]     restore it in #0
  <                    go to 3
  [-[-[-[-[-[-[-[-[-[  check if number is greater than 9
  <<+<----- ----->>>   if yes increment number in cell #1 and decrease number in cell #0 by 10
  [-]]]]]]]]]]]        set #3 to 0 so we can leave the loop
  <<                   go to #1
                       we need to check if the number in cell #1 was incremented
                       cell #2 will store the most recent value of cell #1
  [->->+<<]            so we subtract the value of cell #2 by the value of cell #1 and 
                       store the value of cell #1 in cell #3
  >>[-<<+>>]           restore the value of #1
  <                    go back to cell #2
]                      if cell #1 was increased then cell #2 will be minus 1 and the loop will restart
<[>+++++++[<+++++++>-] if cell #1 is greater than 0 then add 49 to it
<-.[-]]                subtract 1 so we have the ascii code of the value in #1 then print it and set it to zero
+++++++[<+++++++>-]<-. do the same with the value in cell #0

或简称:

>,[>+++++++[<------->-]<+[-<+>],]>-[+<[->+>+<<]>>[-<<+>>]<<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<[-[-[-[-[-[-[-[-[-[<<+<---------->>>[-]]]]]]]]]]]<<[->->+<<]>>[-<<+>>]<]<[>+++++++[<+++++++>-]<-.[-]]+++++++[<+++++++>-]<-.

答案 6 :(得分:0)

我创建了一个解决方案,可以将任意数量的数字与任意数量的数字相加:

,[+>,]>++++++++[<<[------<]>[>]>-]<<[++++[----[<]<+[<+<+>>-]<<[>>+<<
-]>[<<<[<<]+[>>]>-]>>>>[>]<-[[<]<<<<<[<<]>>>+>[>>]>>>>[>]<-]<[<]<<<<<[<<]>>[[-]>
>]>>>>[>]>+<]>-[<<[<]<[-]>>[>]>+]<<]<[-]<->>>>>->+[[-]>+>[<]<]<->>+>->+[>>+]<<[+
[--[>+>+<<-]>>[<<+>>-]>+<<[>++++++++++<[->-[>>]<<<]>>>>[<<<----------<<+>>>>]<]>
>[-]<<<[[-]<[-]<<[->>>>>+<<<<<]]<]<]>>>>>>>>>>[<<]>>[+[->++++++++[<++++++>-]<.[-
]]>>]

例如:

input:  13611+85399+35943
output: 134953

确保将“内存溢出行为”设置为自动换行。