求方程 ax²+bx+c=0的根,用3个函数分别求当b²-4ac大于0、等于0和小于0时的根并输出结果。从主函数输入a、b、c的值。
时间: 2023-09-10 18:04:50 浏览: 204
### 回答1:
可以使用以下三个函数来求解方程根:
```python
import math
def roots_when_b2_minus_4ac_greater_than_0(a, b, c):
delta = b**2 - 4*a*c
x1 = (-b + math.sqrt(delta))/(2*a)
x2 = (-b - math.sqrt(delta))/(2*a)
return x1, x2
def roots_when_b2_minus_4ac_equals_0(a, b, c):
x = -b/(2*a)
return x
def roots_when_b2_minus_4ac_less_than_0(a, b, c):
real_part = -b/(2*a)
imaginary_part = math.sqrt(abs(b**2 - 4*a*c))/(2*a)
x1 = complex(real_part, imaginary_part)
x2 = complex(real_part, -imaginary_part)
return x1, x2
```
然后在主函数中,可以根据判别式的值来分别调用三个函数,并输出结果:
```python
a = float(input("请输入a的值:"))
b = float(input("请输入b的值:"))
c = float(input("请输入c的值:"))
delta = b**2 - 4*a*c
if delta > 0:
x1, x2 = roots_when_b2_minus_4ac_greater_than_0(a, b, c)
print("方程的两个实根分别为:", x1, x2)
elif delta == 0:
x = roots_when_b2_minus_4ac_equals_0(a, b, c)
print("方程的唯一实根为:", x)
else:
x1, x2 = roots_when_b2_minus_4ac_less_than_0(a, b, c)
print("方程的两个共轭复根分别为:", x1, x2)
```
注意,在判断 delta 的值时,使用的是 `>` 和 `==`,而不是 `>=`,因为当 delta 小于 0 时,无法求得实根,会报错。
### 回答2:
下面是一个用三个函数分别求取一元二次方程根的示例,根据输入的a、b、c的值,输出当b²-4ac大于0、等于0和小于0时的根的结果。
```python
import math
def quadratic_roots_greater_than_zero(a, b, c):
delta = b * b - 4 * a * c
if delta > 0:
x1 = (-b + math.sqrt(delta)) / (2 * a)
x2 = (-b - math.sqrt(delta)) / (2 * a)
return x1, x2
else:
return None
def quadratic_roots_equal_to_zero(a, b, c):
delta = b * b - 4 * a * c
if delta == 0:
x = -b / (2 * a)
return x
else:
return None
def quadratic_roots_less_than_zero(a, b, c):
delta = b * b - 4 * a * c
if delta < 0:
real_part = -b / (2 * a)
imaginary_part = math.sqrt(-delta) / (2 * a)
x1 = complex(real_part, imaginary_part)
x2 = complex(real_part, -imaginary_part)
return x1, x2
else:
return None
def main():
a = float(input("请输入a的值:"))
b = float(input("请输入b的值:"))
c = float(input("请输入c的值:"))
roots1 = quadratic_roots_greater_than_zero(a, b, c)
roots2 = quadratic_roots_equal_to_zero(a, b, c)
roots3 = quadratic_roots_less_than_zero(a, b, c)
print("当b²-4ac大于0时,根为:", roots1)
print("当b²-4ac等于0时,根为:", roots2)
print("当b²-4ac小于0时,根为:", roots3)
if __name__ == "__main__":
main()
```
通过以上的代码,您可以在主函数中输入a、b、c的值,并且根据b²-4ac的值选择对应的函数进行调用,然后输出结果。
### 回答3:
可以使用以下三个函数来求方程 ax² + bx + c = 0 的根,并输出结果。
1. 当 b² - 4ac 大于 0 时,方程有两个实根。
```python
def solve_equation_large(a, b, c):
delta = b**2 - 4*a*c
if delta > 0:
x1 = (-b + delta**0.5) / (2*a)
x2 = (-b - delta**0.5) / (2*a)
return x1, x2
else:
return None
```
2. 当 b² - 4ac 等于 0 时,方程有一个实根。
```python
def solve_equation_equal(a, b, c):
delta = b**2 - 4*a*c
if delta == 0:
x = -b / (2*a)
return x
else:
return None
```
3. 当 b² - 4ac 小于 0 时,方程没有实根,只有复数根。
```python
def solve_equation_small(a, b, c):
delta = b**2 - 4*a*c
if delta < 0:
real_part = -b / (2*a)
imaginary_part = (-delta)**0.5 / (2*a)
x1 = complex(real_part, imaginary_part)
x2 = complex(real_part, -imaginary_part)
return x1, x2
else:
return None
```
从主函数中输入 a、b、c 的值,并根据 b² - 4ac 的值调用相应的函数输出结果。
```python
a = float(input("请输入 a 的值:"))
b = float(input("请输入 b 的值:"))
c = float(input("请输入 c 的值:"))
root_large = solve_equation_large(a, b, c)
root_equal = solve_equation_equal(a, b, c)
root_small = solve_equation_small(a, b, c)
if root_large:
print("方程的两个实根为:", root_large)
elif root_equal:
print("方程的一个实根为:", root_equal)
elif root_small:
print("方程的两个复数根为:", root_small)
else:
print("方程无解。")
```
注意:上述代码只是演示了一种实现方式,具体实现可能还需要根据实际情况进行适当修改。
阅读全文