首页 行业资讯 宠物日常 宠物养护 宠物健康 宠物故事

c语言程序 哪里出错了呢 始终运行不出结果

发布网友

我来回答

2个回答

热心网友

试试
scanf("%lf%lf%lf",&a,&b,&c);

可能是因把你输入的数字以float的形式存入了内存中,而程序调用的时候以double的形式去读相应的内存,所以解析出来的double型数值与你输入的不一致了。个人没有发现其他问题。
````````符号位 阶码 尾数 长度
float 1 8 23 32
double 1 11 52 追问

改了,还是不对。

答案相差好多

追答看你的代码没改啊%f->%lf

热心网友

#include<stdio.h>
#include<math.h>
main()
{
double a,b,c,disc,x1,x2,realpart,imagpart;
scanf("%f%f%f",&a,&b,&c);
printf("the equation");
if(fabs(a)<=1e-6)
printf("is not a quadratic");
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);
}
}
}
没有查看程序正误

热心网友

试试
scanf("%lf%lf%lf",&a,&b,&c);

可能是因把你输入的数字以float的形式存入了内存中,而程序调用的时候以double的形式去读相应的内存,所以解析出来的double型数值与你输入的不一致了。个人没有发现其他问题。
````````符号位 阶码 尾数 长度
float 1 8 23 32
double 1 11 52 追问

改了,还是不对。

答案相差好多

追答看你的代码没改啊%f->%lf

热心网友

#include<stdio.h>
#include<math.h>
main()
{
double a,b,c,disc,x1,x2,realpart,imagpart;
scanf("%f%f%f",&a,&b,&c);
printf("the equation");
if(fabs(a)<=1e-6)
printf("is not a quadratic");
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);
}
}
}
没有查看程序正误

热心网友

试试
scanf("%lf%lf%lf",&a,&b,&c);

可能是因把你输入的数字以float的形式存入了内存中,而程序调用的时候以double的形式去读相应的内存,所以解析出来的double型数值与你输入的不一致了。个人没有发现其他问题。
````````符号位 阶码 尾数 长度
float 1 8 23 32
double 1 11 52 追问

改了,还是不对。

答案相差好多

追答看你的代码没改啊%f->%lf

热心网友

#include<stdio.h>
#include<math.h>
main()
{
double a,b,c,disc,x1,x2,realpart,imagpart;
scanf("%f%f%f",&a,&b,&c);
printf("the equation");
if(fabs(a)<=1e-6)
printf("is not a quadratic");
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);
}
}
}
没有查看程序正误

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com