Submit: 423 Solved: 269
[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
主函数已给定如下,提交时不需要包含下述主函数
/* C代码 */
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 :%.2f\n",c);
p=fcos;
c=integral(p,a2,b2,n);
printf("The integral of cos(x) is :%.2f\n",c);
p=fexp;
c=integral(p,a3,b3,n);
printf("The integral of exp(x) is :%.2f\n",c);
return 0;
}
/* C++代码 */
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;
}
한국어 中文 فارسی English ไทย
Anything about the Problems, Please Contact Admin:admin
All Copyright Reserved 2010-2014 HUSTOJ TEAM
GPL2.0 2003-2014 HUSTOJ Project TEAM
Anything about the Problems, Please Contact Admin:admin
All Copyright Reserved 2010-2014 HUSTOJ TEAM
GPL2.0 2003-2014 HUSTOJ Project TEAM
float
fsin(
float
x)
{
return
sin
(x);
}
float
fcos(
float
x)
{
return
cos
(x);
}
float
fexp(
float
x)
{
return
exp
(x);
}
float
integral(
float
(*p)(
float
),
float
a,
float
b,
int
n)
{
float
x,i,s=0;
x=(b-a)/n;
for
(i=a;i<=b;i=i+x)
{
s=s+p(i+x/2)*x;//使用定义法求面积,如用精确函数则wrong answer;
}
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 :%.2f\n"
,c);
p=fcos;
c=integral(p,a2,b2,n);
printf
(
"The integral of cos(x) is :%.2f\n"
,c);
p=fexp;
c=integral(p,a3,b3,n);
printf
(
"The integral of exp(x) is :%.2f\n"
,c);
return
0;
}