求解,为什么输出结果与理想不符,哪里出错了?
/*用do...while 计算一元二次方程。并证明do...while 主要用于人机交互
*/
#include<stdio.h>
#include<math.h>
int main(void)
{
double a, b, c;
double x1, x2;
double delta;
char ch;
do
{
printf("请输入一元二次方程的三个系数:\n");
printf("a = ");
scanf("%lf", &a);
printf("b = ");
scanf("%lf", &b);
printf("c = ");
scanf("%lf", &c);
delta = b * b - 4 * a * c;
if(delta > 0)
{
x1 = (-b + sqrt(delta))/(2 * a);
x2 = (-b - sqrt(delta))/(2 * a);
printf("有两个解,x1 = %lf, x2 = %lf\n", x1, x2);
}
else if(0 == delta)
{
x1 = x2 = (-b) /(2 * a);
printf("有唯一解,x1 = x2 = %lf",x1, x2);
}
else
{
printf("该函数没有解\n");
}
printf("是否需要继续验证?(Y/N)");
scanf(" %c\n", &ch);
}
while('y'==ch || 'Y'==ch);
return 0;
}
输出结果如下:
请输入一元二次方程的三个系数:
a = 1
b = 5
c = 6
有两个解,x1 = -2.000000, x2 = -3.000000
是否需要继续验证?(Y/N)y
1
请输入一元二次方程的三个系数:
a = b = 5
c = 6
有两个解,x1 = -2.000000, x2 = -3.000000
是否需要继续验证?(Y/N)n
1
--------------------------------
Process exited after 13.94 seconds with return value 0
请按任意键继续. . .