C语言编写一个程序求解一元二次方程ax2+bx+c=0的根。要求系数a,b,c从键盘输入在实数范围内求解

当判别式△<0时要显示无实数根的信息This equation haven't real root

#include <stdio.h>
#include <math.h>
int main()
{
    float a, b, c, jud;
    printf ("输入二次方程的三个系数(第一个不能为0):");
    scanf ("%f %f %f", &a, &b, &c);
    jud = b * b - 4 * a * c; //根的判别式
    if (jud > 0)
    {
        printf ("该方程有两个不相等的实根:\n");
        printf ("x1 = %.2f\n",(-b + sqrt (jud)) / (2 * a));
        printf ("x2 = %.2f\n", (-b - sqrt (jud)) / (2 * a));
    }
    else if (jud == 0)
    {
        printf ("该方程有两个相等的实根:\n");
        printf ( "x1 = x2 =  %.2f\n", -b / (2 * a));
    }
    else
        printf ("This equation haven't real root\n");
 
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-03-31

从#include "stdio.h"开始

1、#include "stdio.h"

2、#include "math.h"

3、main()

4、{ float a,b,c,p,q,k,l

5、{printf("\n \n\n")

6、printf(">> 输入a,b,c \n\n>> ")

7、scanf("%f,%f,%f",&a,&b,&c)

8、printf("\n--------------------------------------------------")

9、printf("\n\n你的方程为: %f*(x)^2+%f*(x)+%f=0\n",a,b,c)

10、l=b*b-4*a*c

11、k=pow(l,0.5)

12、p=(k-b)/(2*a)

13、q=(-k-b)/(2*a)

14、if (l

本回答被网友采纳
第2个回答  2018-02-22
#include <iostream>
#include <cstdio>
#include <cmath>
//专属解一元二次方程神器

using namespace std;
int main(int argc, char** argv)
{
double a,b,c,x1,x2;
cin>>a>>b>>c;
if(b*b==4*a*c)//判断两实根是否相等
{
x1=(-b+sqrt(b*b-4*a*c))/(2*a);//根据公式计算两根
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("x1=x2=%.5lf",x1);
}
else if(b*b>4*a*c)//判断是否有实根
{
x1=(-b+sqrt(b*b-4*a*c))/(2*a);//根据公式计算两根
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
if(x1>x2)
{
if(x1==0)
{
x1=0;
printf("x1=%.5lf;x2=%.5lf",x1,x2);
}
else
{
printf("x1=%.5lf;x2=%.5lf",x1,x2);
}
}
else
{
if(x1==0)
{
printf("x1=%.5lf;x2=%.5lf",x2,x1);
}
else
{
printf("x1=%.5lf;x2=%.5lf",x2,x1);
}
}
}
return 0;
}
//希望采纳
第3个回答  2017-10-25
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, jud;
scanf ("%f %f %f", &a, &b, &c);
jud = b * b - 4 * a * c;
if (jud > 0)
{
printf ("%7.2f",(-b + sqrt (jud)) / (2 * a));
printf ("%7.2f", (-b - sqrt (jud)) / (2 * a));
}
return 0;
}
相似回答