当前位置: 首页 > news >正文

计算加减乘除混合运算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

相关文章:

  • 页面布局让footer居页面底部_网站页面底部固定的方法
  • ocr语种识别_【梦想云中台能力】智能图片处理OCR
  • fifo算法_LRU缓存算法的实现
  • 安卓实训项目源码_【兼职项目】预算3万开发无线温度电流传感,2万开发直流电机打磨机控制...
  • hbuilderx内置服务器启动失败_安卓应用perfect player竟然内置直播源高速观看港台卫视/影视/动画...
  • git rm -r --cached_git 撤销对文件的追踪
  • hadoop生态_大数据运营技术与工具:Hadoop生态系统
  • erp系统服务器托管_勤哲Excel服务器做门业生产企业管理ERP系统
  • python3爬虫框架scrapy_Python3环境安装Scrapy爬虫框架过程及常见错误
  • 廖雪峰python教程整理_廖雪峰老师Python3教程练习整理
  • gif图片生成器_Python几行代码制作Gif动图
  • python 运算符_Python运算符
  • html个人博客完整代码_不写代码,也能用 GitHub Pages 搭建免费个人博客
  • range函数python范围_python的range函数与切片操作符
  • python调用knn算法_在Python中使用KNN算法处理缺失的数据
  • 【347天】每日项目总结系列085(2018.01.18)
  • 78. Subsets
  • Angularjs之国际化
  • extjs4学习之配置
  • Iterator 和 for...of 循环
  • JavaScript类型识别
  • Joomla 2.x, 3.x useful code cheatsheet
  • Odoo domain写法及运用
  • PHP 7 修改了什么呢 -- 2
  • Python打包系统简单入门
  • React Native移动开发实战-3-实现页面间的数据传递
  • 深度学习中的信息论知识详解
  • 听说你叫Java(二)–Servlet请求
  • UI设计初学者应该如何入门?
  • #etcd#安装时出错
  • #HarmonyOS:软件安装window和mac预览Hello World
  • #我与Java虚拟机的故事#连载18:JAVA成长之路
  • (a /b)*c的值
  • (附源码)spring boot儿童教育管理系统 毕业设计 281442
  • (三分钟了解debug)SLAM研究方向-Debug总结
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (转)h264中avc和flv数据的解析
  • (转)shell调试方法
  • (转)大型网站架构演变和知识体系
  • (转)负载均衡,回话保持,cookie
  • (转载)PyTorch代码规范最佳实践和样式指南
  • *1 计算机基础和操作系统基础及几大协议
  • .Family_物联网
  • .NET Compact Framework 3.5 支持 WCF 的子集
  • .net framwork4.6操作MySQL报错Character set ‘utf8mb3‘ is not supported 解决方法
  • .NET MVC之AOP
  • .NET(C#、VB)APP开发——Smobiler平台控件介绍:Bluetooth组件
  • .Net下使用 Geb.Video.FFMPEG 操作视频文件
  • :如何用SQL脚本保存存储过程返回的结果集
  • @AutoConfigurationPackage的使用
  • [ C++ ] STL_stack(栈)queue(队列)使用及其重要接口模拟实现
  • [Android] Implementation vs API dependency
  • [android] 练习PopupWindow实现对话框
  • [bbk5179]第66集 第7章 - 数据库的维护 03
  • [C#]科学计数法(scientific notation)显示为正常数字