首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在C中使用递归函数时避免使用全局变量

在C中使用递归函数时避免使用全局变量
EN

Stack Overflow用户
提问于 2015-05-28 11:03:17
回答 2查看 6K关注 0票数 5

下面的代码使用了一个名为interp的递归函数,但我无法找到一种方法来避免为iter和fxInterpolated使用全局变量。完整的代码列表(执行N维线性插值)直接编译如下:

代码语言:javascript
运行
复制
gcc NDimensionalInterpolation.c -o NDimensionalInterpolation -Wall -lm

给出的例子的输出为2.05。代码运行良好,但我希望找到全局变量的替代方案。如果能对此提供任何帮助,我们将不胜感激。谢谢。

代码语言:javascript
运行
复制
#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;
}

到目前为止,谢谢您的评论。我想避免使用静电。我已经删除了全局变量,并按照建议尝试使用iter变量进行解析。但是没有joy。此外,我还收到了一个编译警告:引用*iter++时“不使用计算值”;我做错了什么?

代码语言:javascript
运行
复制
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);
 }

}

EN

Stack Overflow用户

发布于 2015-05-29 20:42:04

一种解决方案是使用静力学,然后在第一次调用时重置变量,通过我称为初始化的标志。这样,您可以选择将变量重置或不重置。

代码语言:javascript
运行
复制
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;
 }

 .....
......
}
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30504755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档