程序如下:



#include<stdio.h>
#include<math.h>


double getResultX2(float a,float b,float value){
double x2;
x2 = (-b - sqrt(value)) / 2*a ;

return x2;
}

double getResultX1(float a,float b,float value){
double x1;
x1 = (-b + sqrt(value)) / 2*a ;

return x1;
}

// 计算 b^2 -4ac
float getValue(float a,float b,float c){
float result = b*b - 4*a*c;
return result;
}


void main(){

float a,b,c;
double x1,x2;

printf("请输入函数 ax^2 + bx + c = 0 的三个未知数 a,b,c (数据之间用空格隔开)\n");
scanf("%f %f %f",&a,&b,&c);

printf("你输入的数据是 a=%f,b=%f,c=%f \n",a,b,c);

float value = getValue(a,b,c);
printf("b^2 -4ac = %f \n",value);

if(value < 0){
printf("\n");
printf("此函数无解。\n\n");
return;
}else{
x1 = getResultX1(a,b,value);
x2 = getResultX2(a,b,value);
}

printf("\n");
printf("解为:x1 = %f , x2 = %f \n",x1,x2);

}