Python3.6基于正則實(shí)現(xiàn)的計(jì)算器示例【無優(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í)最高的*和/找出來
# 這里有幾種情況,(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:
# 括號(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 單行多行萬能正則匹配方法
- Python3使用正則表達(dá)式爬取內(nèi)涵段子示例
- python3爬蟲之入門基礎(chǔ)和正則表達(dá)式
- python3正則模塊re的使用方法詳解
相關(guān)文章
Python使用BeautifulSoup(bs4)解析復(fù)雜的HTML內(nèi)容
在 Web 開發(fā)和數(shù)據(jù)分析中,解析 HTML 是一個(gè)常見的任務(wù),尤其是當(dāng)你需要從網(wǎng)頁中提取數(shù)據(jù)時(shí),Python 提供了多個(gè)庫來處理 HTML,其中最受歡迎的就是 BeautifulSoup,本文將介紹如何使用 bs4 的 BeautifulSoup 庫來解析復(fù)雜的 HTML 內(nèi)容,需要的朋友可以參考下2024-11-11
Python實(shí)現(xiàn)藍(lán)線挑戰(zhàn)特效的示例代碼
在抖音曾經(jīng)火了一陣子的藍(lán)線挑戰(zhàn)特效,其原理很簡(jiǎn)單。本文將試著用opencv-python實(shí)現(xiàn)這個(gè)效果,做了攝像頭版本和視頻處理版本,感興趣的可以學(xué)習(xí)一下2022-10-10
利用Python爬蟲爬取金融期貨數(shù)據(jù)的案例分析
從技術(shù)角度來看,經(jīng)過一步步解析,任務(wù)是簡(jiǎn)單的,入門requests爬蟲及入門pandas數(shù)據(jù)分析就可以完成,本文重點(diǎn)給大家介紹Python爬蟲爬取金融期貨數(shù)據(jù)的案例分析,感興趣的朋友一起看看吧2022-06-06
pandas時(shí)間序列之如何將int轉(zhuǎn)換成datetime格式
這篇文章主要介紹了pandas時(shí)間序列之如何將int轉(zhuǎn)換成datetime格式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
感知器基礎(chǔ)原理及python實(shí)現(xiàn)過程詳解
這篇文章主要介紹了感知器基礎(chǔ)原理及python實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09

