写一个用矩形法求定积分的通用函数
编辑: 面试啦 发布时间: 2023-07-31 23:11:00
解: #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\n”,c);
p=fcos;
c=integral(p,a2,b2,n);
printf(“The integral of cos(x) is :%f\n”,c);
c=integral(p,a3,b3,n);
printf(“The integral of sin(x) is :%f\n”,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);}
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\n”,c);
p=fcos;
c=integral(p,a2,b2,n);
printf(“The integral of cos(x) is :%f\n”,c);
c=integral(p,a3,b3,n);
printf(“The integral of sin(x) is :%f\n”,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);}
词条:c面试题
面试题库导航
- C++面试题
- JAVA面试题
- Oracle面试题
- MySQL面试题
- Linux面试题
- 网络工程师面试题
- 网管面试题
- 软件工程师面试题
- PHP面试题
- .net面试题
- 软件测试面试题
- EJB面试题
- Spring面试题
- Javascript面试题
- Python面试题
- QTP面试题
- Servlet面试题
- J2EE面试题
- Database面试题
- ASP面试题
- Unix面试题
- Loadrunner面试题
- 通信面试题
- 嵌入式面试题
- Ruby面试题
- Delphi面试题
- Mobile开发面试题
- SOA面试题
- JAVA面试题面试题
- 研发工程师面试题
- 软件架构师面试题
- 系统工程师面试题
- Android面试题
- JAVA程序员
- C#面试题