Algorithm or maths is very important for us

来源:互联网 发布:c语言打印杨辉三角8行 编辑:程序博客网 时间:2024/05/16 12:38
#include <algorithm.h>
#include <stdio.h>
#include <stdlib.h>


int main()
{
    floatf(float x);
    floatxpoint(float x1,float x2);
    floatroot(float x1,float x2);

    floatx1,x2,f1,f2,x;
    do
    {
       printf("input x1,x2:\n");
       scanf("%f,%f",&x1,&x2);
       f1 = f(x1);
       f2 = f(x2);
    }
    while(f1*f2>=1e-6);
    x = root(x1,x2);
    printf("Aroot of equation is %8.4f\n",x);
    return 0;
}

float f(float x)//f= x^3-5x^2+16x-80
{
    floaty;
    y =(((x-5.0)*x)+16.0)*x-80.0;
    returny;
}

float xpoint (float x1,float x2)
{
    floatx;
    x = (x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
    return x;
}

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

0 0
原创粉丝点击