for(pass=1;pass<size;pass++)
for(count=0;count<size-pass;count++)
{
if((*compare)(work[count],work[count+1]))
swap(&work[count],&work[count+1]);
}
}
int main()
{
int order;
int counter;
int a[SIZE]={2,6,4,8,10,12,89,68,45,37};
printf("\nData items in original order:\n");
for(counter=0;counter<SIZE;counter++)
printf("%5d",a[counter]);
printf("\nEnter 1 to sort in accending order,\n"
"Enter 2 to sort in descending order: ");
scanf("%d",&order);
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;counter++)
printf("%5d",a[counter]);
printf("\n");
return 0;
}
(2) 函数指针数组
[cpp]
#include<stdio.h>
void function1(int a);
void function2(int a);
void function3(int a);
int main()
{
void (*f )(int)={function1,function2,function3};
int choice;
printf("Enter a number between 0 and 2, 3 to end: ");
scanf("%d",&choice);
while(choice >=0 && choice<3)
{
(*f[choice])(choice);
printf("Enter a number between 0 and 2, 3 to end: ");
scanf("%d",&choice);
}
printf("Program execution completed.\n");
return 0;
}
void function1(int a)
{
printf("You entered %d so funtion1 was called\n\n",a);
}
void function2(int a)
{
printf("You entered %d so funtion2 was called\n\n",a);
}
void function3(int a)
{
printf("You entered %d so funtion3 was called\n\n",a);
}