注意事项:
sqrt()开根号函数的返回值类型为double,为了避免计算过程中数据精度流失,因而要采用强制转换数据类型的方法,将a,b,c三个参数先转换为同类型,载进行计算。
参考代码:
#include<stdio.h> #include<math.h> static float a,b,c; int main(void){ double behindOne(double m,double n,double k); double behindTwo(double m,double n,double k); float front,test; scanf("%f%f%f",&a,&b,&c); front = (-b)/(2*a); test = b*b - 4*a*c; if(test<0) printf("x1=%.3f+%.3lfi x2=%.3f-%.3lfi",front,behindOne((double)a,(double)b,(double)c),front,behindOne((double)a,(double)b,(double)c)); if(test==0) printf("x1=%.3f x2=%.3f",front,front); if(test>0) printf("x1=%.3lf x2=%.3lf",(double)front+behindTwo((double)a,(double)b,(double)c),(double)front-behindTwo((double)a,(double)b,(double)c)); return 0; } double behindOne(double m,double n,double k){ double result; result = (sqrt(fabs(n*n-4*m*k)))/(2*a); return result; } double behindTwo(double m,double n,double k){ double result; result = (sqrt(n*n-4*m*k))/(2*m); return result; }
0.0分
0 人评分