File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Stack :
2
+
3
+ def __init__ (self ):
4
+ self .stack = []
5
+
6
+ def push (self , item ):
7
+ self .stack .append (item )
8
+
9
+ def pop (self ):
10
+ return self .stack .pop ()
11
+
12
+ def isEmpty (self ):
13
+ return self .stack == []
14
+
15
+ def top (self ):
16
+ return self .stack [self .size () - 1 ]
17
+
18
+ def size (self ):
19
+ return len (self .stack )
20
+
21
+ def evaluator (string ):
22
+ length = len (string )
23
+ operator_stack = Stack ()
24
+ operand_stack = Stack ()
25
+
26
+ for x in range (0 , length ):
27
+ item = string [x ]
28
+ print item
29
+ if (item not in "()+-*/" ):
30
+ operand_stack .push (item )
31
+
32
+
33
+ elif item == ')' :
34
+ operand1 = int (operand_stack .pop ())
35
+ operand2 = int (operand_stack .pop ())
36
+ operator = operator_stack .pop ()
37
+
38
+ if operator == '+' :
39
+ operand_stack .push (operand1 + operand2 )
40
+ elif operator == '-' :
41
+ operand_stack .push (operand2 - operand1 )
42
+ elif operator == '*' :
43
+ operand_stack .push (operand2 * operand1 )
44
+ elif operator == '/' :
45
+ operand_stack .push (operand2 / operand1 )
46
+
47
+ elif item in "+-/*" :
48
+ operator_stack .push (item )
49
+
50
+ return operand_stack .pop ()
51
+
52
+ print (evaluator ("(2+3)" ))
You can’t perform that action at this time.
0 commit comments