永发信息网

一元二次方程求复根的问题

答案:2  悬赏:70  手机版
解决时间 2021-02-16 10:20
  • 提问者网友:暗中人
  • 2021-02-15 10:47
大家帮我看看啊,前面两个都可以的,可为什么最后那个求复根的老出错呢?我公式没有写错啊?先谢谢了!老师规定要这么做,所以每次只能重新回去改前面的系数啦。

12 #include
13 #include
14 #define FILENAME "quad1.txt"
15 int main()
16 {
17 int a=1, b=3, c=3,q,n;
18 double p, x1,x2,x3,x4,x5,m;
19 FILE *quad;
20
21 quad = fopen(FILENAME,"w");
22 fprintf(quad,"%i %i %i",a,b,c);
23
24 n=b*b-4*a*c;
25 q=2*a;
26
27 if(n>0)
28 { p=sqrt(n);
29 x1=(-b+p)/q;
30 x2=(-b-p)/q;
31 printf("There are two real roots.\n");
32 printf("The real roots of (%i)x^2 +(%i)x+(%i)are:\n",a,b,c);
33 printf(" %.3f and %.3f\n",x1,x2);
34 }
35
36 else if (n==0)
37 { x3=(-b)/q;
38 printf("There is one real root.\n");
39 printf("The real root of(%i)x^2 +(%i)x+(%i)is:\n",a,b,c);
40 printf(" %.3f\n",x3);
41 }
42
43 else
44 { m=sqrt(-n);
45 x4=(-b)/q;
46 x5=m/q;
47 printf("There are two complex roots.\n");
48 printf("The complex roots to (%i)x^2 +(%i)x+(%i)are:\n",a,b,c);
49 printf(" (%.3lf)+ (%.3lf)i and (%.3lf)-(%.3lf)i\n",x4,x5,x4,x5);
50 }
51
52 fclose(quad);
53 return 0;
54 }

我把他们都改了,为什么还是不行呀??T_T
最佳答案
  • 五星知识达人网友:独行浪子会拥风
  • 2021-02-15 11:02
因为这里 int a=1, b=3, c=3,q,n;

你把他们定义成整型了 所以3/2=1
全部回答
  • 1楼网友:一把行者刀
  • 2021-02-15 12:30
你的程序对a=0这种情况的说明不准确,应改为: if a = 0 then msgbox "这是一元一次方程" x = -c / b text4.visible = false text5 = x 这样比较完整。 考虑复根时: private sub command1_click() if a <> 0 and b^2 - 4 * a * c <0 then text4 = (-b) / (2 * a) & "+j" & sqr(-b ^ 2 + 4 * a * c) / (2 * a) text5 = (-b) / (2 * a) & "-j" & sqr(-b ^ 2 + 4 * a * c) / (2 * a) end if end sub 全部: dim a, b, c as integer private sub command1_click() a = val(text1) b = val(text2) c = val(text3) if a = 0 then msgbox "这是一元一次方程" x = -c / b text4.visible = false text5 = x else if a <> 0 and (b * b) - 4 * a * c >= 0 then text4.text = val((-b + sqr((b * b) - 4 * a * c)) / 2 * a) text5.text = val((-b - sqr((b * b) - 4 * a * c)) / 2 * a) elseif a <> 0 and (b * b) - 4 * a * c < 0 then text4 = (-b) / (2 * a) & "+j" & sqr(-b ^ 2 + 4 * a * c) / (2 * a) text5 = (-b) / (2 * a) & "-j" & sqr(-b ^ 2 + 4 * a * c) / (2 * a) end if end if end sub private sub text1_click() text1 = inputbox("请输入数值", "输入框") end sub private sub text2_click() text2 = inputbox("请输入数值", "输入框") end sub private sub text3_click() text3 = inputbox("请输入数值", "输入框") end sub
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯