《一元一次方程解》虛擬碼與Python程式碼實現
解一元一次方程虛擬碼: class TreeStructure expression:string ->表示式 left:TreeStructure ->左邊的樹結構 right:TreeStructure ->右邊的樹結構 state=0:string ->訪問狀態 parent:TreeStructure ->指向於父節點 operator:string ->操作符 result:int ->結果 def 棧對字串根據符號進行分割構造成樹(傳入一個字元變數str) 宣告一個名為root的樹,將str填入其構造方法 宣告一個棧並將root樹壓入棧中 while 棧不為空 宣告一個名為current的變數並將棧中第一個賦值給它 呼叫方法查詢current的expression中符號所在位置獲取返回值並命名為num IF num為-1 IF 將第一個“(”與最後一個“)”將其消除掉 current出棧 建立一個變數命名為left,並將其出去括號的字串填入構造方法 將當前出棧的current賦值給left的parent 將left賦值給root樹的左樹 將left壓入棧中 ELSE current出棧 將當前current對應的expression賦值給它的result屬性 ELSE IF num大於0 current出棧 根據下標0到返回值,來擷取字串,得到符號左邊的字串 = leftStr 根據返回值+1到字串長度,來擷取字串,得到符號右邊的字串 = rightStr left = new TreeStucture(leftStr) right = new TreeStucture(rightStr) 將當前num對應的符號賦值給current的operator 將current加入到left.parent作為父節點,並賦值給root的左樹 將current加入到right.parent作為父節點,並賦值給root的右樹 將right樹壓入棧中 將left樹壓入棧中 def 根據樹後序遍歷出來的結果計算表示式(傳入已經構建好的字串樹變數root) 宣告一個stack棧並將root壓入棧中 while stack棧不為空 current 獲取棧頂的樹並賦值作為當前的樹 IF current的state等於他子節點個數時 IF current.opteator不為空時 對current的opteator運算子進行判斷後將current的左右子節點的result進行運算 ELSE IF current只有一個子節點 將current的子節點的result賦值給current的result 將current出棧 IF 將當前current的parent不為空 父節點中state+=1 ELSE IF 左樹不為空 將左樹壓入stack棧中 IF 右樹不為空 將右樹壓入stack棧中 print(root.result)
def 查詢當前字元中的符號所在位置(傳入一個字串) FOR 對字串長度進行迴圈 得到當前下標的單個字元 IF 字元等於"(" 計數+1 ELSE IF 字元等於")" 計數-1 ELSE IF 字元=="+" 與 計數==0 返回當前下標 FOR 以字串最後一個字元開始進行迴圈 得到當前下標的單個字元 IF 字元等於"(" 計數+1 ELSE IF 字元等於")" 計數-1 ELSE IF 字元等於"-" 與 計數==0 返回當前下標 FOR 對字串長度進行迴圈 得到當前下標的單個字元 IF 字元等於"(" 計數+1 ELSE IF 字元等於")" 計數-1 ELSE IF (字元等於"*" 或 字元等於"/") 與 計數==0 返回當前下標 返回-1
Python實現程式碼: class Stack(object): def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def peek(self):
return self.items[len(self.items) - 1]
def size(self):
return len(self.items)
def push(self, item):
self.items.append(item)
def pop(self): return self.items.pop()
class TreeNode(object): def __init__(self, x): self.expression = x self.left = None self.right = None self.parent = None self.state = 0 self.operator = None self.result= None
def structuralTree(str): root = TreeNode(str) stack = Stack() stack.push(root) while stack.size() >0 : current = stack.peek() nums = strToTree(current.expression) if nums == -1 : if current.expression.startswith("(") and current.expression.endswith(")") : current=stack.pop() expression = current.expression[1:len(current.expression)-1]; left = TreeNode(expression) left.parent = current current.left = left stack.push(left) else: current=stack.pop() current.result=current.expression elif nums > 0 : current=stack.pop() leftStr = current.expression[0:nums] rightStr = current.expression[nums+1:len(str)] operator = current.expression[nums:nums+1] current.operator = operator leftTree = TreeNode(leftStr) rightTree = TreeNode(rightStr) leftTree.parent = current rightTree.parent = current current.left = leftTree current.right = rightTree stack.push(rightTree) stack.push(leftTree) return root
def strToTree(str): num = 0 for i in range(len(str)): if str[i] == "(": num += 1 elif str[i] == ")": num -= 1 elif str[i] == "+" and num == 0 : return i for i in range(len(str[::-1])): if str[i] == "(": num += 1 elif str[i] == ")": num -= 1 elif str[i] == "-" and num==0 : return i for i in range(len(str)): if str[i] == "(": num += 1 elif str[i] == ")": num -= 1 elif (str[i] == "*" or str[i] == "/") and num==0 : return i return -1
def postOrder(root): stack = Stack() stack.push(root) while stack.size() >0 : current = stack.peek() num = 0 if current.left != None: num += 1 if current.right != None: num += 1 if current.state == num : if current.operator != None : if current.operator == "+": current.result = int(current.left.result) + int(current.right.result) elif current.operator == "-": current.result = int(current.left.result) - int(current.right.result) elif current.operator == "*": current.result = int(current.left.result) * int(current.right.result) elif current.operator == "/": current.result = int(current.left.result) / int(current.right.result) elif current.left != None and current.right == None: current.result=current.left.result stack.pop() if current.parent != None : current.parent.state += 1 else: if current.left != None: stack.push(current.left) if current.right != None: stack.push(current.right) print(root.expression,"=",root.result)
root = structuralTree("((2+3)/5-(2+3)*4-(3-3))") postOrder(root)