用 Python解一元一次方程
#!python3
import re
def solve(eq, var='x'):
eq = re.sub(r'([\d\.]+)([xy])', r'\1*\2', eq)
try:
c = eval(eq.replace("=", "-(" ) + ")", {var: 1j})
x = -c.real/c.imag
except Exception as err:
print(err)
return
return x
if __name__ == '__main__':
eq = '2x +10 = 10x'
print(eq)
x = solve(eq)
print('x=%s' % x)