生成一个求方程的根的c语言代码不要注释
时间: 2024-05-11 13:13:39 浏览: 21
```c
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, delta, x1, x2;
printf("请输入方程的三个系数(a,b,c):");
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("方程的唯一实根为:x=%.2lf\n", x1);
} else {
double realPart = -b / (2 * a);
double imagPart = sqrt(-delta) / (2 * a);
printf("方程的两个虚根分别为:x1=%.2lf+%.2lfi, x2=%.2lf-%.2lfi\n", realPart, imagPart, realPart, imagPart);
}
return 0;
}
```
相关问题
生成一个求解一元三次方程的C语言代码
以下是求解一元三次方程的C语言代码:
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, d;
double x1, x2, x3;
printf("请输入一元三次方程的系数a、b、c:\n");
scanf("%lf %lf %lf", &a, &b, &c);
d = b * b - 3 * a * c;
if (d < 0)
{
printf("方程无实数解。\n");
}
else if (d == 0)
{
x1 = x2 = x3 = -b / (3 * a);
printf("方程的三个实数解为:x1=x2=x3=%lf。\n", x1);
}
else
{
x1 = (-b + sqrt(d)) / (3 * a);
x2 = (-b - sqrt(d)) / (3 * a);
x3 = (2 * b + sqrt(d)) / (3 * a);
printf("方程的三个实数解为:x1=%lf,x2=%lf,x3=%lf。\n", x1, x2, x3);
}
return 0;
}
生成一个求解三阶方程的C语言代码
我可以为您提供一个求解三阶方程的C语言代码,代码如下:
```c
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, d, x1, x2, x3, p, q;
printf("请输入三阶方程的系数(a,b,c):");
scanf("%lf %lf %lf", &a, &b, &c);
d = b * b - 3 * a * c;
if (fabs(a) < 1e-6 && fabs(b) < 1e-6 && fabs(c) < 1e-6) {
printf("方程根是任意的\n");
} else if (fabs(a) < 1e-6 && fabs(b) < 1e-6) {
printf("方程无解\n");
} else if (fabs(a) < 1e-6) {
printf("方程有一个根: %lf\n", -c / b);
} else if (d > 1e-6) {
p = (-b + sqrt(d)) / (3 * a);
q = (-b - sqrt(d)) / (3 * a);
x1 = -b / (3 * a) + p + q;
x2 = (-b + 2 * p * (1 + I * sqrt(3))) / (3 * a);
x3 = (-b + 2 * q * (1 - I * sqrt(3))) / (3 * a);
printf("方程有三个复数根:\n");
printf("x1 = %lf\n", x1);
printf("x2 = %lf + %lf i\n", creal(x2), cimag(x2));
printf("x3 = %lf + %lf i\n", creal(x3), cimag(x3));
} else if (fabs(d) < 1e-6) {
x1 = -b / (3 * a) + 2 * c / (3 * a) * pow(d, -0.5);
x2 = -c / (3 * a) - pow(d, -0.5) / 3;
x3 = -c / (3 * a) + pow(d, -0.5) / 3;
printf("方程有三个实数根:\n");
printf("x1 = %lf\n", x1);
printf("x2 = %lf\n", x2);
printf("x3 = %lf\n", x3);
} else if (d < -1e-6) {
double t = acos(sqrt(-27 * a * a / d) / 2) / 3;
x1 = -b / (3 * a);
x2 = 2 * sqrt(-d / 3) * cos(t) - b / (3 * a);
x3 = 2 * sqrt(-d / 3) * cos(t + 2 * M_PI / 3) - b / (3 * a);
printf("方程有三个实数根:\n");
printf("x1 = %lf\n", x1);
printf("x2 = %lf\n", x2);
printf("x3 = %lf\n", x3);
}
return 0;
}
```
这个 C 语言代码可以求解三阶方程的根,通过输入方程的系数 a、b、c,可以输出方程的根。如果系数为零或方程无解,程序会输出相应的提示信息。当然,如果您有其他更具体的要求,我也可以进行修改和定制。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)