import mathdef gz(a, b, c): delta = b**2 - 4*a*c if delta > : x1 = (-b + math.sqrt(delta)) / (2*a) x2 = (-b - math.sqrt(delta)) / (2*a) return round(x1, 3), round(x2, 3) elif delta == : x = -b / (2*a) return round(x, 3) else: return "无实数根"def ez(a, b, c): delta = b**2 - 4*a*c if delta > : x1 = (-b + math.sqrt(delta)) / (2*a) x2 = (-b - math.sqrt(delta)) / (2*a) return round(x1, 3), round(x2, 3) elif delta == : x = -b / (2*a) return round(x, 3) else: x1 = complex(-b / (2*a), math.sqrt(-delta) / (2*a)) x2 = complex(-b / (2*a), -math.sqrt(-delta) / (2*a)) return round(x1, 3), round(x2, 3)def sz(a, b, c): delta = b**2 - 4*a*c if delta > : x1 = (-b + math.sqrt(delta)) / (2*a) x2 = (-b - math.sqrt(delta)) / (2*a) return round(x1, 3), round(x2, 3) elif delta == : x = -b / (2*a) return round(x, 3) else: p = -b / (2*a) q = math.sqrt(-delta) / (2*a) return f"{round(p, 3)}+{round(q, 3)}i", f"{round(p, 3)}-{round(q, 3)}i"# 主函数a = float(input("请输入a的值:"))b = float(input("请输入b的值:"))c = float(input("请输入c的值:"))print("当b^2-4ac大于时的根为:", gz(a, b, c))print("当b^2-4ac等于时的根为:", ez(a, b, c))print("当b^2-4ac小于时的根为:", sz(a, b, c))
时间: 2023-05-28 20:03:36 浏览: 129
这段代码定义了三个函数,用于求解一元二次方程的解。其中,函数gz()用于求解有实数解的情况,函数ez()用于求解有复数解的情况,函数sz()用于求解有虚数解的情况。函数的输入参数为方程的三个系数a、b、c,返回值为方程的解。
阅读全文