写一个用矩形法求定积分的通用函数
2023-07-06
解: #include
main()
float integral(float(8p)(float),float a,float b,int n);
float a1,b1,a2,b2,a3,b3,c,(*p)(float);
float fsin(float);
float fcos(float);
float fexp(float);
int n=20;
printf(“Input a1,b1:”);
scanf(“%f,%f”,&a1,&b2);
printf(“Input a2,b2:”);
scanf(“%f,%f”,&a2,&b2);
printf(“Input a3,b3:”);
scanf(“%f,%f”,&a3,b3);
p=fsin;
c=integral(p,a1,b1,n);
printf(“The integral of sin(x) is:%f ”,c);
p=fcos;
c=integral(p,a2,b2,n);
printf(“The integral of cos(x) is :%f ”,c);
c=integral(p,a3,b3,n);
printf(“The integral of sin(x) is :%f ”,c);
float integral(float (*p)(float),float a,float b,int n)
int i;
floatx,h,s;
h=(b-a)/n;
x=a;
s=0;
for(i=1;i