1
我們在一個家庭作業程序中得到了這個typedef。作爲程序員noob,我以前沒有看到過這樣的事情。這是否意味着任何DoubleFunction2D實際上是(double,double)的2元組?帶元組的typedef如何工作?
計劃:
的typedef:
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);
}
typedef-ed指向函數的指針 – LPs