如何使用L/R移位、L/R循环、and、XOR和OR有效地解析和计算位字符串表达式?

2024-09-29 01:37:25 发布

您现在位置:Python中文网/ 问答频道 /正文

在课堂上,我们刚开始学习位串的闪动,从基本函数开始:LSHIFT、RSHIFT、LCIRC、RCIRC和XOR或。然后,突然,我们接到了编写一个python程序的任务,该程序将解析和计算位字符串表达式。虽然我发现手工求解位字符串表达式相当简单,但我对通过python解析和计算位字符串表达式的有效方法还没有很好的掌握。我已经定义了下面所有必需的函数,这些函数适用于单运算符表达式(即LCIRC 4 0010),但到目前为止,我完全不知道如何解析多运算符表达式(即LCIRC 3 LCIRC 3 0010)。在

import collections 
commands = ["LCIRC","RCIRC"] 
i = 0 
parse = input("Enter a string here to be evaluated. Use bitstrings only, as this program cannot catch any exceptions otherwise. -->").upper().replace("AND","&").replace("OR","|").replace("XOR","^").replace("NOT","~").split(" ") 
#parsing the function 

def lcirc(circ,operand): 
    circ = int(parse[i+1]) 
    operand = list(parse[i-1]) 
    parse.pop(i);parse.pop(i);parse.pop(i); 
    length = len(operand) 
    circ = circ % length 
    operand = operand[circ % length:]+ operand[:circ % length]                       
    operand = "".join(operand) 
    return operand 

def rcirc(Rcirc,operand): 
    Rcirc = int(parse[i+1]) 
    operand = list(parse[i-1]) 
    parse.pop(i);parse.pop(i);parse.pop(i); 
    length = len(operand) 
    Rcirc = Rcirc % length 
    operand = operand[-Rcirc % length:]+ operand[:-Rcirc % length]                       
    operand = "".join(operand) 
    return operand

def rshift(shift,operand): 
    shift = parse[i+1] 
    operand = list(parse[i+2]) 
    parse.pop(i);parse.pop(i);parse.pop(i); 
    print(operand) 
    length = len(operand) 
    if int(shift) >= len(operand): 
        for a in range(0,len(operand)): 
            operand.insert(0,"0") 
    if int(shift) < len(operand): 
        for a in range(0,int(shift)):
            operand.insert(0,"0") 
    operand = operand[:length] 
    operand = "".join(operand) 
    return operand 

def lshift(shift,operand):
    shift = parse[i+1]
    operand = list(parse[i+2])
    parse.pop(i);parse.pop(i);parse.pop(i);
    length = len(operand)
    if int(shift) >= len(operand):
        for a in range(0,len(operand)):
            operand.insert(length,"0")
    if int(shift) < len(operand):
        for a in range(0,int(shift)):
            operand.insert(length,"0")
    operand = operand[-length:]
    operand = "".join(operand)
    return operand

def and(op1, op2): #operand1, operand2
    return str(bin(int(op1,2) & int(op2,2))).replace("0b","")

def xor(op1, op2): #operand1, operand2
    return str(bin(int(op1,2) ^ int(op2,2))).replace("0b","")

def or(op1, op2): #operand1, operand2
    return str(bin(int(op1,2) | int(op2,2))).replace("0b","")

def evaluate(): 
    #parsing and evaluating the expression, insert code here.
    i = 0


while True: 
    if "LCIRC" not in parse and "RCIRC" not in parse and "RSHIFT" not in parse and "LSHIFT" not in parse and "~" not in parse and "|" not in parse and "&" not in parse and "^" not in parse: 
        break 
    else: 
        evaluate() 

print("Your answer is --> " +"".join(parse)) 

该程序应能接受输入,如:

^{pr2}$

Tags: andinlenreturnshiftparsedefnot
2条回答

假设我正确地理解了您的问题陈述(第二个示例输出应该是1000吗?),我可以想出两种解决问题的方法:

1)从右向左评估。如果您只关注单变量函数调用,就像循环移位一样,那么您应该先简单地计算最里面(最右边)的表达式。在

2)使用recursion。这基本上采取如下形式(请原谅伪代码)。在

def handleExpression(str):
    #assuming str is of form of parse
    #if two sided operator
    if(typeof str[0] == number)
        #Find base operator and the two expressions which represent its arguments.
        #^^Format the arguments in the form of parse
        #(You can think of this in terms of parentheses: for every open parenthesis, you need a closing parenthesis.
        #So, you can just keep track of open and close "parentheses" until you reach an operand with a balance of "parentheses")
        operation = #xor, and, etc.
        return operation(handleExpression(leftSide), handleExpression(rightSide))
    else #some kind of shift
        #whatever code

显然,我在这段代码中留下了很多漏洞,但毕竟这不是一项任务吗?:)我希望这有助于你开始思考这个问题。在

似乎是递归或Reverse Polish notation的任务

我的简单方法是,使用带有maxplit参数的stringsplit方法:

def evaluate(s):
    cmd, arg1, arg2 = s.split(None, 2)
    if ' ' in arg2:
        arg2 = evaluate(arg2)
    return command(arg1, arg2) # TODO: call correct function with correct arguments

def command(a1, a2):
    print a1, a2

相关问题 更多 >