求二元一次方程的解。

我想求两个二元一次方程的解,这是我的代码
方程是ax+by=c,dx+ey=f

#include
#include
#include

int main()
{
double a, b, c, d, e, f;
scanf("%lf %lf %lf %lf %lf %lf", &a, &b, &c, &d, &e, &f);
assert(fabs(a*e-d*b) < 1.0e-8);
printf("x=%lf\ny=%lf\n", (c*e-b*f)/(a*c-d*b), (d*c-a*f)/(d*b-a*c));
return 0;
}
我想当解不唯一时程序异常退出,唯一时算出结果,所以故意输入 5,1,8,20,4,6 按理应该异常退出啊,但是为什么还是求出结果了呢?

兄弟,你的程序有问题。

#include <stdio.h>
#include <math.h>
#include <assert.h>

int main()
{
double a, b, c, d, e, f;
scanf("%lf %lf %lf %lf %lf %lf", &a, &b, &c, &d, &e, &f);
assert(fabs(a*e-d*b) < 1.0e-8);  //  这里应该是assert(fabs(a*e-d*b)>1.0e-8)
//此处还可考虑添加 assert(fabs(a*c-d*b))
printf("x=%lf\ny=%lf\n", (c*e-b*f)/(a*c-d*b), (d*c-a*f)/(d*b-a*c));
//此处应该是printf("x=%lf\ny=%lf\n", (c*e-b*f)/(a*e-d*b), (d*c-a*f)/(d*b-a*e));
return 0;
}

void assert(expression)
assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行。
你输入5,1,8,20,4,6 ,此时

 fabs(d*b-a*e)<1.0e-8

为true,故不会异常退出。

断言处写的不对,即assert(fabs(a*e-d*b) < 1.0e-8);
C/C++编程时,乘号不能省略,所以1.0e-8应该改为1.0*e-8。
这样你遇到的问题就解决了

#include
#include
#include

int main()
{
double a, b, c, d, e, f;
scanf_s("%lf %lf %lf %lf %lf %lf", &a, &b, &c, &d, &e, &f);
assert(fabs(a*e - d*b) < 1.0*e - 8);
printf("x=%lf\ny=%lf\n", (c*e - b*f) / (a*c - d*b), (d*c - a*f) / (d*b - a*c));
return 0;
}
试试看