求一元二次方程的根【double型的0输出%.2lf为-0.00】

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define eps 1e-6

int main() {
    double a,b,c;
    cin>>a>>b>>c;

    if(a==0 && b==0) {
        if(c==0) {
            cout<<"Zero Equation"<<endl;
            return 0;
        }
        cout<<"Not An Equation"<<endl;
        return 0;
    }
    if( fabs(b*b-4*a*c)<=eps) {
        double ans=-1*b+sqrt(b*b-4*a*c);
        ans/=2.0*a;
        printf("%.2lf\n",ans);
        return 0;
    }
    if(b*b-4*a*c>eps) {
        if(a<=eps) {
            printf("%.2lf\n",-c/b);
            return 0;
        }
        double ans=-1*b+sqrt(b*b-4*a*c);
        ans/=2.0*a;
        double ans2=-1*b-sqrt(b*b-4*a*c);
        ans2/=2.0*a;
        if(ans-ans2>eps) {
            printf("%.2lf\n",ans);
            printf("%.2lf\n",ans2);
            return 0;
        }
        printf("%.2lf\n",ans2);
        printf("%.2lf\n",ans);
        return 0;
    }
    double a1;
    double a2;
    c=(-1)*c/a+(b/a*0.5)*(b/a*0.5);
    a1=(-1)*0.5*b/a;
    c=fabs(c);
    a2=sqrt(c);
    if(a1==0)
    {
        printf("0.00+%.2lfi\n",a1,a2);
        printf("0.00-%.2lfi\n",a1,a2);
        return 0;
    }
    printf("%.2lf+%.2lfi\n",a1,a2);
    printf("%.2lf-%.2lfi\n",a1,a2);
    return 0;
}
posted @ 2016-07-07 15:43  see_you_later  阅读(322)  评论(0编辑  收藏  举报