C++用函数求一元二次方程的根,

1个回答

  • #include

    #include

    #include

    main()

    {

    float a ,b,c,delta,x1,x2,y1,y2;

    do

    {

    printf("please input the factors of a quadratic equation:a,b,cn");

    scanf("%f%f%f",&a,&b,&c);

    printf("a=%f,b=%f,c=%fn",a,b,c);

    delta=b*b-4*a*c;

    if(a==0&&b==0)

    {

    printf("the quadratic equation has no any rootn");

    }

    else if(a!=0)

    { if(delta>=0)

    {

    x1=(-b+sqrt(delta))/(2*a);

    x2=(-b-sqrt(delta))/(2*a);

    printf("the roots of the quadratic equation:x1=%.2f,x2=%.2fn",x1,x2);

    }

    else

    {

    x1=x2=-b/(2*a);

    y1=y2=sqrt(abs(delta))/(2*a);

    printf("the roots of the quadratic equation:x1=%.2f+i%.2f,x2=%.2f-i%.2fn",x1,y1,x2,y2);

    }

    }

    else if(a==0)

    {

    x1=x2=-c/b;

    printf("x1=x2=%.2fn",x1);

    }

    printf("Press "q" key to exit programe! n");

    }while(getch()!='q');

    getch();

    }