請寫一個整數計算器,支援加減乘三種運算和括號。Python版本

深沉的背影發表於2020-11-02

面試之手撕程式碼-請寫一個整數計算器,支援加減乘三種運算和括號。Python

思路

首先建立兩個棧,一個用來存放數字,一個用來存放符號

之後遍歷使用者輸入的表示式,對數字壓入數字棧,對於符號壓入符號棧,不過壓入符號棧時候有如下規則:

(1) 如果符號為"(",直接壓入符號棧,如果為")",不壓符號棧,而要進行出棧,直到遇到"("停止;在這過程中對數字棧出棧兩次,與符合棧出棧的符號(除"(")做運算,結果壓入數字棧;

(2).如果符為"*","/",如果符號棧為空,直接壓棧;否則執行如下:

    如果當前棧頂元素為"/"或"*",出棧,直到非二者,並且,將出棧符號和數字棧中取出兩個數運算,結果壓入數字棧;如果為別的元素,則直接壓入符號棧 。

(3).如果符為"-","+",如果符號棧為空,直接壓棧;否則執行如下:

    如果當前棧頂元素為"/"或"*","-"或"+",出棧,直到非它們中任何一個,並且,將出棧符號和數字棧中取出兩個數運算,結果壓入數字棧;如果當前棧頂元素為別的元素,則直接壓入符號棧 。

示例1

輸入"1+2"

結果為:3

示例2

輸入"(2*(3-4))*5"

結果為:-10

示例3

輸入"3+2*3*4-1"

結果為:26

具體程式碼:

class Solution:
    def solve(self , s ):
        # write code here
        def computer(first,last,sign):
            if sign == "+":
                return first + last
            if sign == "/":
                return first / last
            if sign == "*":
                return first * last
            if sign == "-":
                return first - last
        sign_stack = []
        number_stack = []
        number = 0
        for i in range(len(s)):
            if s[i] >= '0' and s[i] <='9':
                number = number * 10 + int(s[i])
                if i + 1 < len(s) and s[i+1]>='0' and s[i+1] <='9':
                    continue
                else:
                    number_stack.append(number)
                    number = 0
            elif s[i] == "(":
                sign_stack.append(s[i])
            elif s[i] == ")":
                while sign_stack[-1] != "(":
                    last = number_stack.pop()
                    first = number_stack.pop()
                    sign = sign_stack.pop()
                    res = computer(first,last,sign)
                    number_stack.append(res)
                sign_stack.pop()
            elif s[i] in ["*","/"]:
                if len(sign_stack) == 0:
                    sign_stack.append(s[i])
                elif sign_stack[-1] in ["*","/"]:
                    while len(sign_stack) != 0 and sign_stack[-1] in ["*","/"]:
                        last = number_stack.pop()
                        first = number_stack.pop()
                        sign = sign_stack.pop()
                        res = computer(first,last,sign)
                        number_stack.append(res)
                    sign_stack.append(s[i])
                else:
                    sign_stack.append(s[i])
            elif s[i] in ["+","-"]:
                if len(sign_stack) == 0:
                    sign_stack.append(s[i])
                elif sign_stack[-1] in ["+","-","*","/"]:
                    while len(sign_stack) != 0 and sign_stack[-1] in ["+","-","*","/"]:
                        last = number_stack.pop()
                        first = number_stack.pop()
                        sign = sign_stack.pop()
                        res = computer(first,last,sign)
                        number_stack.append(res)
                    sign_stack.append(s[i])
                else:
                    sign_stack.append(s[i])
        while sign_stack:
            last = number_stack.pop()
            first = number_stack.pop()
            sign = sign_stack.pop()
            res = computer(first,last,sign)
            number_stack.append(res)
        return number_stack[-1]

 

相關文章