#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) {
        printf(" -\n");
    } 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++) {
        printf("[case %2d:] ", i);
        call_func(i);
    }

    return 0;
}
