#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<math.h>
int main()
{
float a = 0; //定义abc
float b = 0;
float c = 0;
float x1 = 0, x2 = 0;; //方程的两个根x1,x2
float x = 0;
float delta = 0;
float sqrt_delta = 0;
printf("输入a,b,c的值:\n");
scanf("%f%f%f", &a, &b, &c);
if (0 != a)
{
printf("你要求的方程为:%fx*x+%fx+%f+0\n", a, b, c);
delta = b * b - 4 * a * c;
if (delta >= 0) //判断delta
{
sqrt_delta = sqrt(delta);
if (delta == 0) //delta==0方程只有一个实根
{
x = -b / (2 * a);
printf("该方程只有一个根:x=%f", x);
}
else //delta>0方程有两个实根
{
x1 = (-b + sqrt_delta) / (2 * a);
x2 = (-b - sqrt_delta) / (2 * a);
printf("该方程有两个实根:\n");
printf("x1=%f\n", x1);
printf("x2=%f\n", x2);
}
}
else
{
sqrt_delta = sqrt(-delta);
printf("该方程有复根:\n");
printf("x1=%f+%fi\n", -b / (2*a), sqrt_delta / (2 * a));
printf("x1=%f-%fi\n", -b / (2*a), sqrt_delta / (2 * a));
}
}
else
{
printf("参数错误!\n");
}
return 0;
}
【C语言】一元二次方程求解
于 2023-05-16 18:18:51 首次发布