求一元二次方程的解
求方程ax2+bx+c=0的根,分别用三个函数求当b2-4ac大于0,等于0,和小于0的根,并输出结果。从主函数输入a,b,c的值。
实现代码:
#include<iostream> #include<math.h> using namespace std; int main(){ double a,b,c,m,p,q; void dengyu(double,double); void dayu(double,double); void xiaoyu(); cout<<"please input a,b,c:"; cin>>a>>b>>c; cout<<"the result of "<<a<<"x^2+"<<b<<"x+"<<c<<"\n"; cout<<(m=b*b-4*a*c)<<endl; cout<<(p=-b/(2*a))<<endl; if(m>0){ cout<<(q=sqrt(m)/(2*a))<<endl; dayu(p,q); } else if(m==0){ q=sqrt(m)/(2*a); dengyu(p,q); } else if(m<0)xiaoyu(); return 0; } void dayu(double p,double q){ cout<<"root1:"<<(p+q)<<endl; cout<<"root2:"<<(p-q)<<endl; } void dengyu(double p,double q){ cout<<"root1=root2="<<(p+q)<<endl; } void xiaoyu(){ cout<<"there is no root"<<endl; }