c - 当系数可能为 0 时求解二次方程
问题描述
我被要求编写一个 C 程序来解决方程ax 2 +bx+c=0,其中a、b和cdouble
是具有类型的系数。任何系数都可以为零。在这个问题中,我不清楚如何处理double
变量。
这是我的代码。至于现在,我知道我的程序无法区分两个根和无限多个根。它也没有检测到“线性方程情况”。我怎样才能让它检测到无限数量的解决方案?评论中还建议我在 b > 0 时在判别式之前用减号计算根,然后使用 Viet 定理。我知道这是因为将两个数字相加总是更准确。我也想我应该对 b < 0 做完全相反的事情。但是如果 b == 0 怎么办?在这种情况下,程序不会做任何事情。或者我应该只在 b < 0 中包含 b == 0 并且 b <= 0 ?
#include <stdio.h>
#include <math.h>
#include <float.h>
int main() {
double a, b, c, x1, x2;
scanf("%lf", &a);
scanf("%lf", &b);
scanf("%lf", &c); // just reading variables
//ax^2+bx+c=0
if ((b * b - 4 * a * c) < 0) {
printf("no");
} else {
x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a); //calculating roots
x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
if ((fabs((a * x1 * x1 + b * x1 + c)) < DBL_EPSILON) & (fabs((a * x2 * x2 + b * x2 + c)) < DBL_EPSILON)) { //plugging the roots in
if (fabs((x1 - x2)) < DBL_EPSILON) { //checking if the roots are equal
printf("%lf", &x1); // if they are equal, we print only one of them
} else {
printf("%lf", &x1); // if they are not equal, we print both.
printf("\n %lf", &x2);
}
} else { // if there are no two valid roots
if ((fabs((a * x1 * x1 + b * x1 + c)) < DBL_EPSILON)) // we try to find one root.
printf("%lf", &x1);
if (fabs((a * x2 * x2 + b * x2 + c)) < DBL_EPSILON)
printf("%lf", &x2);
if ((fabs((a * x1 * x1 + b * x1 + c)) > DBL_EPSILON) & (fabs((a * x2 * x2 + b * x2 + c)) > DBL_EPSILON)) // if both of the plugged roots don't satisfy the equation
printf("no");
}
}
return 0;
}
解决方案
由于不允许除以零,您必须将问题分为 4 种情况:
a != 0:这是您在代码中处理的情况。
a == 0 && b != 0 :这是一个线性方程,解为 x = -c/b
a == 0 && b == 0 && c != 0 :x 没有可能的值。
在最后一种情况下,a、b 和 c 等于 0:x 有无穷多个解。
编辑:删除与 epsilon 的比较,因为它们似乎没用
推荐阅读
- android-room - 安卓室。跟踪选择查询的进度
- python - 计算每个长度Python的列表(嵌套列表)中的列表数量
- bash - 在 Bash 中获取包含空格的文件名数组时出现问题
- c - '* listeNote' 中对成员 'suivant' 的错误请求,它的指针类型为 'listenote {aka notes*}'(也许您打算使用 '->' ?)
- mysql - 使用带有 nodejs 的 mysql 准备查询时,Web 浏览器崩溃
- javascript - 如何使用 JavaScript 获取光标下的特定字符?
- python - 如何使用 python 和 multiwii 串行协议校准 mpu6050 传感器?
- python - 在 Keras 中获取子模型的层输出
- c++ - 从 QGraphicsItem foreach 循环中获取 QGraphicsSimpleTextItem 的字符串?
- mysql - 如果满足多对多关系条件,Laravel 仅显示模型?