问题3对应的源代码,vc6.0下面调试通过
int calc(int a, int b, char op);
FP_CALC calc_func(char op);
int add(int, int);
int sub(int, int);
int mul(int, int);
int div(int, int);
int (*s_calc_func(char op))(int, int);
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 b? a/b : -1;
}
int calc(int a, int b, char op)
{
FP_CALC fp = calc_func(op);
int (*s_fp)(int, int) = s_calc_func(op);
if (fp) return fp(a, b);
else return -1;
}
FP_CALC calc_func(char op)
{
switch (op)
{
case '+': return (FP_CALC )add;
case '-': return (FP_CALC )sub;
case '*': return (FP_CALC )mul;
case '/': return (FP_CALC )div;
default:
return NULL;
}
return NULL;
}
int (*s_calc_func(char op)) (int, int)
{
return calc_func(op);
}
void main()
{
int a = 100, b = 20;
printf("calc(%d, %d, %c) = %d\n", a, b, '+', calc(a, b, '+'));
printf("calc(%d, %d, %c) = %d\n", a, b, '-', calc(a, b, '-'));
printf("calc(%d, %d, %c) = %d\n", a, b, '*', calc(a, b, '*'));
printf("calc(%d, %d, %c) = %d\n", a, b, '/', calc(a, b, '/'));
} |