[Python] simple calculator implementation (1)

Solution: First, use regular expressions to separate all the numbers and operators in the input formula. In this process, it is necessary to judge whether "-" is a negative sign or a minus sign.

Define the function: def formula_format(formula)

Step 1: Use the sub function to remove spaces inthe input formula .

formula = re.sub(' ', '', formula)

Step 2: Separate the content in the form of "-number" from the rest of the content in the formula, and put each content into the list formula_list. The regular expression is " (-\d+\.{0,1}\d*) ", considering the negative number -123.456, " - " matches the negative sign, " \d+ " matches the digits before the decimal point, " \.{0, 1} " matches one or zero decimal points, and " \d* " matches digits after the decimal point.

formula_list = [i for i in re.split('(-\d+\.{0,1}\d*)', formula) if i]

# 我猜等价于
formula_list = []
formula_list += re.split('(-\d+\.{0,1}\d*)', formula)

Step 3: Determine whether the divided content in the form of "-number" is a negative number, and separate the numbers other than negative numbers from the operators connected to them.

# 用于存放数字和运算符
final_formula = []

Loop judgment for each segment of the segmented formula:

Case 1: For the first paragraph of formula, if it is a number starting with "-", it must be a negative number, so it will not be processed, and it will be directly put into final_formula, and then enter the next cycle. Q: Do I have to write ^ and $?

for item in formula_list:
        if len(final_formula) == 0 and re.search('^-\d+\.{0,1}\d*$', item):
            final_formula.append(item)
            continue

Case 2: For non-first paragraph formulas, if the last element in final_formal is an operator ['+', '-', '*', '/', '('], then "-number" is a negative number .In other words, if "X" in the form "X-number" is a number, then "-number" must not be a negative number. final_formula[-1] represents the last element in the list, that is, for the formula "- number", which is the "X" in "X-number".

if len(final_formula) > 0:
    if re.search('[\+\-\*\/\(]$', final_formula[-1]):
        final_formula.append(item)
        continue

# 我感觉只要横杠前面不是数字就行
# re.search('[^\d]$', final_formula[-1])

Case 3: For a formula that does not meet the previous two conditions, it is an ordinary formula that does not have the possibility of "containing negative numbers", as long as all the numbers and operators in it are separated.

# 按照运算符分割开
item_split = [i for i in re.split('([\+\-\*\/\(\)])', item) if i]
# 全部添加到final_formula中
final_formula += item_split

Step 4: Return final_formula list.


entire code

import re

def formula_format(formula):
    formula = re.sub(' ', '', formula)
    formula_list = [i for i in re.split('(-\d+\.{0,1}\d*)', formula) if i]

    final_formula = []
    for item in formula_list:
        if len(final_formula) == 0 and re.search('^-\d+\.{0,1}\d*$', item):
            final_formula.append(item)
            continue
 
        if len(final_formula) > 0:
            if re.search('[\+\-\*\/\(]$', final_formula[-1]):
                final_formula.append(item)
                continue

        item_split = [i for i in re.split('([\+\-\*\/\(\)])', item) if i]
        final_formula += item_split
    return final_formula

Guess you like

Origin blog.csdn.net/m0_64140451/article/details/131744909