题目描述
写一个用矩形法求定积分的通用函数,分别求
(说明: sin,cos,exp已在系统的数学函数库中,程序开头要用#include<cmath>)。
输入
输入求sin(x) 定积分的下限和上限
输入求cos(x) 定积分的下限和上限
输入求exp(x) 定积分的下限和上限
输出
求出sin(x)的定积分
求出cos(x)的定积分
求出exp(x)的定积分
样例输入
0 1
0 1
0 1
样例输出
The integral of sin(x) is :0.48
The integral of cos(x) is :0.83
The integral of exp(x) is :1.76
参考代码
#include<stdio.h>
#include<math.h>
float fsin(float a)
{
return sin(a);
}
float fcos(float a)
{
return cos(a);
}
float fexp(float a)
{
return exp(a);
}
float integral(float (*p)(float),float a,float b,int n)
{
int i;
float x,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;
}
int main()
{
float integral(float (*p)(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;
scanf("%f%f",&a1,&b1);
scanf("%f%f",&a2,&b2);
scanf("%f%f",&a3,&b3);
p=fsin;
c=integral(p,a1,b1,n);
printf("The integral of sin(x) is :%.2fn",c);
p=fcos;
c=integral(p,a2,b2,n);
printf("The integral of cos(x) is :%.2fn",c);
p=fexp;
c=integral(p,a3,b3,n);
printf("The integral of exp(x) is :%.2fn",c);
return 0;
}
解析
暂无