在C中使用递归函数时避免使用全局变量
下面的代码使用了一个名为interp的递归函数,但我无法找到一种方法来避免对iter和fxInterpolated使用全局变量。完整的代码列表(执行N维线性插值)直接编译为:在C中使用递归函数时避免使用全局变量,c,recursion,C,Recursion,下面的代码使用了一个名为interp的递归函数,但我无法找到一种方法来避免对iter和fxInterpolated使用全局变量。完整的代码列表(执行N维线性插值)直接编译为: gcc NDimensionalInterpolation.c -o NDimensionalInterpolation -Wall -lm 给出的示例的输出为2.05。代码运行良好,但我想为全局变量找到替代方案。在此方面的任何帮助都将不胜感激。谢谢 #include <stdio.h> #include &
gcc NDimensionalInterpolation.c -o NDimensionalInterpolation -Wall -lm
给出的示例的输出为2.05。代码运行良好,但我想为全局变量找到替代方案。在此方面的任何帮助都将不胜感激。谢谢
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int linearInterpolation(double *, double **, double *, int);
double ** allocateDoubleMatrix(int, int);
double * allocateDoubleVector(int);
void interp(int, int, double *, double *, double *);
double mult(int, double, double *, double *);
/* The objectionable global
variables that I want to get rid of! */
int iter=0;
double fxInterpolated=0;
int main(int argc, char *argv[]){
double *fx, **a, *x;
int dims=2;
x=allocateDoubleVector(dims);
a=allocateDoubleMatrix(dims,2);
fx=allocateDoubleVector(dims*2);
x[0]=0.25;
x[1]=0.4;
a[0][0]=0;
a[0][1]=1;
a[1][0]=0;
a[1][1]=1;
fx[0]=1;
fx[1]=3;
fx[2]=2;
fx[3]=4;
linearInterpolation(fx, a, x, dims);
printf("%f\n",fxInterpolated);
return (EXIT_SUCCESS);
}
int linearInterpolation(double *fx, double **a, double *x, int dims){
double *b, *pos;
int i;
b=allocateDoubleVector(dims);
pos=allocateDoubleVector(dims);
for (i=0; i<dims;i++)
b[i] = (x[i] - a[i][0]) / (a[i][1] - a[i][0]);
interp(0,dims,pos,fx,b);
return (EXIT_SUCCESS);
}
void interp(int j, int dims, double *pos, double *fx, double *b) {
int i;
if (j == dims){
fxInterpolated+=mult(dims,fx[iter],pos,b);
iter++;
return;
}
for (i = 0; i < 2; i++){
pos[j]=(double)i;
interp(j+1,dims,pos,fx,b);
}
}
double mult(int dims, double fx, double *pos, double *b){
int i;
double val=1.0;
for (i = 0; i < dims; i++){
val *= fabs(1.0-pos[i]-b[i]);
}
val *= fx;
printf("mult val= %f fx=%f\n",val, fx);
return val;
}
double ** allocateDoubleMatrix(int i, int j){
int k;
double ** matrix;
matrix = (double **) calloc(i, sizeof(double *));
for (k=0; k< i; k++)matrix[k] = allocateDoubleVector(j);
return matrix;
}
double * allocateDoubleVector(int i){
double *vector;
vector = (double *) calloc(i,sizeof(double));
return vector;
}
#包括
#包括
#包括
整数线性插值(双*,双**,双*,整数);
双**分配子矩阵(int,int);
双*AllocateDupleVector(int);
void interp(int,int,double*,double*,double*);
双mult(整数,双精度,双精度*,双精度*);
/*令人反感的全球
我想去掉的变量*/
int-iter=0;
双插值=0;
int main(int argc,char*argv[]){
双倍*fx,**a,*x;
int-dims=2;
x=分配的副采集器(dims);
a=分配的双矩阵(dims,2);
fx=分配的副采集器(dims*2);
x[0]=0.25;
x[1]=0.4;
a[0][0]=0;
a[0][1]=1;
a[1][0]=0;
a[1][1]=1;
fx[0]=1;
fx[1]=3;
fx[2]=2;
fx[3]=4;
线性插值(fx、a、x、dims);
printf(“%f\n”,内插);
返回(退出成功);
}
整数线性插值(双*fx、双*a、双*x、整数dims){
双*b,*pos;
int i;
b=分配的副采集器(dims);
pos=分配的副采集器(dims);
对于(i=0;i),在看待这个问题时,我会考虑两种方法:
将状态保留在参数中
可以使用传递给函数的一个或多个变量(如有必要,作为指针)在函数调用中保持状态
比如说,
int global = 0;
int recursive(int argument) {
// ... recursive stuff
return recursive(new_argument);
}
可能成为
int recursive(int argument, int *global) {
// ... recursive stuff
return recursive(new_argument, global);
}
有时甚至
int recursive(int argument, int global) {
// ... recursive stuff
return recursive(new_argument, global);
}
使用静态变量
您还可以使用static
关键字声明函数中的变量,以便在函数调用之间保留该变量:
int recursive(int argument) {
static int global = 0;
// ... recursive stuff
return recursive(argument);
}
请注意,由于static
关键字,只有在程序启动时才设置global=0
,而不是每次调用函数时都设置,因为没有关键字时设置。这意味着,如果更改global
的值,它将在下次调用函数时保留该值
如果您在程序中只使用一次递归函数,则可以使用此方法;如果您需要多次使用递归函数,我建议您使用上述替代方法。解决方案是使用静态,然后在第一次调用时通过我称为initialise的标志重置变量。这样,您可以选择使用variab不管重置与否
double interp(int j, int dims, double *pos, double *fx, double *b, int initialise) {
static double fxInterpolated = 0.0;
static int iter = 0;
int i;
if (initialise){
fxInterpolated = 0.0;
iter = 0;
}
.....
......
}
为什么这两个变量不能成为参数的一部分?不要将void*
强制转换为指针!请参阅代码中的calloc()
调用。还要注意,在函数中使用任何类型的全局(文件范围或static
函数)都无法重入,因此不能从多个线程并行使用同一函数。。
double interp(int j, int dims, double *pos, double *fx, double *b, int initialise) {
static double fxInterpolated = 0.0;
static int iter = 0;
int i;
if (initialise){
fxInterpolated = 0.0;
iter = 0;
}
.....
......
}