#include
#include
void main( void )
{
double a,b,c,d;
printf("请输入一元二次方程的三个系数:");
scanf("%lf %lf %lf",&a,&b,&c);
d=b*b-4*a*c;
if(d<0){printf("方程没有实根。\n"); return 1;}
if(d==0){printf("方程有重根 x=%lf\n",-b/(2*a)); return 0;}
printf("方程有二个实根 x1=%lf x2=%lf",(-b+sqrt(d))/(2*a),(-b-sqrt(d))/(2*a));
system("pause");
return 0;
}
#include
#include
int main()
{
int i=1,n=10;
double a,b,c,x1,x2,q,p,data;
while(i<=n)
{
scanf("%lf%lf%lf",&a,&b,&c);
data=b*b-4*a*c;
q=(double)(-b)/(2*a);
p=(double)sqrt(data)/(2*a);
if(data>(1e-6))
{
x1=q+p;
x2=q-p;
printf("x1=%.5lf;x2=%.5lf\n",x1,x2);
}
else if(fabs(data)<(1e-6))// 这里判断data等于0
{
x1=x2=q;
printf("x1=%.5lf;x2=%.5lf\n",q,q);
}
else
{
p=(double)sqrt(-data)/(2*a);
if(p>=(1e-6))
{
printf("x1=%.5lf+%.5lfi;x2=%.5lf-%.5lfi\n",q,p,q,p);
}
else
{
printf("x1=%.5lf-%.5lfi;x2=%.5lf+%.5lfi\n",q,p,q,p);
}
}
i=i+1;
}
return 0;
}
没发现其他的问题,欢迎交流哈。
#include
#include
void da(double a,double b,double d);
void xiao();
void deng(double a,double b);
void main()
{
double a,b,c,d;
printf("请输入一元二次方程的三个系数:\n");
scanf("%lf %lf %lf",&a,&b,&c);
d=b*b-4*a*c;
if(d<0){xiao();}
if(d==0){deng(a,b);}
if(d>0){da(a,b,d);}
system("pause");
}
void da(double a,double b,double d){
printf("方程有二个实根 x1=%lf x2=%lf",(-b+sqrt(d))/(2*a),(-b-sqrt(d))/(2*a));
}
void xiao(){
printf("方程没有实根。\n");
}
void deng(double a,double b){
printf("方程有重根 x=%lf\n",-b/(2*a));
}