2011-05-13 69 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