团队-象棋游戏-模块开发过程
2017-11-01 19:40
239 查看
# -*- encoding: utf-8 -*-
def md(l,x):
a = l.index(x)
if x == '*' and l[a + 1] != '-':
k = float(l[a - 1]) * float(l[a + 1])
elif x == '/' and l[a + 1] != '-':
k = float(l[a - 1]) / float(l[a + 1])
elif x == '*' and l[a + 1] == '-':
k = -(float(l[a - 1]) * float(l[a + 2]))
elif x == '/' and l[a + 1] == '-':
k = -(float(l[a - 1]) / float(l[a + 2]))
del l[a - 1], l[a - 1], l[a - 1]
l.insert(a - 1, str(k))
return l
def fun(s):
l = re.findall('([\d\.]+|/|-|\+|\*)',s)
sum=0
while 1:
if '*' in l and '/' not in l:
md(l, '*')
elif '*' not in l and '/' in l:
md(l, '/')
elif '*' in l and '/' in l:
a = l.index('*')
b = l.index('/')
if a < b:
md(l, '*')
else:
md(l, '/')
else:
if l[0]=='-':
l[0]=l[0]+l[1]
del l[1]
sum += float(l[0])
for i in range(1, len(l), 2):
if l[i] == '+' and l[i + 1] != '-':
sum += float(l[i + 1])
elif l[i] == '+' and l[i + 1] == '-':
sum -= float(l[i + 2])
elif l[i] == '-' and l[i + 1] == '-':
sum += float(l[i + 2])
elif l[i] == '-' and l[i + 1] != '-':
sum -= float(l[i + 1])
break
return sum
实现运算过程
def calculate(expression):
ex=[]
ans=0
if '(' not in expression:
ans=fun(expression)
return ans
for i in range(len(expression)):
if expression[i]=='(':
ex.append(i)
elif expression[i]==')':
temp=0
sub=expression[ex[len(ex)-1]+1:i]
temp=fun(sub)
expression=expression[0:ex[len(ex)-1]]+str(temp)+expression[i+1:len(expression)+1]
ex.pop()
return calculate(expression)
s='1 - 2 * ( (60-30 +(-40/5+3) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
print(1 - 2 * ( (60-30 +(-40/5+3) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) ))
s3='3*(4+50)-((100+40)*5/2-3*2*2/4+9)*(((3+4)-4)-4)'
print(3*(4+50)-((100+40)*5/2-3*2*2/4+9)*(((3+4)-4)-4))
print(calculate(s))
print(calculate(s3))
相关文章推荐
- 团队-象棋游戏-模块开发过程
- 团队-象棋游戏-模块测试过程
- 团对-象棋游戏-开发环境搭建过程
- 团对-象棋游戏-开发环境搭建过程
- 团队编程 象棋游戏 开发文档
- 团队-象棋游戏-开发文档
- 团队-象棋游戏-模块测试过程
- 团队-象棋游戏-代码设计规范
- 游戏开发过程中所涉及的工作人员
- Android小兔子跳铃铛游戏开发过程和心得
- 小团队游戏开发的阶段划分
- 【Unity】游戏开发过程中的前后台切换技术
- 网络游戏服务器开发过程中小对象内存管理
- 基于pygame的象棋游戏一:搭建程序开发环境
- 使用C++进行Cocos2d-x游戏开发入门过程中的要点解析
- 鲸鱼游戏后端开发工程师职位面试过程回顾
- 中国象棋游戏开发计划
- StarCraft的工程师谈美国的游戏开发过程
- J2ME 拼图游戏 快速开发 全过程 之代码祥解(2 )——游戏数据库与游戏模型开发
- 寻找游戏合作开发团队