我们在一个家庭作业程序中得到了这个 typedef。作为一个程序员菜鸟,我以前没有见过这样的东西。这是否意味着任何 DoubleFunction2D 实际上是 (double, double) 的 2 元组?
程序:
类型定义:
typedef double (*DoubleFunction) (double);
typedef double (*DoubleFunction2D) (double, double);
typedef double (*DoubleFunction3D) (double, double, double);
示例用法
(我的 WIP 任务解决方案,尚未编译/测试。内部):
double expf2D(double x, double y)
{
double r = sqrt(pow(x,2) + pow(y,2));
return my_expf(r);
}
double DiskMonteCarloIntegrator(DoubleFunction2D f, double r1, double r2, int N)
{
bool is_inside_ring(double x, double y){
return is_inside_ellipse(x, y, r2/2, r2/2) && !(is_inside_ellipse(x, y, r1/2, r1/2));
}
int i=0;
double x, y, sum = 0;
while(i < N)
{
x = RandomDouble(-1*r1, r1);
y = RandomDouble(-1*r1, r1);
if(is_inside_ring(x, y))
{
sum += f(x, y);
i++;
}
}
double avg = sum / N;
double integral = avg * (pow(r2, 2) - pow(r1, 2)) * M_PI;
return integral;
}
//extract
void main(int argc, char *argv[]){
DiskMonteCarloIntegrator(expf2D, 1.0, 2.0, 1000000);
}