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

Yah*_*hoo 8 brainfuck

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

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

dir*_*tly 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
Run Code Online (Sandbox Code Playgroud)

  • 在我一次又一次地完成代码后,我意识到它只能使用单值.对吧!! 我的意思是如果结果是单值eg3 + 2 = 5会好的,但是如果我们有4 + 6 = 10的话怎么办!! (2认同)

Dhe*_*Ram 11

我在2-3天前看过这篇文章,我已经开始研究它,现在我有一个多位数添加的解决方案.首先我认为这个PL的名字有点冒犯,但现在我知道,如果我被授权命名这种编程语言,我会选择相同的.

现在,我将告诉您如何使用我的代码.

$ bf sum.bf
199+997=
1196
$
Run Code Online (Sandbox Code Playgroud)

我的代码中只能添加+ ve数字.并确保在两个输入中使用相同的位数.即如果你想添加57与3,然后输入像57 + 03 =或03 + 57 =.现在的代码.我已经记录了一个例子.我仍然不想查看我的代码,因为自己设计比研究或解决bf中的代码更容易.首先,您需要知道如何比较两个数字.我在这个问题上的答案是一个解决方案.在文档中,我使用'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
Run Code Online (Sandbox Code Playgroud)

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