求一元二次方程的根
有了点状况,不是报错就是出现-1.#J,没查到错,特来请教
程序代码:#include<stdio.h>
#include<math.h>
#include<string.h>
int solution(float a,float b,float c,float s[])
{
float d,x1,x2,sq;
d = b*b - 4*a*c;
sq = (float)sqrt(d);
if(d >= 0)
{
if(d == 0)
x1 = x2 = -b/(2*a);
else
{
x1 = (-b+sq) / (2*a);
x2 = (-b+sq) / (2*a);
}
s[0] = x1;
s[1] = x2;
return 1;
}
else
return 0;
}
void main()
{
float a,b,c;
int i;
float s[2];
printf("请输入a,b,c:\n");
scanf("%f,%f,%f",&a,&b,&c);
if(solution(a,b,c,s))
for(i = 0;i < 2;i++)
printf("这个方程的解%d为:%.2f\n",i,s[i]);
else
printf("ERROR!!!\n");
}







