forked from souravjain540/Basic-Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfix_to_postfix.py
More file actions
35 lines (24 loc) · 895 Bytes
/
infix_to_postfix.py
File metadata and controls
35 lines (24 loc) · 895 Bytes
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
Operators= set(["+","-","*","/","(",")","^"])
Priority = {"-":1,"+":1,"*":2,"/":2,"^":3}
def infix_to_postfix(expression):
stack=[]
output =""
for character in expression:
if character not in Operators:
output+=character
elif character == "(":
stack.append("(")
elif character==")":
while stack and stack[-1] != "(":
output += stack.pop()
stack.pop()
else:
while stack and stack[-1] != "(" and Priority[character] <= Priority[stack[-1]]:
output += stack.pop()
stack.append(character)
while stack:
output+=stack.pop()
return output
expression= input("Enter infix expression ")
print("infix notation: ",expression)
print("postfix notation: ",infix_to_postfix(expression))