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)
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)
コード例 #3
0
def calpostfix(stt):
    stack = Stack()
    for i in stt:
        if i in '+-*/':
            pop1 = stack.pop()
            pop2 = stack.pop()
            temp = pop2+i+pop1
            stack.push(str(eval(temp)))
        else:
            stack.push(i)
    print('calresult:',stack.peek())
コード例 #4
0
def ToPostfix(expression):
    L = []
    stack = Stack()
    for i in expression:
        if i in '(+-*/':
            stack.push(i)
        elif i == ')':
            while stack.peek() != '(':
                L.append(stack.pop())
            stack.pop()
        else:
            L.append(i)
    print('postfix:',''.join(L))
    return L
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 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
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')
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')