9.5求积分

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
float fun1(float x);
float fun2(float x);
float Integral(float (*f)(float),float a,float b);
int main()
{
 float y1,y2;
 y1 = Integral(fun1,0.0,1.0);
 y2 = Integral(fun2,0.0,3.0);
 printf_s("y1 = %f\n y2 = %f\n",y1,y2);
 system("pause");
 return 0;
}
float fun1(float x)
{
 return 1 + x * x;
}
float fun2(float x)
{
 return x/(1 + x * x);
}
float Integral(float (*f)(float),float a,float b)
{
 float s,h;
 int n = 100,i;
 s = ((*f)(a) + (*f)(b)) / 2 ;
 h = (b - a )/n;
 for(i = 1;i < n;i++)
 {
  s += (*f)(a + i * h);
 }
 return s * h;
}
posted @ 2015-04-13 16:15  码农@163  阅读(141)  评论(0编辑  收藏  举报