用C语言求方程的根

2025-03-01 12:13:37
推荐回答(4个)
回答1:

你没有考虑a=0的情况,我把我写的给你看看,你看看有什么不同吧:
#include
#include
main()
{
int a,b,c;
double DT,x,x1,x2;
scanf("%d %d %d",&a,&b,&c);
DT=b*b-4*a*c;
if((a==0)&&(b==0))
printf("Input error!\n");
else
{
if(a==0)
{ x=-c/(1.*b);
printf("x=%.6f\n",x);
}
else
{
if(DT==0)
{x=-b/(2*a);
printf("x1=x2=%.6f\n",x);
}
if(DT>0)
{ x1=(-b+sqrt(DT))/(2*a);
x2=(-b-sqrt(DT))/(2*a);
printf("x1=%.6f\nx2=%.6f\n",x1,x2);
}
if((DT<0)&&(b==0))
{DT=-DT;
x1=(sqrt(DT))/(2*a);
x2=(-sqrt(DT))/(2*a);
printf("x1=%.6fi\nx2=%.6fi\n",x1,x2);
}
if((DT<0)&&(b!=0))
{DT=-DT;
x1=-b/(2.*a);
x2=sqrt(1.*DT)/(2*a);
printf("x1=%.6f+%.6fi\nx2=%.6f-%.6fi\n",x1,x2,x1,x2);
}
}
}
return 0;
}
具体的输入输出格式可能不一样,因为不清楚你的要求,有疑问可以问我

回答2:

else deta=sqrt(b*b-4*a*c); x1=(-b+deta)/(2*a); x2=(-b-deta)/(2*a); printf("%8.2f,%8.2f\n",x1,x2);最好改为else { deta=sqrt(b*b-4*a*c); x1=(-b+deta)/(2*a); x2=(-b-deta)/(2*a); printf("%8.2f,%8.2f\n",x1,x2);} 还有把你运行的错误结果写出来吧,包括输入和输出,这样更容易分析问题,暂时只能这样了

回答3:

我在机器上运行了一下你的代码。好久不用C了,也有点不懂了。我把问题复制下来。让大侠给你解决吧

1>------ 已启动生成: 项目: test1, 配置: Debug Win32 ------
1>正在编译...
1>Test.cpp
1>d:\360data\重要数据\我的文档\visual studio 2008\projects\test\test1\test.cpp(1) : warning C4067: 预处理器指令后有意外标记 - 应输入换行符
1>d:\360data\重要数据\我的文档\visual studio 2008\projects\test\test1\test.cpp(4) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> d:\program files\vc\include\stdio.h(306) : 参见“scanf”的声明
1>d:\360data\重要数据\我的文档\visual studio 2008\projects\test\test1\test.cpp(6) : error C3861: “sqrt”: 找不到标识符
1>d:\360data\重要数据\我的文档\visual studio 2008\projects\test\test1\test.cpp(10) : error C3861: “sqrt”: 找不到标识符
1>生成日志保存在“file://d:\360data\重要数据\我的文档\Visual Studio 2008\Projects\test\test1\Debug\BuildLog.htm”
1>test1 - 2 个错误,2 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========

回答4:

你前面两个输出的第二个%8.2f后面都多了个i。。结果不对是怎么个不对法你要写清楚啊