2011-05-13 72 views
1

嘿, 假设我有以下功能:'动态'在C中使用变量?

#define SIZE 100 
double a[SIZE]; 
double b[SIZE]; 

double a_function(int index1, int index2) 
{ 
    switch(index1) 
    { 
     case 1: 
      return a[1]*a[2]*a[5]*a[3]; 
     case 2: 
      return a[6]*cos(a[77])*exp(a[95]); 
     // .............. MUCH MORE CASES 
     // ALWAYS USING ONLY VALUES OF ARRAY a 
     case 100: 
      return exp(a[20])*sin(a[21]); 
    } 
} 

我要实现以下目标:index2比介于0和SIZE-1,我想在任何情况下使用b[ index2 ]没有“取代”每a[ index2 ]改变switch/case-construct。此外,a和b不能修改,所以它们是只读的!

在这个简单的例子:
a_function(2, index2)为索引2 = {6,77,95} - >返回a[6]*cos(a[77])*exp(a[95]);

a_function(2, 6) - >返回b[6]*cos(a[77])*exp(a[95]);

如何做到这一点任何想法? 也许有一些帮助功能或使用'模板'? 非常感谢!

回答

4

我认为你必须做一个宏这样做

#define X(n) ((index2==n)?(b[n]):(a[n])) 

double a_function(int index1, int index2) 
{ 
    switch(index1) 
    { 
     case 1: 
      return X(1)*X(2)*X(5)*X(3); 
     case 2: 
      return X(6)*cos(X(77))*exp(X(95)); 
     ... 
     case 100: 
      return exp(X(20))*sin(X(21)); 
    } 

}

+0

啊谢谢,看起来很酷。但我只是想知道如果我经常整合这个宏,性能会如何呢? – tim 2011-05-13 21:45:05

+0

再次感谢,它现在正在使用,它的作品就像一个魅力! – tim 2011-05-14 13:45:57

0

方法如下:

#define SIZE 100 
    double a[SIZE]; 
    double b[SIZE]; 

    double a_function(int index1, int index2) 
    { 
switch(index1) 
{ 
    double *choice = (index2 != 6)?a:b;   <<< set choice 
    case 1: 
     return a[1]*a[2]*a[5]*a[3];    
    case 2: 
     return choice[6]*cos(a[77])*exp(a[95]); <<< use choice 
    // .............. MUCH MORE CASES 
    // ALWAYS USING ONLY VALUES OF ARRAY a 
    case 100: 
     return exp(a[20])*sin(a[21]); 
} 

}

+0

嘿,对不起,我认为你误解了我的一点点:6只是一个例子。 'a_function(2,95)' - >返回a [6] * cos(a [77])* exp(b [95]);' – tim 2011-05-13 21:46:30

+0

@lucas最好选择一次,而不是每次都做时间在宏观上的定义。 – NoJunkNoNoise 2011-05-13 21:46:54

+0

比请告诉我如何正确地做到这一点!? :-)你的例子是,正如我所说的,不是我要求的:( – tim 2011-05-13 22:12:43

2

听起来像是你有

{ 
    for (int j=0; j<SIZE; ++j) { 
     a_function(index1, j); 
    } 
} 

那么为什么不使用

double c[SIZE]; 

double a_function(double c[], int index1) 
{ 
    switch(index1) 
    { 
     case 1: 
      return c(1)*c(2)*c(5)*c(3); 
     ... 
    } 
} 

{ 
    double c[SIZE]; 
    for (int i=SIZE; i--;) { 
     c[i] = a[i]; 
    } 

    for (int j=0; j<SIZE; ++j) { 
     c[j] = b[j]; 
     ... = a_function(c, index1); 
     c[j] = a[j]; 
    } 
} 

PS —无论您使用的方法,您的巨型开关可能会更好,因为函数指针的查找表来实现。

typedef double (*transform_t)(double*); 

double transform1(double* c) { return c[1]*c[2]*c[5]*c[3]; } 
double transform2(double* c) { return c[6]*cos(c[77])*exp(c[95]); } 
... 
double transform100(double* c) { return exp(c[20])*sin(c[21]); } 

transform_t transforms[100] = { 
    transform1, 
    transform2, 
    ... 
    transform100, 
}; 


{ 
    double c[SIZE]; 
    for (int i=SIZE; i--;) { 
     c[i] = a[i]; 
    } 

    ... 

    for (int j=0; j<SIZE; ++j) { 
     c[j] = b[j]; 
     ... = (transforms[index1])(c); 
     c[j] = a[j]; 
    } 
} 

更新:我只注意到“A和B不能被修改,所以他们是只读的!”。调整。

更新:添加了switch语句替代方法的建议。

+0

感谢迄今,但我没有得到最后三行代码。你可以再解释一下吗?!?谢谢! – tim 2011-05-14 11:48:44

+0

@Col Heather,我暂时用'b [index2]'替换'[index2]'。然后你可以使用'a []'。你说'a []'不能修改,所以我做了一个可以修改的副本。正如我所指出的那样,这假定您使用相同的数组多次调用该函数。 – ikegami 2011-05-14 21:44:43