3编程计算并输出一元二次方程 ax²+bx+c=0 的两个实根 , \frac {-b \pm \sqrt {b^{2}-4ac}}{2a}, 其中a、b、c的值由用户从键盘 输入,假设a、b、c的值能保证方程有两个不相等的实根(即 b²-4ac>0)。C语言
时间: 2023-05-14 14:06:35 浏览: 196
代码如下:
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, x1, x2;
printf("请输入一元二次方程的系数a、b、c:\n");
scanf("%lf %lf %lf", &a, &b, &c);
x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
printf("方程的两个实根为:%.2lf, %.2lf\n", x1, x2);
return 0;
}
相关问题
【C语言】求一元二次方程ax²+bx+c=0 (b²-4ac>0即有实根)
在C语言中,我们可以使用Bailout算法(也称为牛顿法或二分查找)来解一元二次方程 \( ax^2 + bx + c = 0 \)。当判别式 \( b^2 - 4ac > 0 \),意味着方程有两个实根。以下是基本步骤:
1. **计算判别式**:
首先,我们需要计算判别式 \( D = b^2 - 4ac \),判断是否有实数根。
2. **如果判别式大于0**:
a. **求两个根公式**:
- 使用公式 \( x_1 = \frac{-b + \sqrt{D}}{2a} \)
- 使用公式 \( x_2 = \frac{-b - \sqrt{D}}{2a} \)
3. **计算根**:
对每个公式,我们分别进行浮点数运算,因为可能存在除以零的情况,需要特别处理。
4. **避免精度问题**:
C语言本身对浮点数运算可能存在一定精度误差,可以使用库函数`sqrt()`和`fabs()`来处理接近于零的小数值。
5. **返回结果**:
将得到的解作为一对浮点数返回。
```c
#include <stdio.h>
#include <math.h>
// 函数原型声明
double quadratic_solver(double a, double b, double c);
int main() {
double a, b, c;
printf("Enter coefficients for the equation (ax^2 + bx + c = 0): ");
scanf("%lf %lf %lf", &a, &b, &c);
if (b * b - 4 * a * c > 0) {
double root1 = quadratic_solver(a, b, c);
double root2 = quadratic_solver(a, b, c); // 因为求两次是为了保证精确
printf("Roots are: %.2f and %.2f\n", root1, root2);
} else {
printf("The equation has no real roots.\n");
}
return 0;
}
// 实现解二次方程的函数
double quadratic_solver(double a, double b, double c) {
double discriminant = b * b - 4 * a * c;
double root1 = (-b + sqrt(discriminant)) / (2 * a);
double root2 = (-b - sqrt(discriminant)) / (2 * a);
return discriminant >= 0 ? root1 : TANH(root1); // 使用TANH处理非常小的负值
}
```
请编程计算一元二次方程ax²+bx+c=0的根,a,b,c的值由用户从键盘输入,其中a≠0
一元二次方程的求解通常会涉及到韦达定理,也就是若一元二次方程 \( ax^2 + bx + c = 0 \) 的两个实数根分别为 \( x_1 \) 和 \( x_2 \),则有 \( x_1 + x_2 = -\frac{b}{a} \) 和 \( x_1 \cdot x_2 = \frac{c}{a} \)。
下面是一个简单的Python程序示例,用于计算并打印出这个方程的根:
```python
import cmath
# 用户输入系数
a = float(input("请输入 a 的值 (a ≠ 0): "))
b = float(input("请输入 b 的值: "))
c = float(input("请输入 c 的值: "))
# 计算判别式
d = (b**2) - (4*a*c)
# 如果判别式大于0,有两个实数根
if d > 0:
x1 = (- cmath.sqrt(d)) / (2 * a)
print(f"方程有两个实根:x1 = {x1.real:.2f}, x2 = {x2.real:.2f}")
# 判别式等于0,有一个实数根或一个复数根
elif d == 0:
x = -b / (2 * a)
if int(x.is_integer()):
x = int(x)
print(f"方程有一个实根:x = {x}")
else:
print(f"方程有一个重复实根:x = {x:.2f}")
else: # 判别式小于0,有两个复数根
real_part = -b / (2 * a)
imag_part = cmath.sqrt(-d) / (2 * a)
print(f"方程有两个共轭复数根:x1 = {real_part:.2f} + {imag_part:.2f}i, x2 = {real_part:.2f} - {imag_part:.2f}i")
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)