2301_80150143 2023-11-04 14:31 采纳率: 28.3%
浏览 1

编译结果错误的原因是什么


#include<math.h>

int main()
{
    float a,b,c,d;
    double x1,x2;
    printf("please input three numbers:");
    scanf("%f%f%f",&a,&b,&c);
    d =pow(b,2)-4*a*c;
    printf("%f\n",d);
    if(d<0)
    {
        printf("the input is wrong,please input again:");
    }
    else
    {
        x1=(-b+sqrt(d))/(2*a);
        x2=(-b-sqrt(d))/(2*a);
        printf("%lf\n%lf\n",x1,x2);
    }
}

打扰一下,为什么我这个编译结果,三个都是零,求解

  • 写回答

2条回答 默认 最新

  • 甜羊羊. 2023-11-04 14:37
    关注

    我的代码在代码开头添加了#include <stdio.h>和#include <math.h>,分别用于引入输入输出函数和数学函数。
    在scanf函数中的参数格式中添加了空格分隔符,以便正确读取输入的三个数值。
    在判断根的时候,修改了输出的提示信息。
    在输出根的时候,增加了更具描述性的前缀。
    供参考:

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        float a, b, c, d;
        double x1, x2;
    
        printf("Please input three numbers: ");
        scanf("%f %f %f", &a, &b, &c);
    
        d = pow(b, 2) - 4 * a * c;
        printf("%f\n", d);
    
        if (d < 0)
        {
            printf("No real roots exist.\n");
        }
        else
        {
            x1 = (-b + sqrt(d)) / (2 * a);
            x2 = (-b - sqrt(d)) / (2 * a);
            printf("Roots:\n");
            printf("x1 = %lf\n", x1);
            printf("x2 = %lf\n", x2);
        }
    
        return 0;
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 11月4日