python regular calculator function
- 2020-06-15 09:40:20
- OfStack
This article is an example to share python regular calculator function of the specific code for your reference, the specific content is as follows
# -*- coding: utf-8 -*-
# Author :Gogh
# @Time :2017/12/4 20:16
# @Email :361910002@qq.com
import re
def operator_update(formula):
# right formula formula Remove null characters and update operator processing
formula = formula.replace(" ", "") # Null-out character
formula = formula.replace("+-", "-")
formula = formula.replace("--", "+")
return formula
def calc_muldiv(formula_list):
'''''
Calculate the multiplication and division in the formula
:param formula: The list of
:return:
'''
for index, element in enumerate(formula_list):
if "*" in element or "/" in element:
operators = re.findall("[*/]", element)
calc_list = re.split("[*/]", element)
num = None
for i, e in enumerate(calc_list):
if num:
if operators[i - 1] == "*":
num *= float(e)
elif operators[i - 1] == "/":
num /= float(e)
else:
num = float(e)
formula_list[index] = num
return formula_list
def calc_plumin(operators, num_list):
'''''
Calculate the addition and subtraction of list Numbers
:param operators: Operator list
:param num_list: A list of Numbers to perform an operation on
:return: Return the calculated result
'''
num = None
for i, e in enumerate(num_list):
if num:
if operators[i - 1] == "+":
num += float(e)
elif operators[i - 1] == "-":
num -= float(e)
else:
num = float(e)
return num
def merge(plus_minus_operator, multiply_divide_list):
'''''
Put the list in this form '2*' '-3*' '5/3*' '4/2' Incorporated into the 1 block
:param formula_list:
:return:
'''
for index, element in enumerate(multiply_divide_list):
if element.endswith("*") or element.endswith("/"):
multiply_divide_list[index] = element + plus_minus_operator[index] + multiply_divide_list[index + 1]
del multiply_divide_list[index + 1]
del plus_minus_operator[index]
return merge(plus_minus_operator, multiply_divide_list)
return plus_minus_operator, multiply_divide_list
def bracket_calc(formula):
'''''
The innermost pair of parentheses formula Formula calculation
:param formula:
:return:
'''
formula = re.sub("[()]", "", formula) # Get rid of the () on both sides
formula = operator_update(formula)
plus_minus_operator = re.findall("[+-]", formula) # The list of '+' '-' The operator
multiply_divide_list = re.split("[+-]", formula) # The list of There are '*' '/'
if multiply_divide_list[0] == "": # multiply_divide_list The list of the first 1 If a character is empty, it means 1 The Numbers are negative
multiply_divide_list[1] = "-" + multiply_divide_list[1]
del plus_minus_operator[0]
del multiply_divide_list[0]
res = merge(plus_minus_operator, multiply_divide_list)
plus_minus_operator = res[0] # The list of '+' '-' The operator Merge processing
multiply_divide_list = res[1]
plus_minus_list = calc_muldiv(multiply_divide_list) # Generates a list that only adds and subtracts
res = calc_plumin(plus_minus_operator, plus_minus_list)
return res
def calculate(formula):
''''' Compute program main entry , The main logic is to compute the value in the extension first , And then we can multiply and divide , Then the add and subtract '''
while True:
formula_depth = re.search("[()]+", formula)
if formula_depth:
formula_depth = formula_depth.group()
res = bracket_calc(formula_depth)
formula = formula.replace(formula_depth, str(res))
print("\33[34;1m%s\33[0m" % (formula))
else:
res = bracket_calc(formula)
print("\33[31;1m The results of :%s\33[0m" % (res))
exit()
if __name__ == '__main__':
formula = "1 - 2 * ( (60-30 +(-9-2- 5-2*-3-5/3-40*4/2-3/5+6*3) * (-9-2-5-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) -(-4*3)/ (16-3*2) )"
calculate(formula)