C语言编写一个求一元二次方程的实根的程序。

如题所述

步骤:

    打开visual C++ 6.0-文件-新建-文件-C++ Source File

2.

定义变量:

#include <stdio.h>

#include <math.h>

void main()

{

double a,b,c;                                /*定义系数变量*/

double x1,x2,p;                              /*定义根变量和表达式的变量值*/

3.输入系数:

printf("请输入a,b,c:");                      /*提示用户输入三个系数*/

scanf("%lf%lf%lf",&a,&b,&c);                 /*接收用户输入的系数*/

4.输出回车:

printf("\n");                               /*输出回行*/

5.

计算根:

p=b*b-4*a*c;                            /*给表达式赋值*/

x1=(-b+sqrt(p))/(2*a);                           /*根1的值*/

x2=(-b-sqrt(p))/(2*a);                           /*跟2的值*/

6.

输出结果:

printf("x1=%f,x2=%f\n",x1,x2);                   /*输出两个根的值*/

完整的源代码:

#include <stdio.h>

#include <math.h>

void main()

{

double a,b,c;                                /*定义系数变量*/

double x1,x2,p;                              /*定义根变量和表达式的变量值*/

printf("请输入a,b,c:");                      /*提示用户输入三个系数*/

scanf("%lf%lf%lf",&a,&b,&c);                 /*接收用户输入的系数*/

printf("\n");                               /*输出回行*/

p=b*b-4*a*c;                            /*给表达式赋值*/

x1=(-b+sqrt(p))/(2*a);                           /*根1的值*/

x2=(-b-sqrt(p))/(2*a);                           /*跟2的值*/

printf("x1=%f,x2=%f\n",x1,x2);                   /*输出两个根的值*/

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-12-15
#include<stdio.h>
#include<math.h>

int main()
{
    int a, b, c;
    int x1, x2;
    int d;
    
    printf("请输入方程的三个系数:");
    scanf("%d, %d, %d", &a, &b, &c);
    
    d = b*b - 4*a*c;
    if(d > 0)
    {
        x1 = (-b + sqrt(d))/(2*a);
        x2 = (-b - sqrt(d))/(2*a);
        
        printf("方程有两个不相等的根为:%d, %d\n", x1, x2);
    }
    else if(d = 0)
    {
        x1 = x2 = (-b)/(2*a);
        printf("方程有两个相等的根为:%d\n", x1);
    }
    else if(d < 0)
    {
        printf("方程没有实根\n");
    }
    
    return 0;
}

第2个回答  2018-01-20

吐槽:书上已基本给出了思路,就是照着敲的事。

答案:

#include<stdio.h>
#include<math.h>

int main(void)
{
float a,b,c; //对应ax+by+c=0
float d;     //蝶儿塔d
float x1,x2; //根

printf("输入系数\n");
scanf("%f%f%f",&a,&b,&c);

d = b*b-4*a*c;

if(d>0)
{
x1 = (-b+sqrt(d))/(2*a);
x2 = (-b-sqrt(d))/(2*a);
printf("%f   %f\n",x1,x2);
}
else if(!d)
{
x1 = (float)-b/(2*a);
printf("%f\n",x1);
}
else
printf("无根\n");
return 0;
}

第3个回答  推荐于2017-11-18
#include <stdio.h>
#include <math.h>
int main(void)
{
double a,b,c,x1,x2,d;
scanf("%lf%lf%lf",&a,&b,&c);
d = b * b - 4 * a * c;
if(d > 0)
{
x1 = (-1 * b + sqrt(d)) / (2 * a);
x2 = (-1 * b - sqrt(d)) / (2 * a);
printf("x1 = %g,x2 = %g\n",x1,x2);
}
else if(d = 0)
{
x1 = x2 = (-1 * b) / (2 * a);
printf("x1 = %g,x2 = %g\n",x1,x2);
}
else
{
printf("方程没有实根\n");
}

return 0;
}
望采纳,谢谢~本回答被提问者采纳
第4个回答  2017-11-18
#include"stdio.h"
#include"stdlib.h"
#include"math.h"
int main()
{
double a,b,c,f,x1,x2,x,d;
printf("输入二元一次方程的系数a,b,c\n");
scanf("%lf %lf %lf",&a,&b,&c);
if(a==0)
{
x=c/b;
printf("x=%lf\n",x);
exit(0);
}
d=pow(b,2)-4*a*c;
if(d>0)
{
printf("有两个不同的实根");
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("x1=%lf,x2=%lf\n",x1,x2);
}
else if(d==0)
{
printf("有两个相同的实根");
f=2*a;
x=(-b)/f;
printf("x1=x2=%lf\n",x);
}
else
printf("无解");
return 0;
}
相似回答