Source: Internet
Author: User

This article mainly introduces Python to implement inverse Polish calculation expression method, more detailed analysis of inverse Polish expression concept and implementation skills, with a certain reference value, the need for friends can refer to the

An inverse Polish expression is also called a suffix expression. In the usual expression, the two-dollar operator is always placed between the two operational objects associated with it, so this notation is also known as infix notation. J.lukasiewicz, a Polish logic family, presented another way of expressing expressions in 1929. By this method, each operator is placed after its Operation object, so it is called a suffix representation.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45, 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | #-*-Coding:utf-8-*-symbol_priority = {} Symbol_priority[0] = [' # '] symbol_priority [1] = ['] symbol_priority[2] = [' + ', '-'] symbol_priority[3] = [' * ', '/'] symbol_priority[4] = ['] '] def comparepriority (Symbol, Rpn_stack, symbol_stack): "' Compare priority between two '" ' Global symbols If Len (symbol_priority) > 0:symbol_pop = Symbol_stack.pop () Else:return for list by symbol_priority.values (): if (symbol in list) and (Symbo L_pop in list: "Same priority" Symbol_stack.append (Symbol_pop) symbol_stack.append (symbol) return elif symbol in LIS T: ' symbol ' smaller ' rpn_stack.append (symbol_pop) #recusion call comparepriority (symbol, Rpn_stack, symbol_stack) Return elif Symbol_pop in list: ' "' symbol is bigger ' symbol_stack.append (symbol_pop) symbol_stack.append (symbol) Return else:continue symbol_stack.append (symbol_pop) return def scaneveryone (input_string, Rpn_stack, Symbol_stack): For ch in input_string:if Ch.isdigit (): Rpn_stack.append (CH) else:if len (symbol_stack) > 0:if ch = = ' (': Symbol_stack.append (ch) elif ch = = ') ': while True : Symbol_pop = Symbol_stack.pop () if Symbol_pop = = ' (': Break Else:RPN_stack.append (Symbol_pop) else:comparepriority (CH, Rpn_stack, Symbol_stack) else:symbol_stack.append (CH) def scaninput (Rpn_stack, symbol_stack): input_string = Raw_input () input_string + = ' # ' Scaneveryone (input_string, Rpn_stack, Symbol_stack) def CALRPN (rpn_stack): Value_stack = [] RPN_sta Ck.append (' # ') for value in rpn_stack:if value = = ' # ': Return Value_stack.pop () break If Value.isdigit (): Value_stack.app End (value) Else:right_value = Value_stack.pop () Left_value = Value_stack.pop () cal_string = left_value + value + Right_va Lue value_stack.append (str (cal_string)) def main (): Rpn_stack = [] Symbol_stack = [] Scaninput (Rpn_stack, Symbol_st ACK) print CALRPN (rpn_stack) if __name__ = = ' __main__ ': Main () |

calrpn.py

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45, 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #-*-Coding:utf-8-*-symbol_priority = {} Symbol_priority[0] = [' # '] symbol_priority [1] = ['] symbol_priority[2] = [' + ', '-'] symbol_priority[3] = [' * ', '/'] symbol_priority[4] = ['] '] def comparepriority (Symbol, Rpn_stack, symbol_stack): "' Compare priority between two '" ' Global symbols If Len (symbol_priority) > 0:symbol_pop = Symbol_stack.pop () Else:return for list by symbol_priority.values (): if (symbol in list) and (Symbo L_pop in list: "Same priority" Symbol_stack.append (Symbol_pop) symbol_stack.append (symbol) return elif symbol in LIS T: ' symbol ' smaller ' rpn_stack.append (symbol_pop) #recusion call comparepriority (symbol, Rpn_stack, symbol_stack) Return elif Symbol_pop in list: ' "' symbol is bigger ' symbol_stack.append (symbol_pop) symbol_stack.append (symbol) Return else:continue symbol_stack.append (symbol_pop) return def scaneveryone (input_string, Rpn_stack, Symbol_stack): For ch in input_string:if Ch.isdigit (): Rpn_stack.append (CH) else:if len (symbol_stack) > 0:if ch = = ' (': Symbol_stack.append (ch) elif ch = = ') ': while True : Symbol_pop = Symbol_stack.pop () if Symbol_pop = = ' (': Break Else:RPN_stack.append (Symbol_pop) else:comparepriority (CH, Rpn_stack, Symbol_stack) else:symbol_stack.append (CH) def scaninput (Rpn_stack, symbol_stack): input_string = Raw_input () input_string + = ' # ' Scaneveryone (input_string, Rpn_stack, Symbol_stack) def CALRPN (rpn_stack): Value_stack = [] RPN_sta Ck.append (' # ') for value in rpn_stack:if value = = ' # ': Return Value_stack.pop () break If Value.isdigit (): Value_stack.app End (value) Else:right_value = Value_stack.pop () Left_value = Value_stack.pop () cal_string = left_value + value + Right_va Lue value_stack.append (str (cal_string)) def main (): Rpn_stack = [] Symbol_stack = [] scaninput (Rpn_stack, Sym Bol_stack) Print CALRPN (rpn_stack) if __name__ = = ' __main__ ': Main () |

I hope this article will help you with your Python programming.

**Note** : For more wonderful tutorials, please pay attention to the triple graphics tutorial channel,