Commit 6180fd72 by mrk022

### worked on tutor.py

parent 8ecfff01
 ... ... @@ -17,8 +17,11 @@ class TimesNode: return eval(evalStr) else: return self.left.eval() * self.right.eval()''' #if isinstance(self.left, VariableNode): # return self.left.getMultiplier() * self.left.getMultiplier() #if isinstance(self.left, VariableNode) and isinstance(self.right,VariableNode): # return self.left.eval()*self.right.eval() #elif isinstance(self.left,VariableNode) or isinstance(self.right,VariableNode): # print("can't combine variables of differing types") #need some sort of combined node here i think #else: return self.left.eval() * self.right.eval() ... ... @@ -106,6 +109,7 @@ OPERATORS = set(['+', '-', '*', '/', '(', ')']) PRIORITY = {'+':1, '-':1, '*':2, '/':2} def infix_to_prefix(formula): # this method was taken from open source https://github.com/lilianweng/LeetcodePython/blob/master/expression.py op_stack = [] exp_stack = [] for ch in formula.split(): ... ... @@ -139,14 +143,14 @@ def infix_to_prefix(formula): def eval_tree(expression): x = NumNode(5) '''x = NumNode(5) y = NumNode(4) p = PlusNode(x,y) t = TimesNode(p, NumNode(6)) root = PlusNode(t, NumNode(3)) print(root.eval()) print(root.inorder()) print(root.inorder())''' #x = input("Please enter a prefix expression: ") x = infix_to_prefix(expression) ... ... @@ -159,9 +163,10 @@ def eval_tree(expression): root = E(q) result = root.eval() print(root.eval()) print(root.inorder()) return result #print(infix_to_prefix("3 + 4 - 6")) def split_equation(equation): ... ... @@ -169,9 +174,8 @@ def split_equation(equation): return expressions def main(): exp = split_equation("4x * 3x") eval_tree(exp[0]) exp = split_equation("4x * 3x + 2") x = eval_tree(exp[0]) if __name__ == "__main__": ... ...