-
Notifications
You must be signed in to change notification settings - Fork 0
/
Infix to prefix.py
142 lines (125 loc) · 3.99 KB
/
Infix to prefix.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
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
if self.items == []:
return True
else:
return False
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def top(self):
return self.items[-1]
def reverse(expression):
num = Stack()
RevInfix = []
for i in expression[::-1]:
if i in ["0","1","2","3","4","5","6","7","8","9"]:
num.push(i)
elif i == " ":
continue
elif i in ["+", "-", "*", "/", "^", "(", ")"]:
while not num.is_empty():
RevInfix.append(num.pop())
if i == "(":
RevInfix.append(")")
elif i == ")":
RevInfix.append("(")
else:
RevInfix.append(i)
while not num.is_empty():
RevInfix.append(num.pop())
return RevInfix
def reverse2(expression):
num = Stack()
RevInfix = []
for i in expression[::-1]:
if i in ["0","1","2","3","4","5","6","7","8","9"]:
num.push(i)
elif i == " ":
while not num.is_empty():
RevInfix.append(num.pop())
RevInfix.append(' ')
elif i in ["+", "-", "*", "/", "^"]:
RevInfix.append(i)
while not num.is_empty():
RevInfix.append(num.pop())
return RevInfix
def RevInfixToPrefix(infix):
i = 0
s = Stack()
postfix = []
higher_precedence = {"(": 1, "+": 2, "-": 2, "*": 3, "/": 3, "^": 4}
while i < len(infix):
if infix[i] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
while i < len(infix) and infix[i] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
postfix.append(infix[i])
i += 1
postfix.append(' ')
elif infix[i] == '(':
s.push(infix[i])
i += 1
elif infix[i] == ')':
while s.top() != '(':
postfix.append(s.pop())
postfix.append(" ")
s.pop()
i += 1
elif infix[i] in ['+', '-', '*', '/', '^']:
while not s.is_empty() and higher_precedence[s.top()] > higher_precedence[infix[i]]:
postfix.append(s.pop())
postfix.append(' ')
s.push(infix[i])
i += 1
else:
i += 1
while not s.is_empty():
postfix.append(s.pop())
postfix.append(' ')
i += 1
return postfix
def calculate(prefix):
calc = Stack()
i = len(prefix)-1
while i >= 0:
j = i-1
k = i
if prefix[i] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
while j > 0 and prefix[j] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
j -= 1
i -= 1
calc.push(int("".join(prefix[j+1:k+1])))
i -= 1
elif prefix[i] in ['+', '-', '*', '/', '^']:
if prefix[i] == "+":
x = calc.pop() + calc.pop()
calc.push(x)
i -= 1
elif prefix[i] == "-":
x = calc.pop() - calc.pop()
calc.push(x)
i -= 1
elif prefix[i] == "*":
x = calc.pop() * calc.pop()
calc.push(x)
i -= 1
elif prefix[i] == "/":
x = calc.pop() / calc.pop()
calc.push(x)
i -= 1
elif prefix[i] == "^":
x = calc.pop() ** calc.pop()
calc.push(x)
i -= 1
else:
i -= 1
return calc.top()
infix = input().strip()
ReversedInfix = reverse(infix)
postfix = RevInfixToPrefix(ReversedInfix)
Prefix = reverse2(postfix)
print("Infix : ", infix)
print("Prefix: ", "".join(Prefix))
print(calculate(Prefix))