|
|
|
|
@ -6,6 +6,12 @@ def convert_formula(input):
|
|
|
|
|
将输入的数学表达式转换为 Python 格式,并以字符串形式返回转换后的表达式
|
|
|
|
|
"""
|
|
|
|
|
input = str(input)
|
|
|
|
|
trig_functions = ['sin', 'cos', 'tan', 'cot', 'sec', 'csc', 'arctan', 'arcsin', 'arccos']
|
|
|
|
|
for func in trig_functions:
|
|
|
|
|
# 匹配场景:函数名后紧跟字母/数字(如sinx→sin(x),cos2x→cos(2x))
|
|
|
|
|
# 正则说明:\b确保匹配完整函数名,([a-zA-Z0-9])捕获后续字符
|
|
|
|
|
input = re.sub(rf'\b{func}([a-zA-Z0-9])', rf'{func}(\1)', input)
|
|
|
|
|
input = re.sub(r'(\d+\.?\d*|\.\d+|\))([a-zA-Z]|\d+\.?\d*|\.\d*|\()', r'\1*\2', input)
|
|
|
|
|
# 替换所有的乘除符号为 Python 中的运算符
|
|
|
|
|
input = input.replace("×", "*")
|
|
|
|
|
input = input.replace("÷", "/")
|
|
|
|
|
|