注册 登录
编程论坛 C语言论坛

求解,代码有啥问题?

PGLWGES 发布于 2020-10-16 19:25, 1307 次点击
#include<stdio.h>
#include<math.h>
int main()
{
    double a, b, c, disc, x1, x2, realpart, imagpart;
    scanf_s("%lf,%%lf,%lf", &a, &b, &c);
    printf("The equation");
    if (fabs(a) <= 1e-6)
        printf("is not a quadratic\n");
    else
    {
        disc = b * b - 4 * a * c;
        if (fabs(disc) <= 1e-6)
            printf("has two equal roots:%8.4f\n", -b / (2 * a));
        else
            if (disc > 1e-6)
            {
                x1 = (-b + sqrt(disc)) / (2 * a);
                x2= (-b - sqrt(disc)) / (2 * a);
                printf("has distinct real roots:%8.4f and %8.4f\n", x1, x2);
            }
            else
            {
                realpart = -b / (2 * a);
                imagpart = sqrt(-disc) / (2 * a);
                printf("has complex roots:\n");
                printf("%8.4f+%8.4fi\n", realpart, imagpart);
                printf("%8.4f-%8.4fi\n", realpart, imagpart);
            }
    }
    return 0;
}
1 回复
#2
风过无痕19892020-10-17 00:30
回复 楼主 PGLWGES
错误已经在注释中说明了,并帮你修改了
程序代码:

#include<stdio.h>
#include<math.h>
int main()
{
    double a, b, c, disc, x1, x2, realpart, imagpart;
    scanf_s("%lf,%lf,%lf", &a, &b, &c);  //第2个%lf前多了一个%
    printf("The equation");
    if (fabs(a) <= 1e-6)
        printf("is not a quadratic\n");
    else
    {
        disc = b * b - 4.0 * a * c;   // 由于定义的是 double 类型,改为4.0为好(下同)
        if (fabs(disc) <= 1e-6)
            printf("has two equal roots:%8.4f\n", -b / (2.0 * a));
        else
            if (disc > 1e-6)
            {
                x1 = (-b + sqrt(disc)) / (2.0 * a);
                x2= (-b - sqrt(disc)) / (2.0 * a);
                printf("has distinct real roots:%8.4f and %8.4f\n", x1, x2);
            }
            else
            {
                realpart = -b / (2.0 * a);
                imagpart = sqrt(-disc) / (2.0 * a);
                printf("has complex roots:\n");
                printf("%8.4f+%8.4fi\n", realpart, imagpart);
                printf("%8.4f-%8.4fi\n", realpart, imagpart);
            }
    }
    return 0;
}


[此贴子已经被作者于2020-10-17 02:11编辑过]

1