生成一个求解一元三次方程的C语言代码
时间: 2024-05-25 08:15:06 浏览: 171
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;
}
阅读全文