c语言求一元二次方程的根
时间: 2023-08-03 07:07:25 浏览: 89
下面是一个示例程序,可以求解一元二次方程的根:
```c
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, delta, x1, x2;
printf("请输入一元二次方程的系数(a, b, c):");
scanf("%f%f%f", &a, &b, &c);
delta = b * b - 4 * a * c;
if (delta < 0)
{
printf("该方程在实数范围内无解!\n");
}
else if (delta == 0)
{
x1 = x2 = -b / (2 * a);
printf("该方程有唯一解,x1 = x2 = %.2f\n", x1);
}
else
{
x1 = (-b + sqrt(delta)) / (2 * a);
x2 = (-b - sqrt(delta)) / (2 * a);
printf("该方程有两个解,x1 = %.2f,x2 = %.2f\n", x1, x2);
}
return 0;
}
```
程序会先让用户输入方程的系数,然后计算出方程的判别式 delta,根据 delta 的大小判断方程的根个数和根的具体值。需要注意的是,当 delta 小于 0 时,方程无实数根。
相关问题
c语言求一元二次方程根
好的,求一元二次方程根的公式为:
x = (-b ± sqrt(b^2 - 4ac)) / 2a
其中,a、b、c分别为一元二次方程ax^2 + bx + c = 0中的系数。
具体实现可以参考以下代码:
```c
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, x1, x2, delta;
printf("请输入一元二次方程的系数:\n");
scanf("%f%f%f", &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("方程的两个实根分别为:%.2f和%.2f\n", x1, x2);
}
else if (delta == 0)
{
x1 = x2 = -b / (2 * a);
printf("方程的唯一实根为:%.2f\n", x1);
}
else
{
printf("方程无实根!\n");
}
return 0;
}
```
输入a、b、c三个系数后,程序会自动计算出方程的根,并输出结果。
用C语言求一元二次方程根
在C语言中,计算一元二次方程的根需要使用公式法,特别是当已知方程的系数a、b和c时,可以使用下面的公式:
\[ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \]
这是韦达定理的基础,其中`+`号对应的是第一个根(大于0的情况),`-`号对应的是第二个根(可能小于0)。为了编写这样的程序,你需要创建一个函数,接受三个参数a、b和c,并返回两个可能的解。
以下是一个简单的示例:
```c
#include <stdio.h>
#include <math.h>
// 定义计算二次方程根的函数
void quadratic(double a, double b, double c, double *x1, double *x2) {
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
*x1 = (-b + sqrt(discriminant)) / (2 * a);
*x2 = (-b - sqrt(discriminant)) / (2 * a);
} else if (discriminant == 0) {
*x1 = *x2 = -b / (2 * a);
} else { // 当判别式小于0时,方程无实根
double realPart = -b / (2 * a), imaginaryPart = sqrt(-discriminant) / (2 * a);
*x1 = complex(realPart, imaginaryPart);
*x2 = complex(realPart, -imaginaryPart); // 注意这里的x1和x2是复数对称
}
}
// 对于复数,这里假设complex()函数从头文件cmath中引入
// 实际上可能需要自定义实现
typedef struct {
double real;
double imag;
} Complex;
Complex complex(double r, double i) {
return {r, i};
}
int main() {
double a, b, c;
double x1, x2;
printf("请输入一元二次方程的系数 a, b, c:\n");
scanf("%lf %lf %lf", &a, &b, &c);
quadratic(a, b, c, &x1, &x2);
printf("方程的两个根是: %.2lf 和 %.2lf\n", x1, x2);
return 0;
}
```
在这个例子中,用户输入a、b和c的值,函数会计算并打印出根。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.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)