标题:求解,代码有啥问题?
只看楼主
PGLWGES
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2020-7-30
结帖率:100%
 问题点数:0 回复次数:1 
求解,代码有啥问题?
#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;
}
搜索更多相关主题的帖子: sqrt printf not 代码 include 
2020-10-16 19:25
风过无痕1989
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:8
帖 子:228
专家分:1050
注 册:2020-7-17
得分:0 
回复 楼主 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编辑过]

2020-10-17 00:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-503507-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012148 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved