- #include <stdio.h>
- #include <math.h>
- void main()
- {
- float a,b,c,disc,x1,x2;
- printf("请输入a,b,c,格式如:1,2,3:/n");
- scanf("%f,%f,%f",&a,&b,&c);
- if(fabs(a)<1e-6){
- printf("这个方程的两个根式是:%f,%f/n",-b/(2*a),-b/(2*a));
- }else{
- disc = b*b-4*a*c;
- if(fabs(disc)<=1e-6){
- x1 = (-b+sqrt(disc))/(2*a);
- x2 = (-b-sqrt(disc))/(2*a);
- printf("这个方程有两个根是:%f,%f/n",x1,x2);
- }else{
- x1 = -b/(2*a);
- x2 = sqrt(-disc)/(2*a);
- printf("方程有虚根:%f,%f/n",x1,x2);
- }
- }
- }
代码不是很完善,在求虚根的时候不知道怎么做的。所以就不好了!