Problem E: C语言习题 矩形法求定积分
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 410 Solved: 260
[ Submit][ Status][ Web Board]
Description
写一个用矩形法求定积分的通用函数,分别求
(说明: sin,cos,exp已在系统的数学函数库中,程序开头要用#include<cmath>)。
Input
输入求sin(x) 定积分的下限和上限
输入求cos(x) 定积分的下限和上限
输入求exp(x) 定积分的下限和上限
Output
求出sin(x)的定积分
求出cos(x)的定积分
求出exp(x)的定积分
Sample Input
0 1
0 1
0 1
Sample Output
The integral of sin(x) is :0.48
The integral of cos(x) is :0.83
The integral of exp(x) is :1.76
HINT
#include<iostream>
#include<iomanip>
#include<cmath>
#define PI 3.1415
using
namespace
std;
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=1;
float
c,sum=0.0;
c=(b-a)/n;
a+=c;
for
(i=1;i<=n;i++)
{
sum+=p(a)*c;
a+=c;
}
return
sum;
}
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;
cin>>a1>>b1;
cin>>a2>>b2;
cin>>a3>>b3;
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
p=fsin;
c=integral(p,a1,b1,n);
cout<<
"The integral of sin(x) is :"
<<c<<endl;
p=fcos;
c=integral(p,a2,b2,n);
cout<<
"The integral of cos(x) is :"
<<c<<endl;;
p=fexp;
c=integral(p,a3,b3,n);
cout<<
"The integral of exp(x) is :"
<<c<<endl;
return
0;
}