Python3.6基于正則實現(xiàn)的計算器示例【無優(yōu)化簡單注釋版】
本文實例講述了Python3.6基于正則實現(xiàn)的計算器。分享給大家供大家參考,具體如下:
# -*- coding:utf-8 -*- #!python3 import re import copy def my_calc(inside): """ 計算括號內(nèi)的算術(shù)式 :param inside:算術(shù)式 :return:結(jié)果 """ while True: # 1、首先需要把含有優(yōu)先級最高的*和/找出來 # 這里有幾種情況,(1*1) (1*-1) (-1*1)除法類似(暫時不考慮分母為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)) # */都運算完以后就可以做+-了 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ù)運算 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)
運行結(jié)果:
PS:這里再為大家推薦幾款計算工具供大家進一步參考借鑒:
在線一元函數(shù)(方程)求解計算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi
科學計算器在線使用_高級計算器在線計算:
http://tools.jb51.net/jisuanqi/jsqkexue
在線計算器_標準計算器:
http://tools.jb51.net/jisuanqi/jsq
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
- 對python3中的RE(正則表達式)-詳細總結(jié)
- Python3正則匹配re.split,re.finditer及re.findall函數(shù)用法詳解
- Python3中正則模塊re.compile、re.match及re.search函數(shù)用法詳解
- pycharm使用正則表達式批量添加print括號完美從python2遷移到python3
- 詳解Python3中的正則表達式的基本用法
- python3.x提取中文的正則表達式示例代碼
- python3正則提取字符串里的中文實例
- Python3 單行多行萬能正則匹配方法
- Python3使用正則表達式爬取內(nèi)涵段子示例
- python3爬蟲之入門基礎(chǔ)和正則表達式
- python3正則模塊re的使用方法詳解
相關(guān)文章
翻轉(zhuǎn)數(shù)列python實現(xiàn),求前n項和,并能輸出整個數(shù)列的案例
這篇文章主要介紹了翻轉(zhuǎn)數(shù)列python實現(xiàn),求前n項和,并能輸出整個數(shù)列的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05python PIL中ImageFilter模塊圖片濾波處理和使用方法
這篇文章主要介紹PIL中ImageFilter模塊幾種圖片濾波處理和使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-11-11