这个程序为什么输入两个数字后得不到最大值和两之和....例如输入 9,7 结果就是max=12803,min=9,sum=12812
为什么啊?想不明白........
int process(int (*p)(int,int),int x,int y)
{return (*p)(x,y);
}
int max(int x,int y)
{return x>y?x:y;
}
int min(int x,int y)
{return x<y?x:y;
}
int sum(int x,int y)
{return x+y;
}
main()
{int a,b;
int max(int,int);
int min(int,int);
int sum(int,int);
int process(int (*p)(),int,int);
scanf("%d%d",&a,&b);
printf("max=%d,min=%d,sum=%d\n",process(max,a,b),process(min,a,b),process(sum,a,b));
getch();
}
----------------解决方案--------------------------------------------------------
为什么没人给解答啊..........
----------------解决方案--------------------------------------------------------
编译都没通过。。。
不明白搞得有点复杂。。。
----------------解决方案--------------------------------------------------------
能啊!我试了!
----------------解决方案--------------------------------------------------------
It is not necessary to use the process funtion. Only the max, min and sum funtion can slove the problem.
printf("max=%d,min=%d,sum=%d\n",process(max,a,b),process(min,a,b),process(sum,a,b));
==============>
printf("max=%d,min=%d,sum=%d\n",max(a,b), min(a,b), sum(a,b));
Another point is that there is a serious in your process funtion.The return value is exactly wrong.
----------------解决方案--------------------------------------------------------
不愧是外语外贸的。。。全英文。。。偶的E文不行呀。
没看懂原因。
只知道的确没必要这么麻烦的!
----------------解决方案--------------------------------------------------------
Sorry, the process funtion is return a funtion pointer .I hadn't found that the p perameter is also a funtion pointe.
----------------解决方案--------------------------------------------------------
呵呵,还没有指针呢!
----------------解决方案--------------------------------------------------------
It is not necessary to use the process funtion. Only the max, min and sum funtion can slove the problem.
printf("max=%d,min=%d,sum=%d\n",process(max,a,b),process(min,a,b),process(sum,a,b));
==============>
printf("max=%d,min=%d,sum=%d\n",max(a,b), min(a,b), sum(a,b));
Another point is that there is a serious in your process funtion.The return value is exactly wrong.
我知道没必要用process那个函数.....我在这个题目中要求实验的是调用一个函数实现不同的功能.....对于此题当然可以不必这么麻烦....直接引用那三个函数就可以了....但是我要做的就是实验...有时候为了结构化设计....这样设个万用函数是有好处的....
还有就是我就弄不明白为什么RETURN返回的值不正确呢? 谁能实质性地帮忙解决下这个问题.....谢谢
----------------解决方案--------------------------------------------------------
你用什么编译器?
----------------解决方案--------------------------------------------------------