class Stack(object):
def __init__(self):
self.stack=[]
def isEmpty(self):
return self.stack==[]
def push(self,item):
self.stack.append(item)
def pop(self):
if self.isEmpty():
raise IndexError,'pop from empty stack'
return self.stack.pop()
def peek(self):
return self.stack[-1]
def size(self):
return len(self.stack)
def match(i,j):
opens='([{'
closes=')]}'
return opens.index(i)==closes.index(j)
def syntaxChecker(string):
stack=Stack()
balanced=True
for i in string:
if i in '([{':
stack.push(i)
elif i in ')]}':
if stack.isEmpty():
balanced=False
break
else:
j=stack.pop()
if not match(j,i):
balanced=False
break
if not stack.isEmpty():
balanced=False
return balanced
def decimal_to_bin(dec):
stack=Stack()
cur=dec
while cur>0:
a=cur%2
cur=cur/2
stack.push(a)
binstr=''
while not stack.isEmpty():
binstr+=str(stack.pop())
return binstr
def infixtoPostfix(infix):
a={}
a['*']=3
a['/']=3
a['+']=2
a['-']=2
a['(']=1
stack=Stack()
post=''
for i in infix:
if i not in a and i!=')':
post+=i
elif i=='(':
stack.push(i)
elif i==')':
top=stack.pop()
while top!='(':
post+=top
top=stack.pop()
else:
while not stack.isEmpty() and a[i]<=a[stack.peek()]:
post+=stack.pop()
stack.push(i)
while not stack.isEmpty():
post+=stack.pop()
return post
def postfixExp(postfix):
stack=Stack()
postlist=postfix.split()
for i in postlist:
if i not in '+-*/':
stack.push(i)
else:
a=stack.pop()
b=stack.pop()
result=math(i,b,a)
stack.push(result)
return stack.pop()
def math(x,y,z):
if x=='+':
return float(y)+float(z)
if x=='-':
return float(y)-float(z)
if x=='*':
return float(y)*float(z)
if x=='/':
return float(y)/float(z)
class Queue(object):
def __init__(self):
self.queue=[]
def isEmpty(self):
return self.queue==[]
def enqueue(self,x):
self.queue.append(x)
def dequeue(self):
if self.queue:
a=self.queue[0]
self.queue.remove(a)
return a
else:
raise IndexError,'queue is empty'
def size(self):
return len(self.queue)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有