-
Notifications
You must be signed in to change notification settings - Fork 0
/
mydc.py
91 lines (82 loc) · 2.75 KB
/
mydc.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from liststack import ListStack
import sys
class MyDC:
operator_list = ["+", "-", "*", "/", "%", "p", "q"]
def __init__(self):
# stack = [4, 8.9, 2,...]
self.stack = ListStack()
def run(self):
while True:
# step 1: "3 4.1 p q"
line = input()
# step2 : ["(3)", "(4.1)", "p"]
line = MyDC.__read(line)
# step3: stack = [3, 4.1] print("4.1")
MyDC.__handle(line, self.stack)
@staticmethod
def __read(line):
''' 分割 "3 4.1 + p q 8" --> ['(3)', '(4.1)', 'p', 'q'] '''
line = line + " "
list_ = list()
# 用于累加成一个数值的临时变量
temp = str()
for item in line:
# 0 ~ 9 or . 如, item = "4.1"
if item >= '0' and item <= '9' or item == '.':
temp += item
continue
else:
# 停止上一步累计
if temp:
# "6" --> "(6)"
list_.append(MyDC.format(temp))
# 清空累计变量
temp = str()
# 合法输入
if item in MyDC.operator_list:
list_.append(item)
# 'q'不再录入后续命令
if item == 'q':
break
# 输入空格
elif item == ' ':
continue
# 非法输入
else:
raise Exception()
return list_
@staticmethod
def __handle(command_line, stack):
#打印处理的命令列表
#print(command_line)
for command in command_line:
if command == 'p':
print(stack.gettop())
elif command == 'q':
print("quit")
break
# '+' '-' '*' '/' pop2 push1
elif command in MyDC.operator_list:
try:
num1 = str(stack.pop())
num2 = str(stack.pop())
except IndexError:
raise IndexError("pop from empty stack")
else:
rst = eval(num2 + command + num1)
stack.push(rst)
else:
# 0 ~ 9 command "(6)" --> rst 6 --> stack.push
rst = MyDC.format(command, reverse = True)
stack.push(rst)
# 打印处理完命令后的栈结果
#print("stack.list:", stack.list)
@classmethod
def format(cls, item, reverse=False):
if reverse:
return eval(item.strip("()"))
else:
return "(" + str(item) + ")"
if __name__ == "__main__":
dc = MyDC()
dc.run()