forked from wjw12/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usestack.py
194 lines (175 loc) · 4.85 KB
/
usestack.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
class SStack():
def __init__(self):
self.elems = [ ]
def is_empty(self):
return self.elems == [ ]
def top(self):
if self.elems == [ ]:
raise StackUnderflow
return self.elems[len(self.elems)-1]
def push(self, elem):
self.elems.append(elem)
def pop(self):
if self.elems == [ ]:
raise StackUnderflow
return self.elems.pop()
class ESStack(SStack):
def depth(self):
return len(self.elems)
#############################################
######## Paretheses checker #################
def check_pares(text):
pares = "()[]{}"
open_pares = "([{"
opposite = {")":"(", "]":"[", "}":"{"}
def paretheses(text):
i, text_len = 0, len(text)
while True:
while i < text_len and text[i] not in pares:
i += 1
if i >= text_len:
return
yield text[i], i
i += 1
st = SStack()
for pr, i in paretheses(text):
if pr in open_pares: # push open pares into stack
st.push(pr)
elif st.pop() != opposite[pr]:
print("Unmatching is found at", i, "for", pr)
return False
print("All paretheses are correctly matched.")
return True
################################################
####### Suffix expression evaluator ############
def suffix_exp_evaluator(line):
return suf_exp_evaluator(line.split())
def suf_exp_evaluator(exp):
"""exp is a list of items representing a suffix expression.
This function evaluates it and return its value.
"""
operators = "+-*/"
st = ESStack()
for x in exp:
if not x in operators:
st.push(float(x))
continue
if st.depth() < 2:
raise SyntaxError("Short of operand(s).")
a = st.pop() # second argument
b = st.pop() # first argument
if x == "+":
c = b + a
elif x == "-":
c = b - a
elif x == "*":
c = b * a
elif x == "/":
if a == 0: raise ZeroDivisionError
c = b / a
else:
pass # This branch is not possible
st.push(c)
if st.depth() == 1:
return st.pop()
raise SyntaxError("Extra operand(s).")
# end suf_exp_evaluator
def suffix_exp_calculator():
"""Repeatly ask for expression input until an 'end'."""
while True:
try:
line = input("Suffix Expression: ")
if line == "end":
return
res = suffix_exp_evaluator(line)
print(res)
except Exception as ex:
print("Error:", type(ex), ex.args)
def suffix_demo():
print(suffix_exp_evaluator("1"))
print(suffix_exp_evaluator("1 2 +"))
print(suffix_exp_evaluator("1 3 + 2 *"))
print(suffix_exp_evaluator("1 3 + 2 5 - *"))
#####################################################
##### Transform infix expression to suffix expression
priority = {"(":1, "+":3, "-":3, "*":5, "/":5}
infix_operators = "+-*/()"
numbers = "1234567890."
def tokens(line):
""" This function cannot deal with signed numbers,
nor unary operators.
"""
line = line.replace(' ','')
i, llen = 0, len(line)
while i < llen:
if line[i] == '-':
if i == 0 or line[i-1] == '(':
j = i+1
while j < llen and line[j] in numbers:
j += 1
yield line[i:j]
i = j
continue
if line[i] in infix_operators:
yield line[i]
i += 1
continue
if line[i] in numbers:
j = i+1
while j < llen and line[j] in numbers:
j += 1
yield line[i:j]
i = j
def trans_infix_suffix(line):
st = SStack()
llen = len(line)
exp = []
for x in tokens(line):
if x not in infix_operators:
exp.append(x)
elif st.is_empty() or x == '(':
st.push(x)
elif x == ')':
while not st.is_empty() and st.top() != "(":
exp.append(st.pop())
if st.is_empty():
raise SyntaxError("Missing \'(\'.")
st.pop() # discard left parethesis
else: # consider all ops left-associative
while (not st.is_empty() and priority[st.top()] >= priority[x]):
exp.append(st.pop())
st.push(x)
while not st.is_empty():
if st.top() == "(":
raise SyntaxError("Extra \'(\' in expression.")
exp.append(st.pop())
return exp
def test_trans_infix_suffix(s):
print(s)
print(trans_infix_suffix(s))
print("Value:", suf_exp_evaluator(trans_infix_suffix(s)))
def demo_trans():
test_trans_infix_suffix("1.25")
test_trans_infix_suffix("1 + 2")
test_trans_infix_suffix("1 + 2 - 3")
test_trans_infix_suffix("1 + 2 * 3")
test_trans_infix_suffix("7. / 2 * 3")
test_trans_infix_suffix("(1 + 2) * 3")
test_trans_infix_suffix("1 + 2 * 3 - 5")
test_trans_infix_suffix("13 + 2 * (3 - 5)")
test_trans_infix_suffix("(1 + 2) * (3 - 5)")
test_trans_infix_suffix("(1 + (2 * 3 - 5)) / .25")
if __name__ == "__main__":
## check_pares("")
## check_pares("()")
## check_pares("([]{})")
## check_pares("([]{}]")
## check_pares("(abbvbb[hhh]jhg{lkii288}9000)000fhjsh")
## check_pares("jkdsjckd(mfkk[fdjjfk],,,{kckjfc}jskdjkc]kkk")
## suffix_exp_calculator()
while True:
line = input ("Test line:")
test_list = list()
for i in tokens(line):
test_list.append(i)
print (test_list)