求ax^2+bx+c=0的根。分别考虑d=b^2-4ac大于0、等于0和小于0这三种情况c语言的算

求ax^2+bx+c=0的根。分别考虑d=b^2-4ac大于0、等于0和小于0这三种情况c语言的算法

#include<stdio.h>
#include<math.h> //运用sqrt
int main(){
double a, b, c, p, q, i_f;
scanf("%lf%lf%lf", &a, &b, &c);
printf("%f,%f,%f\n\n", a, b, c);
printf("the 方程式");
i_f = b*b - 4 * a*c;
p = -b / (2.0*a); q = sqrt(b*b - 4 * a*c) / (2.0*a);

/*a<=0.000001, 约等于0
1.a=0,不是方程式;
2.b^2-4ac=0,有俩相等实根;1,2,1
3.b^2-4ac>0,有俩不等实根;2 6 1
4.~~~~~~~<0,有俩共轭复根.1 2 2
*/
if (fabs(a)<= 1e-6)printf("it不是正确的,方程式");
if (fabs(i_f) <= 1e-6)printf("有俩相等实根,%5.2lf\n", p);
if (i_f>1e-6)printf("俩不等实根,%5.2lf,%5.2lf", p + q, p - q);
if (i_f < -(1e-6))printf("有俩共轭复根,%lf+%lfi;%lf-%lfi", p, q,p,q );
////////
system("pause");
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-10-30
#include "math.h" 
main() 
{ float a,b,c,x1,x2,d; 
printf("请输入a:"); scanf("%f",&a); 
printf("请输入b:"); scanf("%f",&b); 
printf("请输入c:"); scanf("%f",&c); 
d=b*b-4*a*c; 
if(d < 0) printf("方程没有实数解。\n"); 
if (d==0) { x1=(-b)/(2*a); printf("x1=%f\n",x1); } 
if (d>0) 
{ x1=(-b+sqrt(d))/(2*a); x2=(-b-sqrt(d))/(2*a); printf("x1=%f,x2=%f\n",x1,x2);} 
}

追问

可以有中文输入么

追答

你运行一次就知道了

追问

OK谢谢啦(^o^)

第2个回答  2013-11-03
#include <stdio.h>
#include <math.h>
int main()
{
    float a, b, c, x1, x2, delta;
    scanf("%f%f%f",&a, &b, &c);
    delta = b * b - 4 * a *c;
    if(a != 0)
    {
        if(delta == 0)
        {
            x1 = -b/(a*2);
            printf( "%f\n", x1);
        }
        else if(delta > 0)
        {
            if(a>0)
            { x1 = (-b + sqrt(delta))/(2*a);
             x2 = (-b - sqrt(delta))/(2*a);
            printf("%f\n%f", x1, x2);}
              else
            { x1 = (-b + sqrt(delta))/(2*a);
             x2 = (-b - sqrt(delta))/(2*a);
             printf("%f\n%f", x2, x1);}
        }
        else
            printf("No");
    }
    else if (b != 0)
    {
         x1 = -c/b;
        printf("%f",x1);
    }
    else
        printf("No");
    return 0;
}
//刚学C的时候写的  你看看吧

追问

嗯,等我试试,谢谢啦

追答

应该没有问题 这个代码是OJ数据验证过的

相似回答