int a,b,c;
float p,q,x1,x2;
printf("please enter a,b,c:");
scanf("%d,%d,%d",&a,&b,&c);
p=-b/(2*a),q=sqrt(b*b-4*a*c)/(2*a);
x1=p+q,x2=p-q;
printf("a=%d,b=%d,c=%d\n,x1=%.2f,x2=%.2f\n",a,b,c,x1,x2);
return 0;
} 为什么输入1,3,2输出的是x1=-0.50,x2=-1.50而不是-1和-2.00。
因为a,b,c都是整数,所以-b/(2*a)是整除
#include <stdio.h>
#include <math.h>
int main()
{
float a,b,c;
float p,q,x1,x2;
printf("please enter a,b,c:");
scanf("%f,%f,%f",&a,&b,&c);
p=-b/(2*a);
q=sqrt(b*b-4*a*c)/(2*a);
x1=p+q;
x2=p-q;
printf("a=%.0f,b=%.0f,c=%.0f\nx1=%.2f,x2=%.2f\n",a,b,c,x1,x2);
return 0;
}