-
Notifications
You must be signed in to change notification settings - Fork 64
/
postfix.py
executable file
·38 lines (33 loc) · 1.02 KB
/
postfix.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
class exp:
def __init__(x):
x.ex="("+(raw_input("enter expression: "))+")"
x.l=[]
x.q=["^"]
x.w=["/","*","%"]
x.e=["+","-"]
x.r=""
x.s=[]
for i in x.ex:
x.l.append(i)
def postfix(x):
for i in x.l:
if i=='(':x.s+='('
elif i in x.q:x.s+=i
elif i in x.w:
if x.s[-1] in x.q or x.s[-1] in x.w:
x.r+=x.s.pop()
x.s+=i
else:x.s+=i
elif i in x.e:
if x.s!=[]:
if x.s[-1] in x.q or x.s[-1] in x.w or x.s[-1] in x.e:
x.r+=x.s.pop()
x.s+=i
elif i==")":
for i in x.s[::-1]:
del x.s[len(x.s)-1]
if i=="(":
break
else:x.r+=i
else:x.r+=i
return x.r