一道C语言解方程的问题

题目是:输入一个正整数repeat(0<repeat<10),做repeat次下列运算:输入参数a,b,c,求一元二次方程a*x*x+b*x+c=0的根,结果保留2位小数... 题目是:输入一个正整数repeat (0<repeat<10),做repeat次下列运算: 输入参数a,b,c,求一元二次方程a*x*x+b*x+c=0的根,结果保留2位小数。 输出使用以下语句: printf("参数都为零,方程无意义!\n"); printf("a和b为0,c不为0,方程不成立\n"); printf("x = %0.2f\n", -c/b); printf("x1 = %0.2f\n", (-b+sqrt(d))/(2*a)); printf("x2 = %0.2f\n", (-b-sqrt(d))/(2*a)); printf("x1 = %0.2f+%0.2fi\n", -b/(2*a), sqrt(-d)/(2*a)); printf("x2 = %0.2f-%0.2fi\n", -b/(2*a), sqrt(-d)/(2*a)); 输入输出示例:括号内为说明 如输入: 5 (repeat=5) 0 0 0 (a=0,b=0,c=0) 0 0 1 (a=0,b=0,c=1) 0 2 4 (a=0,b=2,c=4) 2.1 8.9 3.5 (a=2.1,b=8.9,c=3.5) 1 2 3 (a=1,b=2,c=3) 输出: 参数都为零,方程无意义! a和b为0,c不为0,方程不成立 x = -2.00 x1 = -0.44 x2 = -3.80 x1 = -1.00+1.41i x2 = -1.00-1.41i 我的程序设计是: #include <stdio.h> #include <math.h> int main(void) { int repeat, ri; double a, b, c, d; scanf("%d", &repeat); for(ri = 1; ri <= repeat; ri++){ scanf("%lf%lf%lf", &a, &b, &c); d=pow(b,2)-4.0*a*c; if(a==0&&b==0&&c==0) printf("参数都为零,方程无意义!\n"); if(a==0&&b==0&&c!=0) printf("a和b为0,c不为0,方程不成立\n"); if(a==0&&b!=0) printf("x = %0.2f\n", -c/b); if(a!=0&&d>=0) printf("x1 = %0.2f\n", (-b+sqrt(d))/(2*a));printf("x2 = %0.2f\n", (-b-sqrt(d))/(2*a)); if(a!=0&&d<0) printf("x1 = %0.2f+%0.2fi\n", -b/(2*a), sqrt(-d)/(2*a));printf("x2 = %0.2f-%0.2fi\n", -b/(2*a), sqrt(-d)/(2*a)); } } 可运行时,输出的结果除了想要的语句之外,会有别的语句出现 请问是怎么回事呀? 展开
 我来答
创作者HSrAEzFMkv
2020-02-05 · TA获得超过3840个赞
知道大有可为答主
回答量:3137
采纳率:33%
帮助的人:206万
展开全部
改了两个地方:
1、输入参数时,scanf("%lf
%lf
%lf",
&a,
&b,
&c)的参数列表中各参数最好用空格或者逗号隔开即"%lf
%lf
%lf"或"%lf,%lf,%lf",你原来的程序好像没办法输入三个参数a、b、c的。
2、输出计算结果时,if后面的两个语句应该是一个整体所以要加大括号,即
if(a!=0
&&
d>=0)
{
printf("x1
=
%0.2f\n",
(-b+sqrt(d))/(2*a));
printf("x2
=
%0.2f\n",
(-b-sqrt(d))/(2*a));
}
if(a!=0
&&
d<0)
{
printf("x1
=
%0.2f+%0.2fi\n",
-b/(2*a),
sqrt(-d)/(2*a));
printf("x2
=
%0.2f-%0.2fi\n",
-b/(2*a),
sqrt(-d)/(2*a));
}
所以改完后的完整程序应如下:
#include
<stdio.h>
#include
<math.h>
int
main(void)
{
int
repeat,
ri;
double
a,
b,
c,
d;
scanf("%d",
&repeat);
for(ri
=
1;
ri
<=
repeat;
ri++)
{
scanf("%lf
%lf
%lf",
&a,
&b,
&c);
d=pow(b,2)-4.0*a*c;
if(a==0
&&
b==0
&&
c==0)
printf("参数都为零,方程无意义!\n");
if(a==0
&&
b==0
&&
c!=0)
printf("a和b为0,c不为0,方程不成立\n");
if(a==0
&&
b!=0)
printf("x
=
%0.2f\n",
-c/b);
if(a!=0
&&
d>=0)
{
printf("x1
=
%0.2f\n",
(-b+sqrt(d))/(2*a));
printf("x2
=
%0.2f\n",
(-b-sqrt(d))/(2*a));
}
if(a!=0
&&
d<0)
{
printf("x1
=
%0.2f+%0.2fi\n",
-b/(2*a),
sqrt(-d)/(2*a));
printf("x2
=
%0.2f-%0.2fi\n",
-b/(2*a),
sqrt(-d)/(2*a));
}
}
return
0;
}
我用上面的代码简单的测试一下,应该没问题了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式