c语言map函数指针,函数指针

c语言map函数指针,函数指针

/*Author:Choas Lee

*Date:2012-02-28

*/

#include

#include

#include

float add(float a,float b){return a+b;}

float minus(float a,float b){return a-b;}

float multiply(float a,float b){return a*b;}

float divide(float a,float b){return a/b;}

//该函数的返回值是一个函数

float(* FunctionMap(char op) )(float,float)

{

switch(op)

{

case '+':

return add;

break;

case '-':

return minus;

break;

case '*':

return multiply;

break;

case '\\':

return divide;

break;

default:

exit(1);

}

}

int main()

{

float a=10,b=5;

char ops[]={'+','-','*','\\'};

int len=strlen(ops);

int i=0;

float (*returned_function_pointer)(float,float);//定义了一个函数指针

for(i=0;i

{

returned_function_pointer=FunctionMap(ops[i]);

printf("the result caculated by the operator %c is %f\n",ops[i],returned_function_pointer(a,b));

}

return 0;

}

输出:

the result caculated by the operator + is 15.000000

the result caculated by the operator - is 5.000000

the result caculated by the operator * is 50.000000

the result caculated by the operator \ is 2.000000

参考:

1.

发布者:admin,转转请注明出处:http://www.yc00.com/news/1706467836a1454685.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信