在GOF的书中指出:如果一种特定类型的问题发生的频率足够高,那么可能就值得将该问题的各个实例表述为一个简单语言中的句子。这样就可以构建一个解释器,该解释器通过解释这些句子来解决该问题。而且当文法简单、效率不是关键问题的时候效果最好。
更多相关资料:http://www.cnblogs.com/cbf4life/archive/2009/12/17/1626125.html
import sys class priority:#定义符号优先级 symbol = '#+-*/%()' def getisp(self,a):#栈内优先权 if '#'.find(a)!=-1: return 0 if '('.find(a)!=-1: return 1 if '-+'.find(a)!=-1: return 3 if '*/%'.find(a)!=-1: return 5 if ')'.find(a)!=-1: return 6 def geticp(self,a): #栈外优先权 if '#'.find(a)!=-1: return 0 if '('.find(a)!=-1: return 6 if '-+'.find(a)!=-1: return 2 if '*/%'.find(a)!=-1: return 4 if ')'.find(a)!=-1: return 1 class Expression:#抽象类 def interpreter(): pass class VarExpression(Expression):#数字,终结表达式 def __init__(self,_key): self.key=_key def interpreter(self): return self.key class SymbolExpression(Expression):#运算类基类 def __init__(self,_left,_right): self.left = _left self.right = _right class SubExpression(SymbolExpression):#减 def __init__(self,_left,_right): self.parent=SymbolExpression(_left,_right) def interpreter(self): return self.parent.left.interpreter()-self.parent.right.interpreter() class AddExpression(SymbolExpression):#加 def __init__(self,_left,_right): self.parent=SymbolExpression(_left,_right) def interpreter(self): return self.parent.left.interpreter()+self.parent.right.interpreter() class MulExpression(SymbolExpression):#乘 def __init__(self,_left,_right): self.parent=SymbolExpression(_left,_right) def interpreter(self): return self.parent.left.interpreter()*self.parent.right.interpreter() class DivExpression(SymbolExpression):#除 def __init__(self,_left,_right): self.parent=SymbolExpression(_left,_right) def interpreter(self): return self.parent.left.interpreter()/self.parent.right.interpreter() class ModExpression(SymbolExpression):#取余 def __init__(self,_left,_right): self.parent=SymbolExpression(_left,_right) def interpreter(self): return self.parent.left.interpreter()%self.parent.right.interpreter() class Calculator: exp='' order=[] def __init__(self,_exp): _exp = _exp.replace(' ','') self.exp=_exp _exp=_exp+'#' i=0 f=0 p=1 tmp = 0 stack='#' order=[] pty = priority() while len(stack)>0: if pty.symbol.find(_exp[i])!=-1: if f>0:order = order+[tmp] f = 0 p=1 tmp = 0 ch = _exp[i] ch1 = stack[-1] #print (ch1,' ',pty.getisp(ch1),' ',pty.geticp(ch)) if pty.getisp(ch1)<pty.geticp(ch): stack=stack+ch i=i+1 elif pty.getisp(ch1)>pty.geticp(ch): order = order+[stack[-1]] stack = stack[0:-1] else : if stack[-1]=='(': i=i+1 stack = stack[0:-1] else : if f==0: f = 1 if _exp[i]=='.': f = 2 elif f==1: tmp = tmp*10 +int(_exp[i])-int('0') else: p = p*0.1 tmp = tmp+(int(_exp[i])-int('0'))*p i=i+1 print (order) #order 为后缀表达式 self.order=order stack=[] for i in order:#创建递归树 if pty.symbol.find(str(i))!=-1: right = stack[-1] # 把栈中连个元素取出 left = stack[-2] stack = stack[0:-2] if '+'.find(str(i))!=-1: stack = stack+[AddExpression(left,right)] elif '-'.find(str(i))!=-1: stack = stack+[SubExpression(left,right)] elif '*'.find(str(i))!=-1: stack = stack+[MulExpression(left,right)] elif '/'.find(str(i))!=-1: stack = stack+[DivExpression(left,right)] elif '%'.find(str(i))!=-1: stack = stack+[ModExpression(left,right)] else : stack=stack+[VarExpression(i)] self.expression = stack[0] def run(self):#计算递归树 return self.expression.interpreter() def Client(): a=input("请输入正确表达式:eg:1*(2+3):\n") cal = Calculator(a) print (a,"=",cal.run()) Client()