求解一元二次方程组

首先判断是否满足构成一元二次方程组的条件,其次判断是实根还是虚根,最后求出根的大小。

#include
#include
int main()
{
        float a, b, c;
        float i, j;
        float root1, root2;
        float t;
        printf("Please input the coefficient of the equation:\n");
        scanf("%f %f %f", &a, &b, &c);
        t = b*b-4*a*c;
        if(a==0)
        {
                printf("Not a system of quadratic equation!\n");
                return 0;
        }
        if(t == 0 || t > 0)
        {
                if(t == 0)
                {
                        root1 = -b / (2*a);
                        root2 = root1;
                        printf("root1 = root2 = %f\n", root1);
                }
                else
                {
                        root1 = (-b+sqrt(t)) /  (2*a);
                        root2 = (-b-sqrt(t)) /  (2*a);
                        printf("root1 = %f \n", root1);
                        printf("root2 = %f \n", root2);
                }
        }
        else
        {
                i = -b/(2*a);
                j = sqrt(-t)/ (2*a);
                printf("root1 = %f + %fi\n", i, j);
                printf("root2 = %f - %fi\n", i, j);
        }
        return 0;
}

输出:

(1)Please input the coefficient of the equation:

2 5 4

root1 = -1.250000 + 0.661438i

root2 = -1.250000 - 0.661438i

(2)Please input the coefficient of the equation:

0 2 1

Not a system of quadratic equation!

(3)Please input the coefficient of the equation:

1 5 1

root1 = -0.208712

root2 = -4.791288

你可能感兴趣的:(C,Language,Practice)