任意输入a、b、c的值,求得并输出当y=0时,y=ax^2+bx+c的解。
1. 源代码如下
#include <stdio.h>
#include<math.h>
int main(){
float a, b, c, disc, s1, s2;
scanf("%f%f%f", &a, &b, &c);
disc = b*b-4*a*c;
printf("a=%.2f,b=%.2f,c=%.2f,\ndisc=%.2f\n",a, b, c, disc);
s1 = (-b + sqrt(disc)) / 2 * a;
s2 = (-b - sqrt(disc)) / 2 * a;
if(a != 0){
if (disc < 0)
printf("not has s1 and s2");
if (disc == 0)
printf("s1 = s2 = %.2f", s1);
if (disc > 0)
printf("s1 = %.2f, s2 = %.2f", s1, s2);
} else{
printf("error!");
}
return 0;
}
2. 运行图片如下