#include <stdio.h>
#include <math.h>
int main(void)
{
double a, b, c, root1, root2;
printf("Input the coefficient a => ");
scanf("%lf", &a);
printf("Input the coefficient b => ");
scanf("%lf", &b);
printf("Input the coefficient c => ");
scanf("%lf", &c);
/* Compute the roots. */
root1 = (- b + sqrt(b*b-4*a*c))/(2*a);
root2 = (- b - sqrt(b*b-4*a*c))/(2*a);
printf("The first root is %8.3f\n", root1);
printf("The second root is %8.3f\n", root2);
return 0;
}
但是,我的输出是
Input the coefficient a => 232
Input the coefficient b => 23
Input the coefficient c => 2
The first root is nan
The second root is nan
我是个初学者,格式有问题吗? 使用代码块,用 C 语言编写。
最佳答案
试试这个:
#include <stdio.h>
#include <math.h>
int main(void)
{
double a, b, c, root1, root2;
double temp;
printf("Input the coefficient a => ");
scanf("%lf", &a);
printf("Input the coefficient b => ");
scanf("%lf", &b);
printf("Input the coefficient c => ");
scanf("%lf", &c);
/* Compute the roots. */
temp = b*b-4*a*c;
if (temp >= 0) {
root1 = (- b + sqrt(temp))/(2*a);
root2 = (- b - sqrt(temp))/(2*a);
printf("The first root is %8.3f\n", root1);
printf("The second root is %8.3f\n", root2);
} else {
printf("There is no root!\n");
}
return 0;
}
remember : load math library like this -> gcc "fileName"
-lm
关于Codeblocks 不打印特定格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39847198/