c++ 函数指针做形参

求y=sqrt(x)的定积分
 
 
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
 
double fun(double x)
{
double y=sqrt(x);
return y;
}
double ans(double a,double b,double(*pt)(double x))
{
int i,n;
double step,sum=0;
n=(b/a)*10000;
step=(float)((b-a)/n);
for(i=1;i<=n;i++)
{
sum+=step*pt(a);
a+=step;
}
return sum;
}
 
 
int _tmain(int argc, _TCHAR* argv[])
{
double a,b,z;
cin>>a>>b;
z=ans(a,b,fun);
cout<<z<<endl;
return 0;
}
 
原文地址:https://www.cnblogs.com/humanchan/p/3020863.html