コード例 #1
0
def infixToPostfix(infixexpr):
    opstack = Stack()  # 创建空栈
    result = []  # 结果的列表
    prec = {'*': 3, '/': 3, '+': 2, '-': 2, '(': 1}  # 运算符的优先级
    operator_list = ['+', '-', '*', '/']

    for token in tolist(infixexpr):
        if token.capitalize() in string.ascii_uppercase:  # 判断是否是操作数
            result.append(token.capitalize())
        else:
            if token == '(':  # 判断是是否是左括号
                opstack.push(token)
            elif token == ')':  # 判断是否是右括号
                topToken = opstack.pop()
                while topToken != '(':
                    result.append(topToken)
                    topToken = opstack.pop()
            elif token in operator_list:  # 判断是否是运算符
                while not opstack.isEmpty() and (prec[opstack.peek()] >= prec[token]):
                    result.append(opstack.pop())
                opstack.push(token)
            else:
                pass

    while not opstack.isEmpty():
        result.append(opstack.pop())

    return "".join(result)
コード例 #2
0
def parChecker(symbolString):
    """
    简单括号是否匹配
    :param symbolString: 括号字符串
    :return: bool型,匹配:True,不匹配:False
    """
    # 初始化一个栈
    s = Stack()
    # 是否匹配标志
    balanced = True
    # 下标
    index = 0
    # 遍历
    while index < len(symbolString) and balanced:
        # 取出字符
        symbol = symbolString[index]
        # 如果为左括号,则入栈
        if symbol == '(':
            s.push(symbol)
        else:
            # 当字符不为左括号,如果栈为空,则不匹配
            if s.isEmpty():
                balanced = False
            # 如果栈不空,则将左括号出栈
            else:
                s.pop()
        index = index + 1

    if balanced and s.isEmpty():
        return True
    else:
        return False
コード例 #3
0
def infixToPostfix(infixexpr):
    # 记录操作符优先级
    prec = {}
    prec["*"] = 3
    prec["/"] = 3
    prec["+"] = 2
    prec["-"] = 2
    prec["("] = 1
    # 初始化栈
    opStack = Stack()
    postfixList = []
    # 将表达式转为单词列表
    tokenList = infixexpr.split()

    for token in tokenList:
        if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
            postfixList.append(token)
        elif token == '(':
            opStack.push(token)
        elif token == ')':
            topToken = opStack.pop()
            while topToken != '(':
                postfixList.append(topToken)
                topToken = opStack.pop()
        else:
            while (not opStack.isEmpty()) and (prec[opStack.peek()] >=
                                               prec[token]):
                postfixList.append(opStack.pop())
            opStack.push(token)
    while not opStack.isEmpty():
        postfixList.append(opStack.pop())
    return " ".join(postfixList)
コード例 #4
0
def parChecker(symbolString):
    """
    普通情况:匹配括号(只有括号的符号)
    :param symbolString:
    :return:
    """
    s = Stack()

    balanced = True
    index = 0
    while index < len(symbolString) and balanced:
        symbol = symbolString[index]
        if symbol in '( [ {':
            s.push(symbol)
        else:
            if s.isEmpty():
                balanced = False
            else:
                top = s.pop()
                if not matches(top, symbol):  # false
                    balanced = False
        index += 1
    return True if s.isEmpty() and not balanced else False
コード例 #5
0
def baseConberter(decNumber, base):
    # 定义字符
    digits = '0123456789ABCDEF'
    # 初始化栈
    remstack = Stack()

    while decNumber > 0:
        rem = decNumber % 2
        remstack.push(rem)
        decNumber = decNumber // base

    newString = ""
    while not remstack.isEmpty():
        newString = newString + digits[remstack.pop()]

    return newString
コード例 #6
0
def int_to_str(n, base):
    convert_string = "0123456789"
    rem_stack = Stack()

    #Code to push digits onto the stack
    while n > 0:
        if n < base:
            rem_stack.push(n)
        else:
            rem_stack.push(n % base)
        n = n // base

    #Code to get back the stack values
    equiv_string = ""
    while not rem_stack.isEmpty():
        equiv_string = equiv_string + convert_string[rem_stack.pop()]
    return equiv_string if len(equiv_string) > 1 else "0"
コード例 #7
0
def divideBy2(decNumber):
    # 初始化栈
    remstack = Stack()
    # 截止条件为被除数大于0
    while decNumber > 0:
        # 取余数
        rem = decNumber % 2
        # 余数入栈
        remstack.push(rem)
        # 更新被除数
        decNumber = decNumber // 2

    # 当栈不空的时候,出栈
    binString = ""
    while not remstack.isEmpty():
        binString = binString + str(remstack.pop())

    return binString
コード例 #8
0
ファイル: using stack.py プロジェクト: Svastikkka/DS-AND-ALGO
from Stack.stack import Stack

obj = Stack()
obj.push(1)
obj.push(2)
while obj.isEmpty() is False:
    print(obj.pops())