C++如何求解一元二次方程有虚根的情况

求方程ax^2+bx+c=0的根。(20分)
题目内容:

编程序,根据输入a, b ,c的值,求方程ax^2+bx+c=0的根。

当a=0,b=0时 ,输出“error!”

Question:在于虚根情况下不知道计算机是否会自动计算根号下为负时直接写成数乘以i的形式(前面几种情况都对)

#include <iostream>
#include<cmath>

using namespace std;

int main()
{   double a,b,c,d;
    cin>>a>>b>>c;
    d=b*b-4*a*c;
    if(a==0 )
      {  if (b==0)
          cout<<"error!";
         else
         cout<<"x="<<-b/a;
      }
    else
    {

     if(d==0)
    cout<<"x1=x2="<<-b/(2*a);
    else if(d>0)
    cout<<"x1="<<(-b+sqrt(d))/(2*a)<<','<<"x2="<<(-b-sqrt(d))/(2*a);
    else if(a>0)
        cout<<"x1="<<(-1) *b/2/a<<'+'<<sqrt((-1)*d)/2/a<<'i' <<','<<"x2="<<(-1) *b/2/a<<sqrt((-1)*d)/2/a<<'i' ;
    else
        cout<<"x1="<<(-1) *b/2/a<<sqrt((-1)*d)/2/a<<'i' <<','<<"x2="<<(-1) *b/2/a<<sqrt((-1)*d)/2/a<<'i' ;}
    return 0;

}