Brainfuck compare 2 numbers as greater than or les

2019-01-19 10:48发布

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.

标签: brainfuck
5条回答
霸刀☆藐视天下
2楼-- · 2019-01-19 11:34

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.

查看更多
Fickle 薄情
3楼-- · 2019-01-19 11:34

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.

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

查看更多
闹够了就滚
4楼-- · 2019-01-19 11:40

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)
]
查看更多
\"骚年 ilove
5楼-- · 2019-01-19 11:44

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.

查看更多
孤傲高冷的网名
6楼-- · 2019-01-19 11:46

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

查看更多
登录 后发表回答