鱼C论坛

 找回密码
 立即注册
查看: 4100|回复: 2

大家来找茬,这两个程序那里不同了?

[复制链接]
发表于 2013-1-13 13:06:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. //process by myself
  2. #include <stdio.h>
  3. void main(int argc, char *argv[])
  4. {
  5.         int fmax(int ,int );
  6.         int fmin(int ,int );
  7.         int fadd(int ,int );
  8.         int a,b;
  9.        
  10.         void process(int a,int b ,int (*fun)());
  11.        
  12.         printf("please input a&b:");
  13.         scanf("%d %d",&a,&b);
  14.        

  15.              printf("max=");
  16.     process(a,b,fmax);

  17.         printf("\nmin=\n");
  18.         process(a,b,fmin);
  19.        
  20.                         printf("add=");
  21.         process(a,b,fadd);

  22.        
  23.        
  24.        
  25.         return 0;
  26. }

  27. int fmax(int a,int b)
  28. {
  29.         int z;
  30.         if(a>b)
  31.         z=a;
  32.         else z=b;
  33.         return(z);
  34. }

  35. int fmin(int a,int b)
  36. {
  37.         int z;
  38.         if(a<b)
  39.         z=a;
  40.         else
  41.         z=b;
  42.         return (z);
  43.        
  44. }
  45. int fadd(int a,int b)
  46. {
  47.         int z;
  48.         z=a+b;
  49.         return (z);
  50. }


  51. void process(int a,int b,int(*fun)())
  52. {
  53.          int result;

  54.       result = (*fun)(a, b);
  55.       printf("%d\n", result);
  56. }
复制代码
//wrong wrong wrong
  1. /***********************************************************/
  2. /*  设一个函数process,在调用它的时候,每次实现不同的功能。*/
  3. /*  输入a和b两个数,第一次调用process时找出a和b中大者,*/
  4. /*  第二次找出其中小者,第三次求a与b之和。               */
  5. /***********************************************************/

  6. #include <stdio.h>

  7. void main()
  8. {
  9.       int max(int, int);            /* 函数声明 */
  10.       int min(int, int);            /* 函数声明 */
  11.       int add(int, int);            /* 函数声明 */
  12.    
  13.       void process( int, int, int(*fun)() );    /* 函数声明 */
  14.       
  15.       int a, b;

  16.       printf("Endter a and b: ");
  17.       scanf("%d %d", &a, &b);
  18.       
  19.       printf("max = ");
  20.       process(a, b, max);

  21.       printf("min = ");
  22.       process(a, b, min);

  23.       printf("sum = ");
  24.       process(a, b, add);
  25. }

  26. int max(int x, int y)              /* 函数定义 */
  27. {
  28.       int z;
  29.       
  30.       if( x > y )
  31.       {
  32.             z = x;
  33.       }
  34.       else
  35.       {
  36.             z = y;
  37.       }

  38.       return z;
  39. }

  40. int min(int x, int y)              /* 函数定义 */
  41. {
  42.       int z;

  43.       if( x < y )
  44.       {
  45.             z = x;
  46.       }
  47.       else
  48.       {
  49.             z = y;
  50.       }

  51.       return z;
  52. }

  53. int add(int x, int y)
  54. {
  55.       int z;
  56.       
  57.       z = x + y;
  58.       return z;
  59. }

  60. void process( int x, int y, int(*fun)() )    /* 函数定义 */
  61. {
  62.       int result;

  63.       result = (*fun)(x, y);
  64.       printf("%d\n", result);
  65. }
复制代码
// 运行完全流畅,ok
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-1-13 19:10:47 | 显示全部楼层
看不出这两个程序有什么区别,但是我运行 一下两个都有错误,应该是把process函数改成process( int x, int y, int(*fun)(int,int) ),运行才正确。
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2013-1-14 12:35:20 | 显示全部楼层

小芳威武,多来解决问题啊
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-8-9 08:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表