首先,来编写一个函数solve,给定浮点数a,b,c,d,e,f,求解方程组ax+by=c,dx+ey=f
任务1:使用assert宏,让解不唯一时异常退出
任务2:解不唯一时仍然正常返回,但调用者有办法知道解的数量(无解、唯一解、无穷多组解)
任务一
#include<iostream>
#include<assert.h>
using namespace std;
int solve(double a,double b,double c,double d,double e,double f){
double x,y;
assert(a*e!=b*d);
x=(c*e-b*f)/(a*e-b*d);
y=(c*d-a*f)/(b*d-a*e);
cout<<x<<endl;
cout<<y;
}
int main()
{
double a,b,c,d,e,f;
cin>>a>>b>>c>>d>>e>>f;
//double x,y;
solve(a,b,c,d,e,f);
return 0;
}
任务二
#include<iostream>
#include<assert.h>
using namespace std;
int solve(double a,double b,double c,double d,double e,double f){
double x,y;
if(a*e!=b*d)
{
x=(c*e-b*f)/(a*e-b*d);
y=(c*d-a*f)/(b*d-a*e);
cout<<x<<endl;
cout<<y;
}
else if((a*e==b*d)&&!(a/d==b/e==c/f))
{
cout<<"无解"<<endl;
}
else
{
cout<<"无穷多组解"<<endl;
}
}
int main()
{
double a,b,c,d,e,f;
cin>>a>>b>>c>>d>>e>>f;
//double x,y;
solve(a,b,c,d,e,f);
return 0;
}