经典一元二次方程根的求解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hpu_zhn/article/details/83615234
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    double a,b,c,d,p,q,x1,x2;
    scanf("%lf%lf%lf",&a,&b,&c);
    p=0-b/(2*a);
    d=4*a*c-b*b;
    if(b*b-4*a*c==0){ 
        printf("x1=x2=%.5f",p);
    }
    else if(b*b-4*a*c>0){
        q=sqrt(-d)/(2*a);
        printf("x1=%.5f;x2=%.5f",p+q,p-q);
    }
    else {
        q=sqrt(d)/(2*a);
        printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi",p,q,p,q);
    }
        return 0;
}

猜你喜欢

转载自blog.csdn.net/hpu_zhn/article/details/83615234