1. #include <stdio.h> 
  2. typedef int (*fun_t)(int , int); 
  3.  
  4. /*构造一个枚举值对应的操作符标示(ID)*/  
  5. enum
  6.  
  7.  
  8.     OPER_ADD = 0, 
  9.     OPER_SUB, 
  10.     OPER_MUL, 
  11.     OPER_DIV, 
  12.     OPER_NUM            //操作符个数  
  13. }; 
  14. int add(int a,int b) 
  15. {    
  16.     return (a + b); 
  17.  
  18. int sub(int a,int b) 
  19.     return (a - b); 
  20.  
  21. int mul(int a,int b) 
  22.     return (a * b); 
  23.  
  24. int div(int a,int b) 
  25.     return (int)(a / b); 
  26.  
  27. static const fun_t oper_table[OPER_NUM] = { 
  28.     add, 
  29.     sub, 
  30.     mul, 
  31.     div 
  32. }; 
  33.  
  34. int main(int argc ,char **argv) 
  35. {    
  36.     int a , b , result; 
  37.     a = 100; 
  38.     b = 20; 
  39.      
  40.     /* use table operation : Add */ 
  41.     result = oper_table[OPER_ADD](a,b); 
  42.     printf("Table operation : %d + %d = %d\n" , a , b , result); 
  43.      
  44.     /* use table operation : Sub */ 
  45.     result = oper_table[OPER_SUB](a,b); 
  46.     printf("Table operation : %d + %d = %d\n" , a , b , result); 
  47.      
  48.     /* use table operation : Multiply */ 
  49.     result = oper_table[OPER_MUL](a,b); 
  50.     printf("Table operation : %d + %d = %d\n" , a , b , result); 
  51.      
  52.     /* use table operation : Divide */ 
  53.     result = oper_table[OPER_DIV](a,b); 
  54.     printf("Table operation : %d + %d = %d\n" , a , b , result); 
  55.      
  56.     return 0;