请求一元二次方程的根

img

运行截图:

img

代码如下

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main()
{
    float a,b,c;
    float t;
    float x1,x2;
    cin >> a >> b>>c;
    t = b*b - 4*a*c;
    if(t>0)
    {
        x1 = (-b+sqrt(t))/(2*a);
        x2 = (-b-sqrt(t))/(2*a);
        printf("x1=%.5f;x2=%.5f",x1,x2);
    }else if (t==0)
    {
        x1 = (-b)/(2*a);
        printf("x1=x2=%.5f",x1);
    }else
    {
        x1 = (-b)/(2*a);
        x2 = sqrt(-t)/(2*a);
        printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi",x1,x2,x1,x2);
    }
    return 0;
}

直接套公式啊