[求助]求二次函数根的程序出错了,刚入门同学的求教。
#include<stdio.h> //请帮我看看,这程序哪里有问题。。才导致下面的运行结果。这是求ax^2+bx+c=0的根的程序#include<math.h>
void fun1(double a,double b,double c,double d) //Δ=0的情况
{
double ans=-b/(2*a);
printf("b^2-4*a*c==0,x1=x2=%lf.\n",ans);
}
void fun2(double a,double b,double c,double d) //Δ>0的情况
{
double ans1,ans2;
ans1=(-b-sqrt(d))/(2*a);
ans2=(-b-sqrt(d))/(2*a);
printf("b^2-4*a*c>0,x1=%lf,x2=%lf.\n",ans1,ans2);
},
void fun3(double a,double b,double c,double d) //Δ<0的情况,输出复数解
{
double real,imar;
real=-b/(2*a);
imar=sqrt(-d)/(2*a);
printf("b*b-4*a*c<0,x1=%lf+%lfi,x2=%lf-%lfi.\n",real,imar,real,imar);
}
int main() //我感觉是不是主函数的问题
{
void fun1(double a,double b,double c,double d);
void fun2(double a,double b,double c,double d);
void fun3(double a,double b,double c,double d);
double a,b,c,d;
printf("please intput a,b,c.\n");
while(scanf("%lf,%lf,%lf",&a,&b,&c)!=EOF)
{
d=b*b-4*a*c;
if(d==0)fun1(a,b,c,d);
else if(d>0)fun2(a,b,c,d);
else fun3(a,b,c,d);
printf("please enter put a,b,,c.\n");
}
}
//无限循环,用的DEV编译器
[此贴子已经被作者于2016-5-25 16:24编辑过]