Esempio n. 1
0
def parentheses(expr):
    stack = Stack()
    expr_list = spit_num(expr)
    for item in expr_list:
        if item == ')':
            num1 = stack.pop()
            oper = stack.pop()
            num2 = stack.pop()
            temp = '(' + num2 + oper + num1 + ')'
            stack.push(temp)
        else:
            stack.push(item)
    return stack.pop()
def evaluate_postfix(expr):
    expr_list = spit_num(expr)
    stack = Stack()
    for item in expr_list:
        if is_num(item):
            stack.push(item)
        elif is_operator(item):
            operator2 = stack.pop()
            operator1 = stack.pop()
            result = cal(float(operator1), float(operator2), item)
            stack.push(result)
        else:
            raise ValueError
    return stack.pop()
def convert(num, base=2):
    digits = '0123456789ABCDEF'
    stack = Stack()
    while num > 0:
        stack.push(num % base)
        num = num // base
    result = ''
    while not stack.is_empty():
        result = result + digits[stack.pop()]
    return result
def convert_binary(num):
    """
    把十进制数字转换为二进制
    :param num: 需要转换的数字
    :return:
    """
    stack = Stack()
    while num > 0:
        stack.push(num % 2)
        num = num // 2
    result = 0
    while not stack.is_empty():
        result = result * 10 + stack.pop()
    return result
Esempio n. 5
0
def parentheses(expr):
    stack = Stack()
    dic = {')': '(', ']': '[', '}': '{'}
    result = True
    for item in expr:
        if item in dic.values():
            stack.push(item)
        elif item in dic.keys():
            try:
                if stack.pop() != dic[item]:
                    result = False
            except EOFError:
                result = False
        else:
            raise ValueError
    if not stack.is_empty():
        result = False
    return result
Esempio n. 6
0
def evaluate(expr):
    oper_satck = Stack()
    vals_stack = Stack()
    expr_list = spit_num(expr)
    for item in expr_list:
        if item == "(":
            continue
        elif item in ['+', '-', '*', '/']:
            oper_satck.push(item)
        elif item == ")":
            op = oper_satck.pop()
            var = vals_stack.pop()
            if op == '+':
                var += vals_stack.pop()
            elif op == '-':
                var -= vals_stack.pop()
            elif op == '*':
                var *= vals_stack.pop()
            elif op == '/':
                var /= vals_stack.pop()
            else:
                pass
            vals_stack.push(var)
        else:
            vals_stack.push(float(item))
    return vals_stack.pop()
Esempio n. 7
0
def reverse(queue):
    stack = Stack()
    while not queue.is_empty():
        stack.push(queue.dequeue())
    while not stack.is_empty():
        queue.enqueue(stack.pop())
def infix_to_postfix(expr):
    """
    infix convert to postfix
    :param expr: infix expression
    :return: postfix expression
    Example:
    >>> infix_to_postfix('(31+21)*33-31/17')
    31 21 + 33 * 31 17 / -
    """
    expr = spit_num(expr)
    stack = Stack()
    result_list = []
    for item in expr:
        if is_num(item):
            result_list.append(item)
        elif is_operator(item):
            while True:
                if stack.is_empty() or stack.peek() == '(' or cmp_priority(
                        item, stack.peek()):
                    stack.push(item)
                    break
                result_list.append(stack.pop())
        elif item == '(':
            stack.push(item)
        elif item == ')':
            while stack.peek() != '(':
                result_list.append(stack.pop())
            stack.pop()
        else:
            raise ValueError
    while not stack.is_empty():
        result_list.append(stack.pop())
    return ' '.join(result_list)
def infix_to_prefix(expr):
    expr = spit_num(expr)[::-1]
    stack = Stack()
    result_list = []
    for item in expr:
        if is_num(item):
            result_list.append(item)
        elif item == ')':
            stack.push(item)
        elif is_operator(item):
            if stack.is_empty() or stack.peek() == ')' or cmp_priority(
                    item, stack.peek()) > 0:
                stack.push(item)
            else:
                while True:
                    result_list.append(stack.pop())
                    if stack.is_empty() or stack.peek() == ')' or cmp_priority(
                            item, stack.peek()) > 0:
                        stack.push(item)
                        break
        else:
            while stack.peek() != ')':
                result_list.append(stack.pop())
            stack.pop()
    while not stack.is_empty():
        result_list.append(stack.pop())
    return ' '.join(result_list[::-1])