csdn_xjm 2023-03-06 16:22 采纳率: 87.5%
浏览 19
已结题

一元二次方程求根程序纠错

程序哪里出现了问题


#include<stdio.h>
#include<math.h>
 
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 :\nx1 = %.2lf\nx2 = %.2lf \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;
}
  • 写回答

2条回答 默认 最新

  • qzjhjxj 2023-03-06 16:35
    关注

    第8行: scanf("%lf %lf lf%",&a,&b,&c); 最后一个格式控制符:lf% 这里错了,修改为: scanf("%lf %lf %lf",&a,&b,&c); 供参考:

    #include<stdio.h>
    #include<math.h>
    
    int main()
    {
        double a, b, c, disc, x1, x2, realpart, imagpart;
        scanf("%lf %lf %lf", &a, &b, &c);   //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 :\nx1 = %.2lf\nx2 = %.2lf \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;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月14日
  • 已采纳回答 3月6日
  • 创建了问题 3月6日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分