Программа для решения уравнения любого вида записи:
например x^2+4=5x
from math import sqrt
def slove(ur):
a,b,c,n = 0,0,0,1
for t in [x.replace("+",";").replace("-",";-").split(";") for x in ur.split("=")]:
for x in t:
if "x^2" in x:
x=x.replace("x^2","")
a+=(1 if x=="" else float(x))*n
elif "x" in x:
x=x.replace("x","")
b+=(1 if x=="" else float(x))*n
else:
c+=(0 if x=="" else float(x))*n
n*=-1
d = b*b - 4*a*c
return -1*(b/(2*a)) if d==0 else [(sqrt(d)-b)/(2*a),(-1*sqrt(d)-b)/(2*a)]
print(slove("x^2+4=5x"))
print(slove("1x^2-5x+4=0"))