注册 登录
编程论坛 C++教室

直接给x1,x2赋值和用scanf输入有什么不同吗?

icecream235 发布于 2011-08-18 03:24, 415 次点击
我在学C,刚编的不知道哪错了,下面的程序我把   
        double x1,x2,x;
    scanf("%f %f",&x1,&x2);

换成    double x1=0.0,x2=10.0,x;就对了为什么啊?????


#include<stdio.h>
#include<math.h>
double y(double x);
double x(double x1,double x2);
double root(double x1,double x2);


double y(double x)
{
    double yj;
    yj=x*x*x-5*x*x+16*x-80;
    return yj;
}
double x(double x1,double x2)
{
    double xj;
    xj=(x1*y(x2)-x2*y(x1))/(y(x2)-y(x1));
    return xj;
}
double root(double x1,double x2)
{

    double xx,yy;
    xx=x1;
    while(fabs(y(xx))>0.0001)
    {
        xx=x(x1,x2);
        yy=y(xx);
        if(yy*y(x1)>0)
            x1=xx;
        else
            x2=xx;
    }
    return xx;
}
main()
{
    double x1,x2,x;
    scanf("%f %f",&x1,&x2);
    while(1)
    {
        if(y(x1)*y(x2)<0)
        {x=root(x1,x2);break;}
        else
        {
            printf("重新输入:");
            scanf("%f%f",&x1,&x2);
        }
    }
    printf("%6.3f\n",x);

}
2 回复
#2
八画小子2011-08-18 04:13
刚才帮你调试了一下,你把double 改成 float就可以了。C语言已经N多年没玩过了,所以为什么这样,我也不是太清楚。还有顺便给你说一下,你最好在main()之前加个int 在最后一个}之前添加return 0;。你的这种代码风格很不符合C标准。应该是看谭浩强的书后被误导了吧?
#3
rjsp2011-08-18 08:09
double x1,x2,x;
scanf("%f %f",&x1,&x2);

double x1,x2,x;
scanf("%lf %lf",&x1,&x2);
1