def build_parse_tree(fp_exp): fp_list = fp_exp.split() p_stack = Stack() e_tree = BinaryTree('') p_stack.push(e_tree) current_tree = e_tree for i in fp_list: if i == '(': current_tree.insert_left('') p_stack.push(current_tree) current_tree = current_tree.get_left_child() elif i not in '+-*/)': current_tree.set_root_val(int(i)) parent = p_stack.pop() current_tree = parent elif i in ['+', '-', '*', '/']: current_tree.set_root_val(i) current_tree.insert_right('') p_stack.push(current_tree) current_tree = current_tree.get_right_child() elif i == ')': current_tree = p_stack.pop() else: raise ValueError return e_tree
def trans(deci, base): flag = '0123456789ABCDEF' stack = Stack() while deci // base > 0: stack.push(flag[deci % base]) deci //= base stack.push(flag[deci]) print(stack.cstack)
from stackType_01 import Stack while True: stack = Stack() str = input('input the arithmetic expression:') if len(str) == 0: print('input something') else: for i in str: if i in '({[': #!!! stack.push(i) elif i in ')}]': stack.pop() if stack.size() == 0: print('It is a balanced arithmetic expression') else: print('not a balanced arithmatic expression')