溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python3.6基于正則實(shí)現(xiàn)的計(jì)算器示例【無優(yōu)化簡單注釋版】

發(fā)布時(shí)間:2020-10-25 00:08:07 來源:腳本之家 閱讀:190 作者:Tyran_U 欄目:開發(fā)技術(shù)

本文實(shí)例講述了Python3.6基于正則實(shí)現(xiàn)的計(jì)算器。分享給大家供大家參考,具體如下:

# -*- coding:utf-8 -*-
#!python3
import re
import copy
def my_calc(inside):
  """
  計(jì)算括號內(nèi)的算術(shù)式
  :param inside:算術(shù)式
  :return:結(jié)果
  """
  while True:
    # 1、首先需要把含有優(yōu)先級最高的*和/找出來
    # 這里有幾種情況,(1*1) (1*-1) (-1*1)除法類似(暫時(shí)不考慮分母為0的情況)
    # 但是有了正則就方便多了
    search_ret = re.search('(\(-)?\d+(\.\d+)?[*/]-?\d+(\.\d+)?', inside)
    if search_ret is None:
      break
    ret_str = search_ret.group()
    if '(' in ret_str:
      ret_str = ret_str[1:]
    num_list = re.split('[*/]', ret_str)
    operator = re.search('[*/]', ret_str).group()
    calc_ret = 0
    if operator == '*':
      calc_ret = float(num_list[0]) * float(num_list[1])
    elif operator == '/':
      calc_ret = float(num_list[0]) / float(num_list[1])
    inside = inside.replace(ret_str, str(calc_ret))
  # */都運(yùn)算完以后就可以做+-了
  while True:
    # 2、把含有+-的算術(shù)式找出來
    search_ret = re.search('(\(-)?\d+(\.\d+)?[+\-]-?\d+(\.\d+)?', inside)
    if search_ret is None:
      break
    ret_str = search_ret.group()
    if '(' in ret_str:
      ret_str = ret_str[1:]
    tmp_str = copy.copy(ret_str)
    num_1 = re.match('-?\d+(\.\d+)?', tmp_str).group()
    tmp_str = tmp_str.replace(num_1, '')
    operator = tmp_str[0]
    num_2 = tmp_str[1:]
    calc_ret = 0
    if operator == '+':
      calc_ret = float(num_1) + float(num_2)
    elif operator == '-':
      calc_ret = float(num_1) - float(num_2)
    inside = inside.replace(ret_str, str(calc_ret))
  return re.sub('[()]', '', inside)
def format_str(s):
  s = s.replace('--', '+')
  s = s.replace('-+', '-')
  s = s.replace('++', '+')
  s = s.replace('+-', '-')
  if s[0] == '+':
    s = s[1:]
  s = '('+s+')'
  return s
def un_bracket_calc(final_str): # -1*2+3-4/-5
  final_str = format_str(final_str)
  final_str = my_calc(final_str)
  return final_str
def my_math(s): # "((-1-2*-3)/(3-2)+(9*5-89)*(2*3*(3-0)))"
  while True:
    inside_bracket = re.search('[()]+[()]+', s)
    if inside_bracket is None:
      # 括號都算完了,如果還有算術(shù)式繼續(xù)運(yùn)算
      s = un_bracket_calc(s)
      break
    src_str = inside_bracket.group()
    ret = my_calc(src_str)
    s = s.replace(src_str, ret)
  return s
s_src = "((-1 - 2 * -3) / (3 - 2) + (9 * 5 - 9) * (2 * 3 * (3 - 0))) * -100 + 99-100 * -1-1"
s_src = s_src.replace(' ', '')
print(my_math(s_src))
s_ret = ((-1 - 2 * -3) / (3 - 2) + (9 * 5 - 9) * (2 * 3 * (3 - 0))) * -100 + 99 - 100 * -1 - 1
print(s_ret)

運(yùn)行結(jié)果:

Python3.6基于正則實(shí)現(xiàn)的計(jì)算器示例【無優(yōu)化簡單注釋版】

PS:這里再為大家推薦幾款計(jì)算工具供大家進(jìn)一步參考借鑒:

在線一元函數(shù)(方程)求解計(jì)算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi

科學(xué)計(jì)算器在線使用_高級計(jì)算器在線計(jì)算:
http://tools.jb51.net/jisuanqi/jsqkexue

在線計(jì)算器_標(biāo)準(zhǔn)計(jì)算器:
http://tools.jb51.net/jisuanqi/jsq

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對大家Python程序設(shè)計(jì)有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI