增加输入转换的更多功能

main
Mu_Xin 11 months ago
parent 5e80a7840e
commit 038b09b0ee

@ -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("÷", "/")

Loading…
Cancel
Save