Chinaunix首页 | 论坛 | 博客
  • 博客访问: 817104
  • 博文数量: 321
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 936
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-23 11:25
文章分类

全部博文(321)

文章存档

2017年(1)

2016年(10)

2015年(61)

2014年(187)

2013年(62)

分类: C/C++

2014-06-24 10:15:12

#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;
}


阅读(305) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~