求一元二次方程的c程序

得不到想要的结果,输入1 0 0 应该得到0才对啊,为啥会这样呢

img

img

img

#include
#include

int main()
{
    double a, b ,c,disc,x1,x2,realpart,imagpart;
    scanf("%lf %lf lf%",&a,&b,&c);
    printf("the equation ");
    if(fabs(a)<=1e-6)
    {
        printf("is not a quadratic!\n");
        printf("x1 = x2 = %lf",-(c/b));
    }
    else
    {
        disc = b*b-4*a*c;
        if(fabs(disc)<=1e-6)
        {
            printf("has two equal roots:%lf\n",-b/(2*a));
        }
        else
        {
            if(disc>1e-6)
            {
                x1 = (-b+sqrt(disc))/2*a;
                x2 = (-b-sqrt(disc))/2*a;
                printf("has two real roots :%lf and %lf \n",x1,x2);
            }
            else
            {
                realpart = -b/(2*a);
                imagpart = sqrt(-disc)/(2*a);
                printf("has complex roots :\n");
                printf("%lf + %lfi\n",realpart,imagpart);
                printf("%lf - %lfi\n",realpart,imagpart);
            }
        }
    }
    return 0;
}

直接给你一个代码,记得采纳哈

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>//sqrt()函数的头文件
int main()
{
    float a, b, c,d;
    puts("aX^2 + bX + c = 0");
    printf("请输入对应系数:>");
    scanf("%f %f %f", &a, &b, &c);
    d = b * b - 4 * a * c;
    if (a == 0)//当a = 0时
    {
        printf("该方程的解为%f\n", -c / b);
    }
    else if (a != 0)//当a 不等于 0 时
    {
        if (d > 0)
        {
            float x1 = (b + sqrt(d)) / (-2 * a);
            float x2 = (b - sqrt(d)) / (-2 * a);
            printf("有两个实数根,分别为:\n \tx1 = %f\n\tx2 = %f", x1, x2);
        }
        else if (d == 0)
            printf("有一个实数根,为\n\tx1 = %f", (b - sqrt(d)) / (-2 * a));
        else
        {
            puts("无实数根,是否求虚数根[y/n]");
            getchar();//处理\n
            int s = getchar();//只读取第一个输入的数
            float a1 = b / (-2 * a), b1 = sqrt(-d) / (-2 * a);
            if (s == 'y')
                printf("两个虚数根为\n\tx1 = %f  %fi\n\tx2 = %f + %fi", a1, b1, a1, -b1);
        }
    }
    return 0;
}

第7行: scanf("%lf %lf lf%",&a,&b,&c); 最后一个格式控制符 lf% 是错误的,修改为: scanf("%lf %lf %lf",&a,&b,&c);
其它,出现 -.0.0 的问题,需修改代码输出。