求解一元二次方程组

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

#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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值