求解一元二次方程组

陌路散爱 提交于 2020-02-03 17:52:02

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

#include<stdio.h>
#include<math.h>
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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!