1 #include <stdio.h> 2 float Fun1(float x); 3 float Fun2(float x); 4 float Integral(float (*f)(float), float a, float b); 5 int main() 6 { 7 float y1, y2; 8 y1 = Integral(Fun1, 0.0, 1.0); 9 y2 = Integral(Fun2, 0.0, 3.0); 10 printf("y1=%f y2=%f ", y1, y2); 11 return 0; 12 } 13 /* 函数功能:计算函数1+x*x的函数值 */ 14 float Fun1(float x) 15 { 16 return 1 + x * x; 17 } 18 /* 函数功能:计算函数x/(1+x*x)的函数值 */ 19 float Fun2(float x) 20 { 21 return x / (1 + x * x); 22 } 23 /* 函数功能:用梯形法计算函数的定积分 */ 24 float Integral(float (*f)(float), float a, float b) 25 { 26 float s, h; 27 int n = 100, i; 28 s = ((*f)(a) + (*f)(b)) / 2; 29 h = (b - a) / n; 30 for (i = 1; i < n; i++) 31 { 32 s += (*f)(a + i * h); 33 } 34 return s * h; 35 }