-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackMachine.py
More file actions
47 lines (36 loc) · 1.11 KB
/
Copy pathStackMachine.py
File metadata and controls
47 lines (36 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class StackMachine:
stack = []
def __init__(self):
self.stack = []
def push(self,num):
self.stack.append(num)
def pop(self):
if len(self.stack) == 0:
return "None"
else:
return self.stack.pop()
def add(self):
if(len(self.stack) == 2):
ans = int(self.stack[0]) + int(self.stack[1])
self.stack[0] = ans
self.stack.pop()
def sub(self):
if(len(self.stack) == 2):
ans = int(self.stack[0]) - int(self.stack[1])
self.stack[0] = ans
self.stack.pop()
def mul(self):
if(len(self.stack) == 2):
ans = int(self.stack[0]) * int(self.stack[1])
self.stack[0] = ans
self.stack.pop()
def div(self):
if(len(self.stack) == 2):
ans = int(self.stack[0]) / int(self.stack[1])
self.stack[0] = ans
self.stack.pop()
def mod(self):
if(len(self.stack) == 2):
ans = int(self.stack[0]) % int(self.stack[1])
self.stack[0] = ans
self.stack.pop()