第1个回答 2019-10-28
float
integral(float
(*fun)(float),
float
a,
float
b);
其中,a、b表示
积分
区间
,fun是
函数指针
。
float
f1(float
x)
{
float
f;
f
=
1
+
x*x;
return
f;
}
float
f2(float
x)
{
float
f;
f
=
1
+
x
+
x*x
+
x*x*x;
return
f;
}
float
f3(float
x)
{
float
f;
f
=
x
/
(1
+
x*x);
return
f;
}
float
integral(float
(*fun)(float),
float
a,
float
b)
{
float
s,
h,
y;
int
n,
i;
s
=
(
(*fun)(a)
+
(*fun)(b)
)
/2.0;
n
=
100;
h
=
(b-a)/n;
for(i=1;
i<n;
i++)
s
=
s
+
(*fun)(a+i*h);
y
=
s
*
h;
return
y;
}
void
main()
{
float
y1,
y2,
y3;
y1
=
integral(f1,
0.0,
1.0);
y2
=
integral(f2,
0.0,
2.0);
y3
=
integral(f3,
0.0,
3.5);
printf("y1=%6.2fny2=%6.2fny3=%6.2fn",
y1,y2,y3);