主题:[讨论]请教各位大侠
#include "math.h"
main()
{float f1(float x),f2(float x);
float a,b,c,(*P)();
int n=5;
float (*p)(float);
float integral(float a,float b,float (*p)(float),int n);
printf("input a,b:");
scanf("%f%f",&a,&b);
p=f1;
c=integral(a,b,p,n);
printf("The first integral is %6.2f\n ",c);
p=f2;
c=integral(a,b,p,n);
printf("The second integral is %6.2f \n",c);
}
float integral(float a,float b,float (*P)(),int n) /* method for integral*/
{float h,x,sum,(*p)();
int i;
h=(b-a)/n;
x=a;
sum=0;
for(i=1;i<=n;i++)
{x=x+h;
sum=(*p)(x)*h+sum;
}
return(sum);
}
float f1(float x)
{float z=1+x;
return(z);
}
float f2(float x)
{float z=2*x+3;
return(z);
}