【问题标题】:How can I check if user input is equal to a value in brainfuck?如何检查用户输入是否等于 Brainfuck 中的值?
【发布时间】:2022-10-15 22:58:33
【问题描述】:

我正在尝试编写一个程序(我确信以前已经做过,只是想挑战自己),每个循环需要两个用户输入,一个用于字母/数字,一个用于检查用户是否完成输入( y/n),然后打印整个字符串。我知道到目前为止我所拥有的并不好,但基本上我开始循环,将指针向上移动两个(所以开头有一个可找到的 0 值),询问第一个和第二个输入,将下一个值设置为小写n,目前正在尝试弄清楚如何比较这些值。我试图打印值,这些值似乎只在两次输入后才打印,然后程序中断。

这是我到目前为止所拥有的:

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

如果它有所作为,我正在使用自制的brainfuck解释器,它可能有一些缺陷:

def brainfuck(code):
    array = [0]
    pointer = 0
    i = 0
    while(i < len(code)):
        if(code[i] == '<'):
            if(i != 0):
                if(pointer != 0):
                    pointer -= 1
        elif(code[i] == '>'):
            pointer += 1
            if(len(array) <= pointer):
                array.append(0)
        elif(code[i] == '+'):
            array[pointer] += 1
        elif(code[i] == '-'):
            if(array[pointer] > 0):
                array[pointer] -= 1
        elif(code[i] == '.'):
            print(pointer,chr(array[pointer]))
        elif(code[i] == ','):
            x = input('Input:')
            try:
                array[pointer] = int(x)
            except ValueError:
                array[pointer] = ord(x)
        elif(code[i] == '['):
            if(array[pointer] == 0):
                openBraces = 1
                while(openBraces > 0):
                    i += 1
                    if(code[i] == '['):
                        openBraces += 1
                    elif(code[i] == ']'):
                        openBraces -= 1
        elif(code[i] == ']'):
            openBraces = 1
            while(openBraces > 0):
                i -= 1
                if(code[i] == '['):
                    openBraces -= 1
                elif(code[i] == ']'):
                    openBraces += 1
            i -= 1
        i += 1

【问题讨论】:

    标签: python brainfuck


    【解决方案1】:

    我很久以前做过这样的事情,应该做的伎俩(如果我没记错的话)

    // if x == y: {if_code}; else: {else_code}
    
    ,
    
    (x)'y
    [->-<]>
    
    0'(y minus x)
    [>+>]<
    
    {if} (0)'0; {else} 0'y minus x'(1)
    -[
      {if_code}
      +>>
    ]<
    
    0'(y minus x)
    [
      <
      {else_code}
      >[-]
    ]<
    
    empty memory
    

    cmets 在每一行之前显示记忆的样子,它们的结构可能有点奇怪,以避免使用被解释器认为是脑残的符号。内存中的值由撇号分隔,括号用于显示指针在内存中的哪个位置。

    确保在内存中的第二个位置具有您想要比较输入的值,并在实现此代码之前将指针移回第一个位置。

    很抱歉我没有通读您的翻译,但我认为它有效。

    希望这可以帮助!

    【讨论】:

      猜你喜欢
      • 2018-02-03
      • 2018-08-19
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      • 2013-01-06
      • 2020-11-02
      • 1970-01-01
      相关资源
      最近更新 更多