排序(函数指针)
SelectionSort()函数既可升序也可降序,
主要由于其形参int(*compare)(int a,int b),
compare是一个指针变量,可指向一个有两个整型实参,返回值为整数的函数。
compare就是一个指针函数,
*compare两侧括号是必须的 它将 * 和 compare 先结合表示一个指针变量
然后与后面括号结合表示该指针变量可以指向一个函数
int *compare(int a,int b)
这个不是一个指针函数,它表示一个两个整型形参并返回整型指针的函数
SelectionSort()用函数指针作为形参,该函数指针既可指向函数Ascending(),也可指向函数Descending()
#include <stdio.h>
#define N 40
int ReadScore(int score[]);
void PrintScore(int score[],int n);
void SelectionSort(int a[],int n,int (*compare)(int a,int b));
int Ascending(int a,int b);
int Descending(int a,int b);
void Swap(int *x,int *y);
int main()
{
int score[N],n;
int order;
n = ReadScore(score);
printf("Total students are %d\n",n);
printf("Enter 1 to sort in ascending order,\n");
printf("Enter 2 to sort in descending order:");
scanf("%d",&order);
printf("Data items in original order\n");
PrintScore(score,n);
if(order==1)
{
SelectionSort(score,n,Ascending);
printf("Data items in ascending order\n");
}
else
{
SelectionSort(score,n,Descending);
printf("Data items in descending order\n");
}
PrintScore(score,n);
return 0;
}
int ReadScore(int score[])
{
int i=-1;
do{
i++;
printf("Input score:");
scanf("%d",&score[i]);
}while(score[i]>=0);
return i;
}
void PrintScore(int score[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%4d",score[i]);
}
printf("\n");
}
void SelectionSort(int a[],int n,int (*compare)(int a,int b))
{
int i,j,k;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
{
if((*compare)(a[j],a[k]))
{
k=j;
}
}
if(k!=i)
{
Swap(&a[k],&a[i]);
}
}
}
int Ascending(int a,int b)
{
return a<b;
}
int Descending(int a,int b)
{
return a>b;
}
void Swap(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}