标签:
/*Author:Choas Lee *Date:2012-02-28 */ #include<stdio.h> #include<stdlib.h> #include<string.h> 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<len;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.http://hipercomer.blog.51cto.com/4415661/792301
标签:
原文地址:http://my.oschina.net/itfanr/blog/361125