-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.py
More file actions
196 lines (170 loc) · 5.71 KB
/
Copy patheval.py
File metadata and controls
196 lines (170 loc) · 5.71 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
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
195
196
# python
# eval()
# from CS GUI calculator
# perform the calculation from the display
import debug_tools as debug
from MyLib import MStack, MQueue
def operate(operator, operand1, operand2):
operand1, operand2 = float(operand1), float(operand2)
if operator == '+':
return operand1 + operand2
if operator == '-':
return operand1 - operand2
if operator == '*':
return operand1 * operand2
if operator == '/':
return operand1 / operand2
def list_to_number(digit_list):
# convert a list of digits to an integer
result = 0
for digit in digit_list:
result *= 10
result += digit
return result
def precedence(operator):
PRECEDENCE = {
'√': 1,
'+': 2,
'-': 2,
'×': 3,
'÷': 3,
'^': 4,
'(': 99999,
# Not used for actually converting to prefix, used to test operators not missing
')': 99999,
}
return PRECEDENCE[operator]
class Node:
OPERATOR = 1
OPERAND = 2
def __init__(self, type=None, value=None):
self.type = type
self.value = value
def __str__(self):
return f'type: {self.type}, value: {self.value}'
class Calculator:
# calculator class for calculating
def __init__(self):
# TODO:
# - decide prefix or postfix -->> Using prefix, maybe postfix later
# - implement infix to pre/post - fix
# - implement basic operation with operator and 2 operands
# - implement an set of functions to let the other file call
# - create a python stub (pyi)
# start stack with top at index 0
self.__input_stack = MStack()
self.__infix_equation = MQueue()
self.__prefix_equation = MQueue()
self.result = 0
self.__infix = ''
def __prepare_infix(self):
operators = ['+', '-', '×', '÷', '√', '^', '(', ')']
tmp = ''
idx = 0
while idx < len(self.__infix):
if self.__infix[idx] in operators:
if len(tmp) != 0:
self.__infix_equation.push(Node(Node.OPERAND, tmp))
tmp = ''
self.__infix_equation.push(Node(Node.OPERATOR, self.__infix[idx]))
else:
tmp += self.__infix[idx]
idx += 1
if len(tmp) != 0:
self.__infix_equation.push(Node(Node.OPERAND, tmp))
def __infix_to_prefix(self):
# convert infix into prefix
'''
for each item in MStack
if is an operand:
output
else
if stk is empty
push stk
continue
if operator == (
push stk
continue
if operator == )
while stk top not (
output
pop stk
continue
if operator precedence <= stk top precedence
push stk
continue
while operator precedence > stk top precedence
output
else
push stk
while stk is not empty
output
reverse result
'''
stk = MStack()
for item in self.__infix_equation:
if item.type == Node.OPERAND:
self.__prefix_equation.push(item)
else:
if stk.empty():
stk.push(item)
continue
if item.value == '(':
stk.push(item)
continue
if item.value == ')':
while stk.top().value != '(':
self.__prefix_equation.push(stk.pop())
stk.pop()
continue
if precedence(item.value) > precedence(stk.top().value):
stk.push(item)
continue
while not stk.empty() and precedence(item.value) <= precedence(stk.top().value):
self.__prefix_equation.push(stk.pop())
else:
stk.push(item)
while not stk.empty():
self.__prefix_equation.push(stk.pop())
# reverse result
self.__prefix_equation.lst = self.__prefix_equation.lst[::-1]
def __calculate_prefix(self):
'''
for item in prefix equation
if item is operator
push into stack
else
while stack top is operand1
operand2 = pop stack
operator = pop stack
operate operand1 operator operand2
push result
'''
stk = MStack()
for item in self.__prefix_equation:
if item.type == Node.OPERATOR:
stk.push(item)
elif stk.top().type == Node.OPERAND:
while not stk.empty() and stk.top().type == Node.OPERAND:
operand = stk.pop()
operator = stk.pop()
item = Node(Node.OPERAND, operate(operator.value, item.value, operand.value))
stk.push(item)
else:
stk.push(item)
return stk.top().value
def input(self, char):
self.__infix += char
return self.__infix
def get_infix(self):
return self.__infix
def clear_infix(self):
self.__infix = ''
def calculate(self):
self.__prepare_infix()
self.__infix_to_prefix()
debug.log('prefix equation is: ' + str(self.__prefix_equation))
result = self.__calculate_prefix()
debug.log('result: ' + str(result))
self.clear_infix()
return result