如何计算2号与BrainFuck总和(How to calculate the sum of 2 n

2019-06-24 14:37发布

我试着写与BrainFuck一个程序,可以读出两个数高达9,计算它们的总和,然后打印结果出来,例如3和5给出结果8。

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

Answer 1:

语言看作一个巨大的磁带(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


Answer 2:

我看到这个帖子2-3天前,我已经在它的工作,现在我对多位数另外一个解决方案。 首先,我认为这PL的名字有点反感,但现在我知道,如果我被授权来命名这种编程语言,我会选择同样的。

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

$ bf sum.bf
199+997=
1196
$

只有+已经号码可以在我的代码被添加。 并确保您使用两个输入相同的位数 。 也就是说,如果你想添加57 3,然后给像57 + 03 = 03或57 + =输入。 现在的代码。 我已经证明用一个例子。 不过我不想看着我的代码,由自己设计比学习或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

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



Answer 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岁以下导致



Answer 4:

我也做了一个程序,只能处理单个数字输入和答案:

#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
.


Answer 5:

许多人已经回答了这个问题,但由于每一个解决方案有些不同,我就加我的为好。

我的解决办法确实单个数字加法(也如果结果为> 9)。 因此,例如用于输入“89”,它retuns“17”。 我加了很多的意见,所以应该是比较容易理解。

GitHub的链接

[
  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'
   >
]


Answer 6:

我建立了一个代码,将与输入数的无限量工作,但它只能输出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

或简称:

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



文章来源: How to calculate the sum of 2 numbers with BrainFuck
标签: brainfuck