一元二次方程ax2+bx+c=0,从键盘输入a、b、c的值后,计算并输出两个根的值,结果保留三位小数。(考虑delt>0、delt=0、delt<0三种情况
时间: 2024-09-14 16:03:55 浏览: 152
当输入a、b、c的值后,可以通过判断delt的值来确定方程的根的情况。当delt>0时,方程有两个不相等的实根,可以通过求根公式计算得到;当delt=0时,方程有两个相等的实根,可以通过求根公式计算得到;当delt<0时,方程没有实根,可以通过求根公式计算得到虚根。具体实现可以参考下面的代码:
```
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, delt, x1, x2, realPart, imagPart;
printf("Please enter the coefficients a, b, c: ");
scanf("%f %f %f", &a, &b, &c);
delt = b * b - 4 * a * c;
if (delt > 0) {
x1 = (-b + sqrt(delt)) / (2 * a);
x2 = (-b - sqrt(delt)) / (2 * a);
printf("The equation has two distinct real roots: x1 = %.3f, x2 = %.3f\n", x1, x2);
} else if (delt == 0) {
x1 = x2 = -b / (2 * a);
printf("The equation has two equal real roots: x1 = x2 = %.3f\n", x1);
} else {
realPart = -b / (2 * a);
imagPart = sqrt(-delt) / (2 * a);
printf("The equation has two complex roots: x1 = %.3f + %.3fi, x2 = %.3f - %.3fi\n", realPart, imagPart, realPart, imagPart);
}
return 0;
}
```
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)