先上代码:
···
def postfix(midfix:[str],prec_table:{str:int})->[str]:
main_stack:[str] = []
result_queue:[str] = []
for op in midfix:
if(main_stack!=[]):
if(op in prec_table.keys()):
if (op == ')'):
while (main_stack!=[] and main_stack[-1]!='('):
result_queue.append(main_stack.pop())
if (main_stack!=[]):
main_stack.pop()
else:
while (main_stack != [] and prec_table[op]
result_queue.append(main_stack.pop())
main_stack.append(op)
else:
result_queue.append(op)
else:
if (op in prec_table.keys()):
main_stack.append(op)
else:
result_queue.append(op)
while (main_stack!=[]):
result_queue.append(main_stack.pop())
return result_queue
···
网友评论