typedef使用大全(指向函数的指针)
2008-06-24 22:39
423 查看
#include <stdio.h>
typedef int (* FP_CALC)(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; }
FP_CALC calc_Func(char op)
{
switch (op)
{
case '+': return add;
case '-': return sub;
case '*': return mul;
case '/': return div;
default: return NULL;
}
return NULL;
}//定义一个返回值为FP_CALC的函数。
int calc(int a, int b, char op)
{
extern FP_CALC calc_Func(char op);
if(calc_Func(op))return (calc_Func(op))(a,b);
else return -1;
}
int main(void)
{
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, '/'));
return 0;
}
//运行结果
calc(100, 20, +) = 120
calc(100, 20, -) = 80
calc(100, 20, *) = 2000
calc(100, 20, /) = 5
typedef int (* FP_CALC)(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; }
FP_CALC calc_Func(char op)
{
switch (op)
{
case '+': return add;
case '-': return sub;
case '*': return mul;
case '/': return div;
default: return NULL;
}
return NULL;
}//定义一个返回值为FP_CALC的函数。
int calc(int a, int b, char op)
{
extern FP_CALC calc_Func(char op);
if(calc_Func(op))return (calc_Func(op))(a,b);
else return -1;
}
int main(void)
{
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, '/'));
return 0;
}
//运行结果
calc(100, 20, +) = 120
calc(100, 20, -) = 80
calc(100, 20, *) = 2000
calc(100, 20, /) = 5
相关文章推荐
- typedef使用大全(指向函数的指针)
- typedef使用大全3(指向函数的指针)
- typedef使用大全(指向函数的指针)
- typedef使用大全3(指向函数的指针)
- typedef使用大全(指向函数的指针)
- typedef使用大全3(指向函数的指针)//typedef的使用可以隐藏复杂的函数指针语法
- typedef使用大全(指向函数的指针)
- typedef使用大全3(指向函数的指针)
- typedef使用(指向函数的指针)
- typedef的使用之指向函数的指针
- 使用typedef定义指向函数的指针
- 使用typedef定义指向函数的指针
- C++学习14:使用typedef定义函数指针类型
- 指向函数的指针的使用
- static函数 函数调用缺少参数列表;请使用“&XXX”创建指向成员的指针;类的static成员函数
- C++里,在类中使用指向类成员函数的指针
- typedef 和指向函数的指针
- typedef函数指针使用方法
- 由signal谈起--typedef 指向函数的指针
- 使用指向函数的指针