发布网友 发布时间:2022-04-21 06:52
共1个回答
热心网友 时间:2022-06-18 13:34
求一元二次方程式a*x*x+b*x+c=0的实根,如果方程没有实根,
则输出有关警告信息。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double q(double,double,double);
double a,b,c,p,x1,x2;
cout<<"请输入3个数:";
cin>>a>>b>>c;
p=-b/(2*a);
try
{
x1=p+q(a,b,c);
x2=p-q(a,b,c);
cout<<"x1="<<x1<<endl<<"x2="<<x2<<endl;
}
catch(double d)
{
cout<<"a="<<a<<",b="<<b<<",c="<<c<<",disc="<<d<<",error!"<<endl;
}
cout<<"end"<<endl;
return 0;
}
double q(double a,double b,double c)
{
double disc;
disc =b*b-4*a*c;
if(disc<0) throw disc;
return sqrt(disc)/(2*a);
}
参考资料:http://hi.baidu.com/tianli921/blog/item/475bc40854ee960b7b8223.html