题解 | #计算一元二次方程#

计算一元二次方程

http://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26

#include<stdio.h>
#include<math.h>
#define sum(a,b,c) (((b)*(b))-((4)*(a)*(c)))//函数式宏 判断deite
#define shi_bu(a,b,c) ((-b)/((2)*(a)))
#define xu_bu(a,b,c) ((sqrt(-sum(a,b,c)))/((2)*(a)))
float my_x2(float a,float b,float c)//函数求x2
{
    float x2=(-b+sqrt(b*b-4*a*c))/(2*a);
    return x2;
}
float my_x1(float a,float b,float c)//函数求x1
{
    float x1=(-b-sqrt(b*b-4*a*c))/(2*a);
    return x1;
}
void test(float a,float b,float c)
{
    if(a==0)
    {
        printf("Not quadratic equation\n");
    }
    else
    {
        if(sum(a,b,c)>0)
        {
            printf("x1=%.2f;x2=%.2f\n",my_x1(a,b,c),my_x2(a,b,c));
        }
        else if(sum(a,b,c)<0)
        {
            printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n",shi_bu(a,b,c),xu_bu(a,b,c),shi_bu(a,b,c),xu_bu(a,b,c));
        }
        else
        {
            printf("x1=x2=%.2f\n",my_x2(a,b,c));
        }
    }
}
int main()
{
    float a=0,b=0,c=0;
    while(scanf("%f%f%f",&a,&b,&c)!=EOF)
    {
        test(a,b,c);
    }
    return 0;
}
全部评论

相关推荐

理智的马里奥不愿再收感谢信:这小米咱是非去不可了是吗?
投递小米集团等公司10个岗位
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务