c++程序设计第3版谭浩强课后答案
C++编程:求一元二次方程式 ax2+bx+c=0 的实根,如果方程没有实根,则输出有关警告信息。
以下是此题的【c源代码】
#include <iostream>
#include <cmath>
using namespace std;
double q(double,double,double);
void main()
{double a,b,c,p,x1,x2;
cout<<"please enter a,b,c:";
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;
}
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://www.wb98.com/cjia/post/cjia_14.1.html
本站文章搜索: