python实现一元二次表达式求值

import math
import cmath
flag="yes"
while True:
flag= (input("是否计算新方程的根:"))
if (flag=="yes"):
print('a*x*x+b*x+c=0')
a= float (input('a='))
b= float (input('b='))
c= float (input('c='))
p=b*b-4*a*c

if p<0:
pp=str("√"+str(-p))
x1=str((-b)/(2*a))+"+"+str((1/2/a))+pp+"i"
x2=str((-b)/(2*a))+"-"+str((1/2/a))+pp+"i"
print("无实根,两个虚根:"+str(x1),str(x2))

if p>0:
x1 = (-b+math.sqrt(p))/(2*a)
x2 = (-b-math.sqrt(p))/(2*a)
print("两个不相等的实根:"+str(x1),str(x2))
if p==0:
x1 = (-b + math.sqrt(p)) / (2 * a)
print("两个相等的实根:"+str(x1))


else:
break;

posted @ 2020-12-21 14:56  Java民工陆小凤  阅读(402)  评论(0编辑  收藏  举报