c语言 求方程式ax2+bx+c=0的代码
时间: 2023-12-21 19:03:42 浏览: 157
以下是求解一元二次方程ax^2 + bx + c = 0的C语言代码:
```c
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, delta, x1, x2;
printf("请输入一元二次方程的系数(a, b, c):\n");
scanf("%lf %lf %lf",&a, &b, &c);
delta = b*b - 4*a*c;
if(delta > 0) {
x1 = (-b + sqrt(delta)) / (2*a);
x2 = (-b - sqrt(delta)) / (2*a);
printf("方程有两个不相等的实根:x1 = %.2lf, x2 = %.2lf\n", x1, x2);
} else if(delta == 0) {
x1 = x2 = -b / (2*a);
printf("方程有两个相等的实根:x1 = x2 = %.2lf\n", x1);
} else {
printf("方程没有实根\n");
}
return 0;
}
```
注:sqrt()函数需要在math.h库中引入。
相关问题
c语言:求方程ax2+bx+c=0方程的解,考虑有复根的情况。
在C语言中,要计算一元二次方程 \( ax^2 + bx + c = 0 \) 的解,通常会使用"二次公式",它包括实根和可能存在的复数根。首先,我们需要计算判别式 \( D = b^2 - 4ac \),然后根据判别式的值来判断根的情况:
- 如果 \( D > 0 \),有两个不同的实根:\( x_1 = \frac{-b + \sqrt{D}}{2a} \) 和 \( x_2 = \frac{-b - \sqrt{D}}{2a} \)。
- 如果 \( D = 0 \),有一个重根(即单根):\( x = \frac{-b}{2a} \),并且可能是实数也可能不是,取决于a是否为0。
- 如果 \( D < 0 \),方程有两个共轭复根:\( x = \frac{-b \pm i\sqrt{|D|}}{2a} \),其中 \( i \) 是虚数单位。
C语言中,可以编写如下的函数来实现这个功能:
```c
#include <stdio.h>
#include <math.h>
void quadratic(double a, double b, double c) {
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
double root1 = (-b + sqrt(discriminant)) / (2 * a);
double root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Two distinct real roots: %.2f and %.2f\n", root1, root2);
} else if (discriminant == 0) {
double root = -b / (2 * a);
printf("One real root: %.2f\n", root);
} else { // discriminant is negative
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Two complex conjugate roots: %.2f + %.2fi and %.2f - %.2fi\n",
realPart, imaginaryPart, realPart, imaginaryPart);
}
}
int main() {
double a, b, c;
printf("Enter coefficients for the equation ax^2 + bx + c = 0: ");
scanf("%lf %lf %lf", &a, &b, &c);
quadratic(a, b, c);
return 0;
}
```
记得输入正确的系数并运行这个程序,它将显示方程的解。如果你需要实际计算,请替换`scanf`部分的用户输入。
题目描述 求方程式ax2+bx+c=0的实根。 输入 三个用空格隔开的整数a、b、c,保证a不为0,且b2-4ac>0。 输出 输出方程式ax2+bx+c=0的两个实根。所有的实数输出请使用C语言的默认舍入方式保留4位小数,注意行尾输出换行。
这是一个经典的数学问题,通常出现在计算机编程课程中,要求编写程序求解一元二次方程 \( ax^2 + bx + c = 0 \) 的根。当判别式 \( b^2 - 4ac \) 大于0时,意味着该方程有两个不同的实根。你可以使用公式 \( x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \) 来计算。以下是C语言函数实现这个功能的例子:
```c
#include <stdio.h>
#include <math.h> // 引入math库,用于浮点数的平方根运算
// 定义求解二次方程的函数
void solveQuadraticEquation(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);
printf("%.4f\n", root1);
printf("%.4f\n", root2);
}
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
// 转换为double类型进行浮点计算
double aDouble = (double)a;
double bDouble = (double)b;
double cDouble = (double)c;
solveQuadraticEquation(aDouble, bDouble, cDouble);
return 0;
}
```
在这个代码中,用户输入三个整数a、b、c,我们首先把它们转换为double类型,然后调用`solveQuadraticEquation`函数计算根并按照要求打印结果,保留4位小数。
阅读全文
相关推荐
![](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)