注意事项:
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 人评分
兰顿蚂蚁 (C++代码)浏览:1225 |
程序员的表白 (C语言代码)浏览:706 |
2006年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:674 |
C语言训练-求s=a+aa+aaa+aaaa+aa...a的值 (C语言代码)浏览:636 |
母牛的故事 (C语言代码)浏览:739 |
1126题解浏览:649 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:438 |
众数问题 (C语言代码)浏览:717 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:527 |
拆分位数 (C语言代码)浏览:464 |