指向函数的指针的简单问题

986532 2002-05-17 01:36:23

//***************************//
// //
// 《C程序设计教程》 //
// //
// H.M.Deitel //
// P.J.Deitel 著 //
// 薛万鹏 等译 //
// //
// 机械电子出版社 //
// //
//***************************//

第227页的例子不能正常在VC6.0下运行,不知为什么?原代码如下:


//***********************************************
//227_pointerf1.cpp
//指向函数的指针
#include<stdio.h>
#define SIZE 10

void bubble(int*,const int,int(*)(int,int));
int ascending(const int,const int);
int descending(const int,const int);

main()
{
int a[SIZE]={2,6,4,8,10,12,89,68,45,37};
int counter,order;

printf("Enter 1 to sort in ascending order,\n");
printf("Enter 2 to sort in descending order:");
scanf("%d",&order);

printf("\nData items in original order\n");

for(counter=0;counter<=SIZE-1;counter++)
printf("%4d",a[counter]);

if(order==1){
bubble(a,SIZE,ascending);
printf("\nData items in ascending order\n");
}
else{
bubble(a,SIZE,descending);
printf("\nData items in descending order\n");
}

for(counter=0;counter<=SIZE-1;counter++)
printf("%4d",a[counter]);

printf("\n");

return 0;
}

void buble(int* work,const int size,int(*compare)(int,int))
{
int pass,count;
void swap(int*,int*);

for(pass=1;pass<=size-1;pass++)
for(count=0;count<=size-2;count++)
if((*compare)(work[count],work[count+1]))
swap(&work[count],&work[count+1]);
}

void swap(int*element1Ptr,int*element2Ptr)
{
int temp;

temp=*element1Ptr;
*element1Ptr=*element2Ptr;
*element2Ptr=temp;
}

int ascending(const int a,const int b)
{
return b<a;
}

int descending(const int a,const int b)
{
return b>a;
}
//***********************************************
...全文
23 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
986532 2002-05-17
  • 打赏
  • 举报
回复
送分了
swingerleung 2002-05-17
  • 打赏
  • 举报
回复
有两个BUG
第一行:
原文:void bubble(int*,const int,int(*)(int,int));
改为:void bubble(int*,const int,int(*)(const int,const int));

函数声明:
void buble(int* work,const int size,int(*compare)(int,int))
改为:
void bubble(int* work,const int size,int(*compare)(int,int))

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧