文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C语言例程29:函数的嵌套调用

C语言例程29:函数的嵌套调用

时间:2010-06-25  来源:hnrainll

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

float f(float x)
{
    float y;
    y = ((x - 8.0)*x - 12.0)*x - 30.0;
    return y;
}

float xpoint(float x1, float x2)
{
    float y;
    y = (x1 * f(x2) - x2*f(x1)) / (f(x2) - f(x1));

    return y;
}
float root(float x1, float x2)
{
    float x, y, y1;
    y1 = f(x1);

    do{
        x = xpoint(x1, x2);
        y = f(x);
        if(y * y1 > 0)
        {
            y1 = y;
            x1 = x;
        }
        else
            x2 = x;
    }
    while(fabs(y) >= 0.0001);
    return x;
}

int main(int argc, char *argv[])
{
    float x1, x2, f1, f2,x;

    do{
        printf("Please input x1, x2:\n");
        scanf("%f, %f", &x1, &x2);
        f1 = f(x1);
        f2 = f(x2);
    }
    while(f1 * f2 > 0);
     x = root(x1, x2);

    printf("A root of equation is %9.6f\n", x);
    system("pause");
    return 0;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载