@leo9年前
2016-12-11
17:08
def infix2postfix(infix):
postfix=[]
digits=['1','2','3','4','5','6','7','8','9','0']
ohigh=['*','/']
olow=['+','-']
s=[]
i=0
while(i<len(infix)):
if infix[i] in digits:
postfix.append(infix[i])
if infix[i]=='(':
s.append(infix[i])
if infix[i] in olow:
while(len(s)!=0 and s[len(s)-1] in ohigh):
postfix.append(s[len(s)-1])
s.pop()
else:
s.append(infix[i])
if infix[i] in ohigh:
s.append(infix[i])
if infix[i]==')':
while(len(s)!=0 and s[len(s)-1]!='('):
postfix.append(s[len(s)-1])
s.pop()
if s[len(s)-1]=='(':
s.pop()
i+=1
while(len(s)!=0):
postfix.append(s[len(s)-1])
s.pop()
return postfix
print infix2postfix("6-((5+4)*(3-2)+1)")
def cal(n2,n1,operation):
if operation=='+':
return (int(n1)+int(n2))
if operation=='-':
return (int(n1)-int(n2))
if operation=='*':
return (int(n1)*int(n2))
if operation=='/':
return d(int(n1)/int(n2))
def evaluatePostfix(postfix):
digits=['1','2','3','4','5','6','7','8','9','0']
op=['+','-','*','/']
s=[]
postfix=postfix[::-1]
#print postfix
while(len(postfix)>1):
if postfix[len(postfix)-1] in digits:
s.append(postfix[len(postfix)-1])
postfix.pop()
if postfix[len(postfix)-1] in op:
operation=postfix[len(postfix)-1]
postfix.pop()
n1=s[len(s)-1]
#print n1
s.pop()
n2=s[len(s)-1]
#print n2
s.pop()
s.append(cal(n1,n2,operation))
#print postfix,s
if len(postfix)==1 and len(s)==2:
return cal(int(s[1]),int(s[0]),postfix[0])
print evaluatePostfix(infix2postfix("6-((5+4)*(3-2)+1)"))
Output:
Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.8.2] on linux
['6', '5', '4', '+', '3', '2', '-', '*', '1', '+', '-']
-4
Infix to Postfix and Evaluation of Postfix
-
Safari 18
iPhone iOS 18.3.2 -
Google Chrome 135
Windows Don’t miss a single second! Catch ‘gà chọi c1.com trực tiếp’ live and in action right here. Get ready for some heart-pounding moments! gà chọi c1.com trực tiếp
-
Google Chrome 135
Windows Can’t get to the Trường Gà C1 Thomo myself, but Gachoic1thomo is the next best thing. Feels like you’re right there in the stands! It is the bomb!!: trường gà c1 thomo
-
Google Chrome 135
Windows Want the latest scores in real time? 7m ty so ma cao is the perfect place to keep you informed. A must have if you’re following Macau sports! 7m ty so ma cao

New to PH Slots? phslotsloginregister makes it super easy to sign up and get started! No hassle, just straight to the games! Register now on phslotsloginregister!