求形如ax2+bx+c=0(a=0)的方程的根。
输入a,b,c的值
输出根的值
C++代码
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double a,b,c,x;
void fun(double a1,double b1,double c1);
cin>>a>>b>>c;
x=b*b-4*a*c;
if(x<0)
cout<<"no solution"<<endl;
else
fun(a,b,c);
return 0;
}
void fun(double a1,double b1,double c1)
{
double x1,x2;
x1=(-b1+sqrt(b1*b1-4*a1*c1))/2*a1;
x2=(-b1-sqrt(b1*b1-4*a1*c1))/2*a1;
if(x1==x2)
cout<<x1<<endl;
else if(x1>x2)
cout<<x1<<" "<<x2<<endl;
else if(x1<x2)
cout<<x2<<" "<<x1<<endl;
}