|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
第七课练习题:
设一个函数process,在调用它的时候 每次实现不同功能,(类似多态),输入A和B,第一次找最大数,第二次找最小数,第三次求两个数的和。要求自己思考process函数写法;
我的代码如下:请牛兄指点更厉害的招数
int max(int, int);
int min(int, int);
int add(int, int);
int main()
{
void process(int x, int y, int(*function)());
int a, b;
printf("input two numbers:\n");
scanf_s("%d,%d", &a, &b);
printf("max=");
process(a, b, max);
printf("min=");
process(a, b, min);
printf("add=");
process(a, b, add);
}
void process(int x,int y,int(*function)())
{
int fruit,(*a)();
a = function;
if (a == max)
{
fruit=max(x, y);
printf("%d\n", fruit);
}
else if (a == min)
{
fruit=min(x, y);
printf("%d\n", fruit);
}
else if (a == add)
{
fruit=add(x, y);
printf("%d\n", fruit);
}
}
int max(int x, int y)
{
int z;
z = x > y ? x : y;
return z;
}
int min(int x, int y)
{
int z;
z = x < y ? x : y;
return z;
}
int add(int x, int y)
{
int z;
z = x+y;
return z;
} |
|