Python3.6基于正則實(shí)現(xiàn)的計(jì)算器示例【無(wú)優(yōu)化簡(jiǎn)單注釋版】
本文實(shí)例講述了Python3.6基于正則實(shí)現(xiàn)的計(jì)算器。分享給大家供大家參考,具體如下:
# -*- coding:utf-8 -*-
#!python3
import re
import copy
def my_calc(inside):
"""
計(jì)算括號(hào)內(nèi)的算術(shù)式
:param inside:算術(shù)式
:return:結(jié)果
"""
while True:
# 1、首先需要把含有優(yōu)先級(jí)最高的*和/找出來(lái)
# 這里有幾種情況,(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ù)式找出來(lái)
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:
# 括號(hào)都算完了,如果還有算術(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é)果:

PS:這里再為大家推薦幾款計(jì)算工具供大家進(jìn)一步參考借鑒:
在線一元函數(shù)(方程)求解計(jì)算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi
科學(xué)計(jì)算器在線使用_高級(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文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- 對(duì)python3中的RE(正則表達(dá)式)-詳細(xì)總結(jié)
- Python3正則匹配re.split,re.finditer及re.findall函數(shù)用法詳解
- Python3中正則模塊re.compile、re.match及re.search函數(shù)用法詳解
- pycharm使用正則表達(dá)式批量添加print括號(hào)完美從python2遷移到python3
- 詳解Python3中的正則表達(dá)式的基本用法
- python3.x提取中文的正則表達(dá)式示例代碼
- python3正則提取字符串里的中文實(shí)例
- Python3 單行多行萬(wàn)能正則匹配方法
- Python3使用正則表達(dá)式爬取內(nèi)涵段子示例
- python3爬蟲之入門基礎(chǔ)和正則表達(dá)式
- python3正則模塊re的使用方法詳解
相關(guān)文章
TensorFlow中權(quán)重的隨機(jī)初始化的方法
本篇文章主要介紹了TensorFlow中權(quán)重的隨機(jī)初始化的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
翻轉(zhuǎn)數(shù)列python實(shí)現(xiàn),求前n項(xiàng)和,并能輸出整個(gè)數(shù)列的案例
這篇文章主要介紹了翻轉(zhuǎn)數(shù)列python實(shí)現(xiàn),求前n項(xiàng)和,并能輸出整個(gè)數(shù)列的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
使用Python的Flask框架來(lái)搭建第一個(gè)Web應(yīng)用程序
Flask框架是一個(gè)以輕量級(jí)著稱的Web開(kāi)發(fā)框架,近兩年來(lái)在Web領(lǐng)域獲得了極高的人氣,這里我們就來(lái)看如何使用Python的Flask框架來(lái)搭建第一個(gè)Web應(yīng)用程序2016-06-06
python PIL中ImageFilter模塊圖片濾波處理和使用方法
這篇文章主要介紹PIL中ImageFilter模塊幾種圖片濾波處理和使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
python詞云庫(kù)wordcloud自定義詞云制作步驟分享
這篇文章主要介紹了python詞云庫(kù)wordcloud自定義詞云制作步驟分享,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08

