C语言中二次方程的计算误差
我写了这个程序来计算二次方程:C语言中二次方程的计算误差,c,compiler-errors,C,Compiler Errors,我写了这个程序来计算二次方程: #include <stdio.h> #include <math.h> int main() { printf("ax%c + bx + c = 0", 253); while (1 == 1) { float a, b, c; printf("\na = "); scanf("%f", &a);
#include <stdio.h>
#include <math.h>
int main()
{
printf("ax%c + bx + c = 0", 253);
while (1 == 1)
{
float a, b, c;
printf("\na = ");
scanf("%f", &a);
printf("b = ");
scanf("%f", &b);
printf("c = ");
scanf("%f", &c);
if (a == 0)
{
if (b == 0)
printf("\aThis equation is wrong");
else
printf("x = %g", -(c / b));
}
else
{
if ((b * b) == (4 * a * c))
printf("x = %g", -(b / (2 * a)));
else if ((b * b) > (4 * a * c))
{
printf("x = %g\n", ((-b + sqrt(((b * b) - (4 * a * c)))) / (2 * a)));
printf("x = %g\n", ((-b - sqrt(((b * b) - (4 * a * c)))) / (2 * a)));
}
else
{
printf("x = %g + %gi\n", (-b / (2 * a)), (sqrt(-((b * b) - (4 * a * c))) / (2 * a)));
printf("x = %g + %gi\n", (-b / (2 * a)), (-sqrt(-((b * b) - (4 * a * c))) / (2 * a)));
}
}
}
return 0;
}
我认为第26到35行中的“else if”和“else”是错误的。因为当我评论它们两个(只是两个)时,它不会给出错误。但我不知道为什么!我在IDE中使用VS代码。C代码在我的PC中编译并运行正常(VStudio 2019)。 错误似乎出现在您在操作系统级别编译代码的过程中:
cd "f:\PARSA\Programming\C\" ; if ($?) { gcc 11.c -o 11 } ; if ($?) { .\11 }
它看起来像是与目录权限相关的错误,或者是一些不存在的文件。
gcc 11.c-o 11-lm
用于数学库(添加了-
,l
(小写l),m
),这些不是c编译器的错误。看起来像是PowerShell(?)错误。
cd "f:\PARSA\Programming\C\" ; if ($?) { gcc 11.c -o 11 } ; if ($?) { .\11 }