计算加减乘除混合运算python实现_python,实现计算器程序,加减乘除混合运算加括号,完善实现...

作业:要求实现一个由键盘输入表达式,计算结果的小程序.

import re

#detect error input

s = input("Expression:")

s = re.sub('\s',"",s)

if re.findall('[^0-9\+\-\*/\)\(]',s):

print("Error input,Stop!")

exit()

def judge1(sign):

if sign == '*':

obj = re.compile('\*')

elif sign == '/':

obj = re.compile('/')

return(obj)

def judge2(sign):

if sign == '+':

obj = re.compile('\+')

elif sign == '-':

obj = re.compile('\-')

return(obj)

def Format(s):

s = s.replace(' ','')

s = s.replace('++','+')

s = s.replace('+-','-')

s = s.replace('-+','-')

s = s.replace('--','+')

return(s)

def calculate(s):

while re.search('[\*,/]',s):

s = Format(s)

obj = judge1(re.search('[\*,/]',s).group())

res = obj.split(s)

first = re.search('[\d,\.]+$',res[0]).group()

second = re.search('^[\d,\.,\-]+',res[1]).group()

if re.search('\.',first):

fir = float(first)

else:

fir = int(first)

if re.search('\.',second):

sec = float(second)

else:

sec = int(second)

if re.search('[\*,/]',s).group() == '*':

result = str((fir * sec))

s = re.sub('%s\*%s'%(first,second),result,s)

print('s=',s)

elif re.search('[\*,/]',s).group() == '/':

result = str(fir / sec)

s = re.sub('%s/%s'%(first,second),result,s)

print('s=',s)

if re.search('^\-[\d,\.]+',s):

return(s)

s = Format(s)

# return(s)

while re.search('[\+,\-]',s):

s = Format(s)

obj = judge2(re.search('[\+,\-]',s).group())

res = obj.split(s)

first = re.search('[\d,\.]+$',res[0]).group()

second = re.search('^[\d,\.,\-]+',res[1]).group()

if re.search('\.',first):

fir = float(first)

else:

fir = int(first)

if re.search('\.',second):

sec = float(second)

else:

sec = int(second)

if re.search('[\+,\-]',s).group() == '+':

result = str(fir + sec)

s = re.sub('%s\+*%s'%(first,second),result,s)

print('s=',s)

elif re.search('[\+,\-]',s).group() == '-':

result = str(fir - sec)

s = re.sub('%s\-%s'%(first,second),result,s)

print('=',s)

if re.search('^\-[\d,\.]+',s):

return(s)

s= Format(s)

return(s)

def first_step(s):

while re.search('\([^()]+\)',s):

res = re.search('\([^()]+\)',s)

s_temp = res.group()

print(s_temp)

[first,second] = s.split(s_temp)

ret = re.search('\([^()]+\)',s)

tmp = re.search('[^()]+',ret.group())

print(tmp.group())

temp = str(calculate(tmp.group()))

print('temp=',temp)

s = first + temp + second

print('first=',first)

print('second=',second)

print('temp=',temp)

print('new s=',s)

# if re.search('[\+,\-]{1}',s):

# break

result = calculate(s)

print("result=",result)

first_step(s)

花了两天时间终于完成这个小作业了,其实没什么难度,就是正则表达式的灵活运用,由于逻辑比较复杂,测试花费了很多时间.

标签:search,group,python,re,second,计算器,print,加减乘除,first

来源: https://www.cnblogs.com/iceberg710815/p/11965177.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值