题目描述
读取三个浮点数 a,b,c_的值,利用一元二次方程求根公式对方程 ax2+bx+c_=0进行求解。
一元二次方程求根公式为:
x=−b±√b2−4ac
−−−−−−−
√2a
如果 b2−4ac<0导致方程无解或 a=0,则输出 Impossivel calcular。
C++ 代码
#include <bits/stdc++.h>
using namespace std;
void er(double a,double b,double c,double m) {
double delta =b*b-4*a*c;
double root1 = (-b+sqrt(delta))/(2 * a);
double root2 = (-b-sqrt(delta))/(2 * a);
if(delta>0){
if(root1<root2){
printf("R1 = %.5lf\nR2 = %.5lf\n", root1, root2);
m=0;
}
else if(root2<root1){
printf("R1 = %.5lf\nR2 = %.5lf\n", root2, root1);
m=0;
}
}
}
int main() {
double a,b,c,m=1;
cin>>a>>b>>c;
er(a,b,c,m);
if(m==1){
cout<<"Impossivel calcular";
}
return 0;
}