Brainfuck compare 2 numbers as greater than or less than

亡梦爱人 提交于 2019-11-28 02:39:59

问题


How can I compare two numbers with an inequality? (greater than or less than)

I want to compare single digits For example

1 2
5 3
9 2

etc.


回答1:


This is the best way to compare two numbers.Why because, if you are intelligent enough, you can use the same code in bigger programs.It's highly portable.

Assume we have two numbers a,b. we have two blocks : if( a>=b ) and else, Hope its enough.

    0 1 0 a b 0

Make the array like this. And point to the (4) i.e. point to the a

    +>+<                   This is for managing if a=0 and b=0
    [->-[>]<<]             This is a magic loop. if a is the one which 
                           reaches 0 first (a<b),then pointer will be at(4).
                           Else it will be at (3)
    <[-  
         //       BLOCK (a>=b)
         //You are at (2) and do whatever you want and come back to (2).
         //Its a must
    ]
    <[-<
         //       BLOCK(a<b)
         //You are at (1) and do whatever you want and come back to (1).
         //Its a must
    ]

It will not affect the following program code as both the code blocks will end up in (1) You can do further coding assuming that pointer will reach (1)

Please remove the documentation if you copy the code. Because code contains some valid brainfuck symbols like < . , etc.




回答2:


Once you know which is the distance between the two numbers you should or decrement both of them in the same loop iteration and then check both for being zero: you will understand which one is the smaller.

Eg:

+++++ > +++ < [->-< check is first is zero, then second]

(this is just to give you a hint, you will have to take care about equal numbers and similar issues.




回答3:


I was thinking about this too, and while I'm sure this isn't the best solution, at least it can answer the question of which number is larger =)

The program asks for two characters, outputs '<' if the first is smaller, '>' if it is larger, and '=' if they are equal. After outputting one char, the program halts by asking for additional input.

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

Hopefully somewhat clearer:

+                                   init (0) to 1
>,                                  read (1)
>,                                  read (2)
<<[                                 loop forever
  >-[>>>]                           decrement (1) going to (4) if (1) != 0
  <[                                goto (0) == 1 if (1) reached 0 (otherwise goto (3))
    >>-[>++++++++++[->++++++<]>.,]  decrement (2) printing lessthan if larger than 0
    ++++++++++[->++++++<]>+.,       if (2) == 0 print '='
  ]
  <-[>>>]                           decrement (2) going to (5) if (2) != 0
  <<[                               goto (0) == 1 if (2) reached 0 (otherwise goto (3))
    >>>++++++++++[->++++++<]>++.,   print largerthan since (2) reached 0 first
  ]
  <<<                               goto(0)
]



回答4:


Given two numbers A and B, the following code will print A if A is greater than B, B if B is greater than A and C if both are equal.

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




回答5:


No such thing exists in BF. The > and < in BF move the pointer to the right and to the left, respectively.



来源:https://stackoverflow.com/questions/6168584/brainfuck-compare-2-numbers-as-greater-than-or-less-than

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!