好恶心的分类处理,同意的点个赞
#include<iostream> #include<iomanip> #include<cmath> using namespace std; int main() { float a,b,c,x1,x2; while(cin>>a>>b>>c) { if(((b*b-4*a*c)>0)&&a) { x1=(-b-sqrt(b*b-4*a*c))/(2*a); x2=(-b+sqrt(b*b-4*a*c))/(2*a); cout<<fixed<<setprecision(2)<<"x1="<<x1<<";"<<"x2="<<x2<<endl; } else if((b*b-4*a*c==0)&&a) { if(0==b&&a>0) x1=x2=b/(2*a); else x1=x2=-b/(2*a); cout<<fixed<<setprecision(2)<<"x1=x2="<<x1<<endl; } else if((b*b-4*a*c)<0&&a) { if(b&&a>0) { cout<<fixed<<setprecision(2)<<"x1="<<-b/(2*a)<<"-"<<sqrt(-(b*b-4*a*c))/(2*a)<<"i;"; cout<<"x2="<<-b/(2*a)<<"+"<<sqrt(-(b*b-4*a*c))/(2*a)<<"i"<<endl; } else if(b&&a<0) { cout<<fixed<<setprecision(2)<<"x1="<<-b/(2*a)<<sqrt(-(b*b-4*a*c))/(2*a)<<"i;"; cout<<"x2="<<-b/(2*a)<<"+"<<-sqrt(-(b*b-4*a*c))/(2*a)<<"i"; } else if (0==b&&a>0) { cout<<fixed<<setprecision(2)<<"x1="<<b/(2*a)<<"-"<<sqrt(-(b*b-4*a*c))/(2*a)<<"i;"; cout<<"x2="<<b/(2*a)<<"+"<<sqrt(-1*(b*b-4*a*c))/(2*a)<<"i"; } } else if (!a) { cout<<"Not quadratic equation"<<endl; } } return 0; }