栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > C++面试题库

写一个用矩形法求定积分的通用函数,分别求:

写一个用矩形法求定积分的通用函数,分别求:

解: #include  <math.h>
     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:%fn”,c);
  p=fcos;
  c=integral(p,a2,b2,n);
  printf(“The integral of cos(x) is :%fn”,c);
  c=integral(p,a3,b3,n);
  printf(“The integral of sin(x) is :%fn”,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<=n;i++)
     {x=x+h;
     s=s+(*p)(x)*h;
     }
    return(s);
    }
   float fsin(float x)
     {return sin(x);}
    float fcos(float x)
     {return cos(x);}
    float fexp(float x)
     {return exp(x);}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/265892.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号