题目:编程计算并输出一元二次方程ax '+bx+c=0的两个根
解析:先判断delta与0的关系,
????????若delta>0,则有两个不同的实根,利用求根公式( -b 土? √b^2 -4*a*c)/2*a ;
????????若delta=0,则有两个相同的实根,则根为x1 = x2 =?-(b/2*a);
????????若delta<0,则无实根;
逻辑:if判读选择
# include <stdio.h>
# include <math.h>
int main(void)
{
double a, b, c;
double delta;
double x1, x2;
int flag;
do
{
printf("输入a,b,c:\n");
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 = %f, x2 = %f\n", x1, x2);
}
else if (0 == delta)
{
x1 = x2 = (-b) / (2 * a);
printf("x1 = x2 = %f\n", x1, x2);
}
else
{
printf("无实数解!\n");
}
printf("继续 1,退出 0: ");
scanf("%d", &flag); //
} while (flag);
return 0;
}