You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
4.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from math import *
import re
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("÷", "/")
# 替换所有的括号
input = input.replace("", "(")
input = input.replace("", ")")
# 替换所有的减号为 Python 中的运算符
input = input.replace("", "-")
# 替换所有的加号为 Python 中的运算符
input = input.replace("", "+")
# 替换所有的²为 Python 中的运算符
input = input.replace("²", "**2")
# 替换所有的³为 Python 中的运算符
input = input.replace("³", "**3")
# 替换所有的绝对值符号为 abs()
l=input.split("|")
n=[2*i+1 for i in range(len(l)-1)]
for i in n:
if n.index(i)%2==0:
l.insert(i,"abs(")
else:
l.insert(i,")")
o = ""
for i in l:
o += i
input = o
# 替换开根号符号为 **0.5,正确匹配作用域
# 正则说明:匹配 '√' 后的括号内容(如 √(a+b))或连续非运算符字符(如 √5、√ab
input = re.sub(r'√([a-zA-Z0-9_.()]+)', r'(\1)**0.5', input)
# 替换所有的 ln 为自然对数函数 log(),支持小数点和变量
# 正则说明:匹配 'ln' 后紧跟的数字(含小数点)、字母、下划线(如 5.6、x、var_1 等)
input = re.sub(r'ln([a-zA-Z0-9_.]+)', r'log(\1)', input)
input = input.replace("ln","log")
# 替换所有乘方符号为 Python 中的 ** 运算符
input = input.replace("^", "**")
return input
def test_convert_formula():
# 测试乘除符号转换
assert convert_formula("3×4÷2") == "3*4/2", "乘除符号转换失败"
# 测试括号转换
assert convert_formula("3 + 4") == "(3 + 4)", "括号转换失败"
# 测试加减符号转换
assert convert_formula("53﹣2") == "5+3-2", "加减符号转换失败"
# 测试平方和立方符号转换
assert convert_formula("2² + 3³") == "2**2 + 3**3", "平方和立方符号转换失败"
# 测试绝对值符号转换
assert convert_formula("|-5|") == "abs(-5)", "绝对值符号转换失败"
# 测试自然对数符号转换
assert convert_formula("ln5.6 + lnx + ln(2x)") == "log(5.6) + log(x) + log(2x)", "自然对数符号转换失败"
# 测试开根号符号转换
assert convert_formula("√9") == "(9)**0.5", "开根号符号转换失败"
# 测试乘方符号转换
assert convert_formula("2^3") == "2**3", "乘方符号转换失败"
# 增加一个包含所有转换的测试用例
assert convert_formula("3×5÷√(ln4)﹣|-3|³") == "3*(5+2**2)/((log(4)))**0.5-abs(-3)**3", "包含所有转换的测试失败"
print("所有测试用例通过!")
def split_function(function):
"""
将形如f(x)=x^2的函数表达式拆分成自变量和表达式两部分
"""
independent_variable=function.split("=")[0].split("(")[1].split(")")[0]
expression=function.split("=")[1]
return independent_variable, expression
def test_split_function():
# 测试函数表达式拆分
assert split_function("f(x)=x^2") == ("x", "x**2"), "函数表达式拆分失败"
# 测试自变量提取
assert split_function("f(x)=x^2")[0] == "x", "自变量提取失败"
# 测试表达式提取
assert split_function("f(x)=x^2")[1] == "x**2", "表达式提取失败"
# 增加一个复杂的函数表达式拆分测试用例
assert split_function("g(x)=3x^2+5ln(x)") == ("x", "3*x**2+5*log(x)"), "复杂函数表达式拆分测试失败"
# 调用测试函数
if __name__ == "__main__":
test_convert_formula()