读书人

一道题目给了小弟我疑惑

发布时间: 2012-03-09 16:54:56 作者: rapoo

一道题目给了我疑惑
题目: (在线等...)
设计一个函数process,调用它时,每次实现不同的功能。(类似多态)
输入one和two两个数,第一个调用process时找出one和two的最大值,第二次找出最小值,第三次求和.

以下是我写的代码,process设计不出来...而且这个寻找函数首址的寻址方式有点头疼,求解释一下

C/C++ code
#include <stdio.h>void main(){    int Max(int one, int two);    int Min(int one, int two);    int Add(int one, int two);    void process(int one, int two, int(*fan)());    int one, two;    scanf("%d%d", &one, &two);    printf("Max = ");    process(one, two, Max);    printf("\nMin = ");    process(one, two, Min);    printf("\nAdd = ");    process(one, two, Add);    printf("\n");}int Max(int one, int two){    int sum;    if (one < two)    {        sum = two;    }    else    {        sum = one;    }    return sum;}int Min(int one, int two){    int sum;    if (one < two)    {        sum = one;    }    else    {        sum = two;    }    return sum;}int Add(int one, int two){    int sum;    sum = one + two;        return sum;}void process(int one, int two, int(*fan)()){    int p1, p2, p3;    p1 = (*fan)(one, two);    p2 = (*fan)(one, two);    p3 = (*fan)(one, two);}


[解决办法]
C/C++ code
#include <stdio.h>void main(){    int Max(int one, int two);    int Min(int one, int two);    int Add(int one, int two);    int process(int one, int two, int(*fan)() );    int one, two;    scanf("%d%d", &one, &two);    printf("Max = %d", process(one, two, Max) );    printf("\nMin = %d", process(one, two, Min) );    printf("\nAdd = %d", process(one, two, Add) );    printf("\n");}int Max(int one, int two){    int sum;    if (one < two)    {        sum = two;    }    else    {        sum = one;    }    return sum;}int Min(int one, int two){    int sum;    if (one < two)    {        sum = one;    }    else    {        sum = two;    }    return sum;}int Add(int one, int two){    int sum;    sum = one + two;        return sum;}int process(int one, int two, int(*fan)()){    return (*fan)(one, two);} 

读书人网 >C语言

热点推荐