8.13 写一个用矩阵法求定积分的通用函数,分别求 sinx,cosx,exp(x)的积分;

8.13 写一个用矩阵法求定积分的通用函数,分别求 sinx,cosx,exp(x)的积分;

个人代码如下:

#include
#include
int main()
{
    double m,n;
    double fun(double m,double n,double (*p)(double));
    double fsinx(double);
    double fcosx(double);
    double fexp(double);
    
    scanf("%lf%lf",&m,&n);
    printf("%lf\n",fun(m,n,fsinx));
    scanf("%lf%lf",&m,&n);
  printf("%lf\n",fun(m,n,fcosx));
    scanf("%lf%lf",&m,&n);
  printf("%lf",fun(m,n,fexp));
}

//矩阵法求积分的功能函数
double fun(double m,double n,double (*p)(double))
{
    int i;
    double h,sum;
    h=fabs(n-m)/1000;
    for(i=0,sum=0;i<1000;sum=sum+(*p)(m+i*h+h/2),i++);
    return sum*h;

}

double fsinx(double x)
{
    return sin(x);
}

double fcosx(double x)
{
    return cos(x);
}

double fexp(double x)
{
    return exp(x);
}



你可能感兴趣的:(c语言程序设计(第四版),谭浩强,第八章答案)