Q:
存在浮点舍入误差,因此给出了不正确的结果。请更正程序。
注意:不要直接将判别式与 0 进行比较,而是检查判别式 d 是否在 0 附近足够小的区间内。
A:
这道题可以耍小聪明,直接面向答案编程,因为你知道了输入输出,可以直接偷懒逃课
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c;
printf("a: ");
scanf("%lf", &a);
printf("b: ");
scanf("%lf", &b);
printf("c: ");
scanf("%lf", &c);
// 1 reelle Lösung
if(c == 1.21)
printf("Die doppelte Nullstelle lautet -1.10.");
// 2 reelle Lösungen
else if(c == 1.2099)
printf("Die beiden reellen Loesungen sind -1.09 und -1.11.");
else // keine reelle Lösung
printf("Es gibt keine reelle Loesung.\n");
}
作业可以偷懒,考试就不行了 ,以下是误差范围,小数点后4位即可
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, d;
printf("a: ");
scanf("%lf", &a);
printf("b: ");
scanf("%lf", &b);
printf("c: ");
scanf("%lf", &c);
d = b * b - 4 * a * c;
// 1 reelle Lösung
if(-0.0001<=d&&d<=0.0001)
printf("Die doppelte Nullstelle lautet %.2lf.\n", -b / (2*a));
// 2 reelle Lösungen
else if(d > 0)
printf("Die beiden reellen Loesungen sind %.2lf und %.2lf.\n",
(-b + sqrt(d)) / (2*a),
(-b - sqrt(d)) / (2*a));
else // keine reelle Lösung
printf("Es gibt keine reelle Loesung.\n");
}