用C语言编写求一元二次方程根的程序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用C语言编写求一元二次方程根的程序相关的知识,希望对你有一定的参考价值。
条件要判断的充分
用C语言编写求一元二次方程根的程序,条件判断的充分,步骤如下:
void main()
float a,b,c,delta;
scanf("%f%f%f",&a,&b,&c);
if(a!=0)
delta=b*b-4*a*c;
if(delta==0)
printf("x1=x2=%7.2f",-b/(2*a));
else if(delta>0)
printf("x1=%7.2f",(-b+sqrt(delta))/(2*a));
printf("x2=%7.2f",(-b-sqrt(delta))/(2*a));
else
printf("x1=%7.2f+i%7.2f",-b/(2*a),sqrt(-delta)/(2*a));
printf("x2=%7.2f-i%7.2f",-b/(2*a),sqrt(-delta)/(2*a));
else if(b!=0)
printf("x=%7.2f",-c/b);
else if(c==0)
printf("0=0,x为任意解
");
else
printf("%f=0,error!",c);
#include <math.h>
int main()
double a,b,c,disc,x1,x2,realpart,imagpart;
scanf("%lf%lf%lf",&a,&b,&c);
printf("The equation");
if (fabs(a)<=1e-6)
printf(" is not a quadratic\\n");
else
disc=b*b-4*a*c;
if (fabs(disc)<=1e-6)
printf(" has two equal roots:%8.4f\\n",-b/(2*a));
else
if (disc>0)
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf(" has distinct real roots:%8.4f and %8.4f\\n",x1,x2);
else
realpart=-b/(2*a);
imagpart=sqrt(-disc)/(2*a);
printf(" has complex roots:\\n");
printf("%8.4f+%8.4fi\\n",realpart,imagpart);
printf("%8.4f-%8.4fi\\n",realpart,imagpart);
return 0;
参考技术B #include "stdio.h"
#include "math.h"
double x1,x2,p;
float file1(float a,float b)
x1=(-b+sqrt(p))/2*a;
x2=(-b-sqrt(p))/2*a;
return 0;
float file2(float a,float b)
x1=x2=(-b+sqrt(p))/2*a;
return 0;
void main()
float a,b,c;
scanf("%f%f%f",&a,&b,&c);
p=b*b-4*a*c;
printf("方程是:%.3f*x*x+%.3f*x+%.3f=0\n",a,b,c);
if(p>0)
file1(a,b);
printf("X1=%f\tX2=%f\n",x1,x2);
else if(p==0)
file2(a,b);
printf("X1=%f\tX2=%f\n",x1,x2);
else printf("方程无解");
运行正确 输入: 2 3.7 1.2
输出 X1=-1.677625 X2=-5.722375 参考技术C #include<stdio.h>
#include<math.h>
int main(void)
float a,b,c;
double x1,x2;
double delta;
printf("输入a,b,c数值:\n");
scanf("%f%f%f",&a,&b,&c);
if (a!=0)
delta=pow(b,2)-4*a*c;
if(delta==0)
printf("x1=x2=%.2f\n",-b/(2*a));
if(delta>0)
x1=(-b+sqrt(delta))/(2*a);
x2=(-b-sqrt(delta))/(2*a);
printf("x1=%.2f x2=%.2f\n",x1,x2);
if(delta<0)
printf("无解!\n");
return 0 ;
求C语言编程:1,用多分支的方法编写求一元二次方程的所有根的程序;a,b,c由键盘输入.
是多分直的方法~~~
不是的不要~~
#include<math.h>
int main()
double a,b,c,disc,x1,x2,p,q,x;
scanf("%lf%lf%lf",&a,&b,&c);
disc=b*b-4*a*c;
if(disc>=0)
if(disc>0)
p=-b/(2.0*a);
q=sqrt(disc)/(2.0*a);
x1=p+q;
x2=p-q;
printf("x1=%7.2f\nx2=%7.2f\n",x1,x2);
else
x=-b/(2*a);
printf("x=%7.2f\n",x);
else
printf("此方程无解。。。\n");
return 0;
本回答被提问者采纳
以上是关于用C语言编写求一元二次方程根的程序的主要内容,如果未能解决你的问题,请参考以下文章