该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
struct num_list /*用链表表示实数列*/
{ double num;
struct num_list *next;
};
void insert(struct num_list *p,double x) /*向链表中加入新的成员*/
{ struct num_list *q;
q=(struct num_list*)malloc(sizeof(struct num_list));
q->next=p->next;
q->num=x;
p->next=q;
};
void show(struct num_list *h) /*显示链表中的成员*/
{ struct num_list *p;
p=h->next;
while(p!=NULL)
{ printf("%g\t",p->num);
p=p->next;
}
};
#define TX 0.000000001 /*设置精度控制及搜索步长的相对值*/
#define TY 0.000000001
#define H 0.00001
struct num_list *binserchroot(double (*f)(double),double y,double a,double b)
{ double x0,x1,x;
double tx,ty,h;
struct num_list *p;
tx=(fabs(a-b)+1)*TX; /*设置精度控制及搜索步长的绝对值*/
ty=TY*TY;
h=(fabs(a-b)+1)*H;
p=(struct num_list*)malloc(sizeof(struct num_list));
if(a>b)
{ x=a;
a=b;
b=x;
}
if(fabs(f(a)-y)
x=a;
while(x
{ if(fabs(f(x+h)-y)
if((f(x)-y)*(f(x+h)-y)<0)
{ x0=x;
x1=x+h;
while(x1-x0>tx)
{ x=(x0+x1)/2;
if(fabs(f(x)-y)
{ insert(p,x);
break;
}
if((f(x)-y)*(f(x0)-y)>0) x0=x;
else x1=x;
}
if(fabs(f(x)-y)>=ty) insert(p,x);
}
x=x+h;
}
return p;
};
main()
{ struct num_list *p;
p=binserchroot(cos,-0.999999,0,20);
printf("cos(x)=-0.999999\tx=?\n");
show(p);
p=binserchroot(cos,-1,0,20);
printf("\ncos(x)=-1\tx=?\n");
show(p);
printf("\nsin(x)=0\tx=?\n");
p=binserchroot(sin,0,0,20);
show(p);
return 0 ;
}