#include<stdio.h>
#include<math.h>
void clean();
int Input(double*a,double*b,double*c);
int check(double a);
void calc(double*a,double*b,double*c,double*d);
int main()
{
double a,b,c,d;
while(1)
{
if(Input(&a,&b,&c))
{
d = b * b - 4 * a * c;
calc(&a,&b,&c,&d);
}
}
}
void clean()
{
char ch;
while((ch = getchar())!='\n');
}
int Input(double*a,double*b,double*c)
{
printf("aX+bY+c=0\n请输入参数:\n");
printf("a:");
scanf("%lf",a);
if(check(*a))
{
return 0;
}
printf("b:");
scanf("%lf",b);
printf("c:");
scanf("%lf",c);
return 1;
}
int check(double a)
{
if(-0.000001 < a && a < 0.000001)
{
printf("a为零就不是个一元二次方程!\n");
clean();
return 1;
}
return 0;
}
void calc(double*a,double*b,double*c,double*d)
{
if(*d<0)
{
printf("无解!\n");
clean();
}
else if (*d >0.000001)
{
printf("X1 = %lf,X2 = %lf\n",(- *b + sqrt(*d))/(2 * *a),(- *b - sqrt(*d))/(2 * *a));
clean();
}
else
{
printf("X = %lf\n",(0 - sqrt(*b))/(2 * *a));
clean();
}
}
|