首页 > 代码库 > Python 正则实现计算器

Python 正则实现计算器

 1 1 # !/usr/bin/env/ python3
 2  2 # -*- coding: utf-8 -*-
 3  3 
 4  4 """用户输入计算表达式,显示计算结果"""
 5  5 
 6  6 __author__ = Jack
 7  7 
 8  8 import re
 9  9 
10 10 bracket = re.compile(r\([^()]+\))  # 寻找最内层括号规则
11 11 mul = re.compile(r(\d+\.?\d*\*-\d+\.?\d*)|(\d+\.?\d*\*\d+\.?\d*))  # 寻找乘法运算规则
12 12 div = re.compile(r(\d+\.?\d*/-\d+\.?\d*)|(\d+\.?\d*/\d+\.?\d*))  # 寻找除法运算规则
13 13 add = re.compile(r(-?\d+\.?\d*\+-\d+\.?\d*)|(-?\d+\.?\d*\+\d+\.?\d*))  # 寻找加法运算规则
14 14 sub = re.compile(r(\d+\.?\d*--\d+\.?\d*)|(\d+\.?\d*-\d+\.?\d*))  # 寻找减法运算规则
15 15 c_f = re.compile(r\(?\+?-?\d+\)?)  # 检查括号内是否运算完毕规则
16 16 strip = re.compile(r[^(].*[^)])  # 脱括号规则
17 17 
18 18 
19 19 def Mul(s):
20 20     """计算表达式中的乘法运算"""
21 21     exp = re.split(r\*, mul.search(s).group())
22 22     return s.replace(mul.search(s).group(), str(float(exp[0]) * float(exp[1])))
23 23 
24 24 
25 25 def Div(s):
26 26     """计算表达式中的除法运算"""
27 27     exp = re.split(r/, div.search(s).group())
28 28     return s.replace(div.search(s).group(), str(float(exp[0]) / float(exp[1])))
29 29 
30 30 
31 31 def Add(s):
32 32     """计算表达式中的加法运算"""
33 33     exp = re.split(r\+, add.search(s).group())
34 34     return s.replace(add.search(s).group(), str(float(exp[0]) + float(exp[1])))
35 35 
36 36 
37 37 def Sub(s):
38 38     """计算表达式中的减法运算"""
39 39     exp = re.split(r-, sub.search(s).group())
40 40     return s.replace(sub.search(s).group(), str(float(exp[0]) - float(exp[1])))
41 41 
42 42 
43 43 def calc():
44 44     while True:
45 45         s = input(Please input the expression(q for quit):)  # 例:‘1+2- (3*  4-3/2+ (   3-2*(3+  5 -3*  -0.2-3.3*2.2 -8.5/ 2.4 )+10) +10)‘
46 46         if s == q:
47 47             break
48 48         else:
49 49             s = ‘‘.join([x for x in re.split(\s+, s)])  # 将表达式按空格分割并重组
50 50             if not s.startswith(():  # 若用户输入的表达式首尾无括号,则统一格式化为:(表达式)
51 51                 s = str((%s) % s)
52 52             while bracket.search(s):  # 若表达式s存在括号
53 53                 s = s.replace(--, +)  # 检查表达式,并将--运算替换为+运算
54 54                 s_search = bracket.search(s).group()  # 将最内层括号及其内容赋给变量s_search
55 55                 if div.search(s_search):  # 若除法运算存在(必须放在乘法之前)
56 56                     s = s.replace(s_search, Div(s_search))  # 执行除法运算并将结果替换原表达式
57 57                 elif mul.search(s_search):  # 若乘法运算存在
58 58                     s = s.replace(s_search, Mul(s_search))  # 执行乘法运算并将结果替换原表达式
59 59                 elif sub.search(s_search):  # 若减法运算存在(必须放在加法之前)
60 60                     s = s.replace(s_search, Sub(s_search))  # 执行减法运算并将结果替换原表达式
61 61                 elif add.search(s_search):  # 若加法运算存在
62 62                     s = s.replace(s_search, Add(s_search))  # 执行加法运算并将结果替换原表达式
63 63                 elif c_f.search(s_search):  # 若括号内无任何运算(类似(-2.32)除外)
64 64                     s = s.replace(s_search, strip.search(s_search).group())  # 将括号脱掉,例:(-2.32)---> -2.32
65 65 
66 66             print(The answer is: %.2f % (float(s)))
67 67 
68 68 if __name__ == __main__:
69 69     calc()

 

Python 正则实现计算器