【发布时间】:2022-01-22 06:15:22
【问题描述】:
#include <math.h>
#include <stdio.h>
main() {
int a, b, c, x, x1, x2;
printf("enter the values of a,b,c:");
scanf("%d%d%d", &a, &b, &c);
printf("The quadratic equation is %d*pow(x,2)+%d*x+%d=0", a, b, c);
if (pow(b, 2) - 4 * a * c >= 0) {
x1 = (-b + sqrt(pow(b, 2) - 4 * a * c)) / 2 * a;
x2 = (-b - sqrt(pow(b, 2) - 4 * a * c)) / 2 * a;
printf("the roots of the equation are x1=%d,x2=%d", x1, x2);
}
else
printf("roots of the equation in the form of x+iy and x-iy");
return 0;
}
对于给定的问题,这段代码是否合适,我对打印虚构的根有点困惑。你能帮忙吗
【问题讨论】:
-
您应该检查
scanf是否成功。if(scanf("%d%d%d", &a, &b, &c) == 3) { success } else { failure } -
对不起,我没明白你的意思,为什么应该是 ==3
-
虽然
a、b和c可以成为int,但我认为您希望x1和x2成为double(并使用%g打印) -
@user17725027 了解
scanf返回的内容,您就会明白为什么它应该是== 3。 -
很抱歉,我仍然没有得到,我怀疑我应该遵循什么步骤以 x+iy 的形式打印虚根,好吧,我理解 x1 和 x2 应该是双倍的,但是我的疑问呢
标签: c