下面的代码使用一个名为interp的递归函数,但我找不到避免使用iter和fxInterpolated的全局变量的方法.完整的代码清单(执行N维线性插值)直接编译:
gcc NDimensionalInterpolation.c -o NDimensionalInterpolation -Wall -lm
Run Code Online (Sandbox Code Playgroud)
给出的示例的输出是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;
}
Run Code Online (Sandbox Code Playgroud)
感谢您的评论到目前为止.我想避免使用静态.我删除了全局变量,并且建议尝试使用iter变量进行解析.但没有快乐.另外我得到一个编译警告:"不使用值计算"参考*iter ++; 我究竟做错了什么?
void interp(int j, int dims, double *pos, double *fx, double *b, int *iter) {
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,iter);
}
Run Code Online (Sandbox Code Playgroud)
}
在查看此问题时,我会考虑两种方法:
将状态保存在参数中
您可以使用一个或多个传递给函数的变量(如果需要,作为指针)以保持函数调用之间的状态.
例如,
int global = 0;
int recursive(int argument) {
// ... recursive stuff
return recursive(new_argument);
}
Run Code Online (Sandbox Code Playgroud)
可能成为
int recursive(int argument, int *global) {
// ... recursive stuff
return recursive(new_argument, global);
}
Run Code Online (Sandbox Code Playgroud)
有时甚至是
int recursive(int argument, int global) {
// ... recursive stuff
return recursive(new_argument, global);
}
Run Code Online (Sandbox Code Playgroud)
使用静态变量
您还可以使用static
关键字在函数调用中声明要保留的函数中的变量:
int recursive(int argument) {
static int global = 0;
// ... recursive stuff
return recursive(argument);
}
Run Code Online (Sandbox Code Playgroud)
请注意,由于static
关键字,global = 0
仅在程序启动时设置,而不是每次调用函数时都设置,因为没有关键字.这意味着如果改变值global
,它将在下次调用函数时保留该值.
如果在程序中只使用一次递归函数,则可以使用此方法; 如果你需要多次使用它,我建议你使用上面的替代方法.