- #include <stdio.h>
- typedef int (*fun_t)(int , int);
- /*构造一个枚举值对应的操作符标示(ID)*/
- enum{
- OPER_ADD = 0,
- OPER_SUB,
- OPER_MUL,
- OPER_DIV,
- OPER_NUM //操作符个数
- };
- int add(int a,int b)
- {
- return (a + b);
- }
- int sub(int a,int b)
- {
- return (a - b);
- }
- int mul(int a,int b)
- {
- return (a * b);
- }
- int div(int a,int b)
- {
- return (int)(a / b);
- }
- static const fun_t oper_table[OPER_NUM] = {
- add,
- sub,
- mul,
- div
- };
- int main(int argc ,char **argv)
- {
- int a , b , result;
- a = 100;
- b = 20;
- /* use table operation : Add */
- result = oper_table[OPER_ADD](a,b);
- printf("Table operation : %d + %d = %d\n" , a , b , result);
- /* use table operation : Sub */
- result = oper_table[OPER_SUB](a,b);
- printf("Table operation : %d + %d = %d\n" , a , b , result);
- /* use table operation : Multiply */
- result = oper_table[OPER_MUL](a,b);
- printf("Table operation : %d + %d = %d\n" , a , b , result);
- /* use table operation : Divide */
- result = oper_table[OPER_DIV](a,b);
- printf("Table operation : %d + %d = %d\n" , a , b , result);
- return 0;
- }