求一元二次方程
的根,通过键盘输入a、b、c,根据△的值输出对应x1和x2的值(保留一位小数)(用if语句完成)。
//一元二次方程的实现
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
double a, b, c, delta, x1=0,x2=0,x3;
char i = 0;
printf("请输入一元二次方程的系数: ");
scanf_s("%lf%lf%lf", &a, &b, &c);
delta = b * b - 4 * a * c;
x3 = -b / (2 * a);
if (delta == 0)
{
x1 = x2 = -b / (2 * a);
printf("x1=%.f,x2=%.f", x1, x2);
}
else if (delta > 0)
{
x1 = (-b / 2 * a) + sqrt(b * b - 4 * a * c) / (2 * a);
x2 = (-b / 2 * a) - sqrt(b * b - 4 * a * c) / (2 * a);
printf("x1=%.f,x2=%.f", x1, x2);
}
else if (delta < 0)
{
x1 = sqrt(fabs(b * b - 4 * a * c))/ (2 * a);
x2 = sqrt(fabs(b * b - 4 * a * c))/ (2 * a);
printf("x1=%.f+%.fi,x2=%.f-%.fi", x3, x1,x3,x2);
}
return 0;
}