求方程的根,a、b、c从键盘输入。
C语言程序如下:
#include <stdio.h>
#include <math.h>
int main()
{
void two_equal_root(double m,double n);//Δ=0时
void two_real_root(double m,double n,double l);//Δ>0时
void two_comlex_root(double m,double n,double l);//Δ<0时
double a,b,c,disc;
printf("请从键盘依次输入a、b、c的值:");
scanf("%lf%lf%lf",&a,&b,&c);
if(fabs(a)<=1e-6){printf("此方程不是一元二次方程!");return 0;}
disc=b*b-4*a*c;
if(fabs(disc)<=1e-6) two_equal_root(a,b);//Δ=0时
else
{
if(disc>1e-6) two_real_root(a,b,disc);//Δ>0时
else two_comlex_root(a,b,disc);//Δ<0时
}
return 0;
}
void two_equal_root(double m,double n)//Δ=0时
{
double x;
x=(-n)/(2*m);
printf("有两个相等的根:x=%.2f",x);
}
void two_real_root(double m,double n,double l)//Δ>0时
{
double x1,x2;
x1=(-n+sqrt(l))/(2*m);
x2=(-n-sqrt(l))/(2*m);
printf("有两个不等的实根:x1=%.2f,x2=%.2f",x1,x2);
}
void two_comlex_root(double m,double n,double l)//Δ<0时
{
double real_part,imag_part;
real_part=(-n)/(2*m);
imag_part=sqrt(-l)/(2*m);
printf("有两个不等的复根:x1=%.2f+%.2fi,x2=%.2f-%.2fi",real_part,imag_part,real_part,imag_part);
}
程序运行结果如下:



请各位大佬多多指正!!!
4672

被折叠的 条评论
为什么被折叠?



