#include <stdio.h>
#include <stdlib.h>
// 関数本体
void func_case_01
(void) { printf("case 1:\n"); } void func_case_02
(void) { printf("case 2:\n"); } void func_case_07
(void) { printf("case 7:\n"); } void func_case_08
(void) { printf("case 8:\n"); } void func_case_09
(void) { printf("case 9:\n"); } void func_case_11
(void) { printf("case 11:\n"); } void func_case_14
(void) { printf("case 14:\n"); } void func_case_17
(void) { printf("case 17:\n"); }
// 関数ポインタ配列
typedef void (*FUNCPTR) (void);
struct FUNC_CASE_ARRAY {
int cs; // case の定数
FUNCPTR p_func; // 呼ぶべき関数
} fc_array[] = {
{17, func_case_17},
{ 7, func_case_07},
{ 2, func_case_02},
{11, func_case_11},
{ 8, func_case_08},
{ 1, func_case_01},
{14, func_case_14},
{ 9, func_case_09},
};
#define n_array (sizeof(fc_array)/sizeof(fc_array[0])) // 個数
// struct FUNC_CASE_ARRAY の cs 比較関数 (for qsort, bsearch)
int compfunc(const void *m1, const void *m2)
{
struct FUNC_CASE_ARRAY *mi1 = (struct FUNC_CASE_ARRAY *) m1;
struct FUNC_CASE_ARRAY *mi2 = (struct FUNC_CASE_ARRAY *) m2;
return mi1->cs - mi2->cs;
}
// case の定数 n で該当の関数をコール (switch)
void call_func(int n)
{
struct FUNC_CASE_ARRAY key, *res;
key.cs = n;
res
= bsearch(&key
, fc_array
, n_array
, sizeof(struct FUNC_CASE_ARRAY
), compfunc
); if (!res) {
} else {
res->p_func();
}
}
//
int main()
{
int i;
qsort(fc_array
, n_array
, sizeof(struct FUNC_CASE_ARRAY
), compfunc
); for (i = 0; i <= fc_array[n_array - 1].cs; i++) {
call_func(i);
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCi8vIOmWouaVsOacrOS9kwp2b2lkIGZ1bmNfY2FzZV8wMSh2b2lkKSB7IHByaW50ZigiY2FzZSAgMTpcbiIpOyB9CnZvaWQgZnVuY19jYXNlXzAyKHZvaWQpIHsgcHJpbnRmKCJjYXN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