Python 70行代码实现简单算式计算器解析

yipeiwu_com5年前Python基础

描述:用户输入一系列算式字符串,程序返回计算结果。

要求:不使用eval、exec函数。

实现思路:找到当前字符串优先级最高的表达式,在算术运算中,()优先级最高,则取出算式最底层的(),再进行加减乘除运算。对于加减乘除,也要确立一个优先级,可以使用一个运算符列表,用for循环逐个处理运算符,并且要考虑同级情况(如for遍历至*时,也要考虑同级别的\是否要提前运算)。不断循环上述过程,直到最终得到一个结果。

关键点:使用re模块匹配出当前状态下优先级最高的算式。

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

实现代码:

import re
'''根据本逻辑,‘-'必须早于‘+'循环 否则特殊情况会报错
  原因是若出现符号--,会被处理为+,若+优先遍历,最后+将无法被处理'''
oper_char = ['^','*','/','-','+']
def format_str(s):
  '''除去空格和两边括号'''
  return s.replace(' ','').replace('(','').replace(')','')
 
def handle_symbol(s):
  '''处理多个运算符并列的情况'''
  return s.replace('+-','-').replace('--','+').replace('-+','-').replace('++','+')
 
def cal(x,y,opertor):
  '''加减乘除开方'''
  if opertor == '^':return x**y
  elif opertor == '*':return x*y
  elif opertor == '/':return x/y
  elif opertor == '+':return x+y
  elif opertor == '-':return x-y
 
def Bottom_operation(s):
  '''无括号运算 返回一个浮点数
    symbol用于判断返回值是正还是负'''
  symbol = 0
  s = handle_symbol(s)
  for c in oper_char:
    while c in s:
      id,char = (s.find(c),c)
      if c in ('*','/') and '*' in s and '/' in s:
        ids,idd = (s.find('*'),s.find('/'))
        id,char = (ids,'*') if ids <= idd else (idd,'/')
      if c in ('+','-') and '+' in s and '-' in s:
        ida,idd = (s.find('+'),s.find('-'))
        id,char = (ida,'+') if ida <= idd else (idd,'-')
      if id == -1:break
      left,right = ('','')
      for i in range(id - 1,-1,-1):
        if s[i] in oper_char:break
        left = s[i] + left
      for i in range(id + 1,len(s)):
       if s[id+1] == '-':
         right += s[i]
        continue
        if s[i] in oper_char:break
        right += s[i]
      if right == '' or left == '':
        if s[0] in ('-','+'):
          if '+' not in s[1:] and '-' not in s[1:]:break
          s = s[1:].replace('-','负').replace('+','-').replace('负','+')
          symbol += 1
          continue
        else:return '输入算式有误'
      old_str = left + char + right
      new_str = str(cal(float(left),float(right),char))
      s = handle_symbol(s.replace(old_str,new_str))
  return float(s) if symbol % 2 == 0 else -float(s)
 
def get_bottom(s):
  '''获取优先级最高的表达式'''
  res = re.search('\([^()]+\)',s)
  if res != None:return res.group()
 
if __name__ == '__main__':
  while True:
    s1 = input('请输入您要计算的表达式(支持加减乘除开方): ')
    while get_bottom(s1) != None:
      source = get_bottom(s1)
      result = Bottom_operation(format_str((source)))
      s1 = s1.replace(source,str(result))
    print(Bottom_operation(format_str(s1)))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python实现快速多线程ping的方法

本文实例讲述了Python实现快速多线程ping的方法。分享给大家供大家参考。具体如下: #!/usr/bin/python #_*_coding:utf-8_*_ # ''' 名称...

python使用 request 发送表单数据操作示例

python使用 request 发送表单数据操作示例

本文实例讲述了python使用 request 发送表单数据操作。分享给大家供大家参考,具体如下: # !/usr/bin/env python # -*- coding: utf-...

Python原始字符串与Unicode字符串操作符用法实例分析

Python原始字符串与Unicode字符串操作符用法实例分析

本文实例讲述了Python原始字符串与Unicode字符串操作符用法。分享给大家供大家参考,具体如下: #coding=utf8 ''''' 在原始字符串里,所有的字符串都是直接按照...

Python语言检测模块langid和langdetect的使用实例

之前使用数据编码风格检测的模块chardet比较多一点,今天提到的两个模块是检测数据的语言类型,比如是:中文还是英文,模块的使用方法也比较简单,我这里只是简单地使用了一下,因为项目中有这...

Python 文本文件内容批量抽取实例

Python 文本文件内容批量抽取实例

Python新手编写脚本处理数据,各种心酸各种语法查找,以此留念! 原始数据格式如下图所示: 这里是一个人脸测试数据,其中每行第一个为测试图片编号,后面为Top 7图片编号及其对应的评...