升序做得很好。但是当我输入一个不同的数字时,它应该调用降序,我认为这是很好的定义。然而我有一个随机排序..
#include<stdio.h>
void sort(int b[], int size, int (*upordown)(int a, int b));
void swap(int *elt1, int *elt2);
int ascending(int a, int b);
int descending(int a, int b);
main()
{
int size =8;
int order,a[size],i;
fprintf(stdout, "please enter a sequence of numbers you wish to sort:\n");
for(i =0; i<size; i++)
{
scanf("%d", &a[i]);
}
printf("\n please enter 1 for ascending order or 2 for descending order");
scanf("%d", &order);
if(order == 1)
{
sort(a, size, ascending);
}
else
{
sort(a, size, descending);
}
printf("Here is the new re arranged array: \n");
for(i =0; i<size; i++)
{
printf("%d\t", a[i]);
}
printf("\n");
return 0;
}
void sort(int b[], int size, int (*upordown)(int a, int b))
{
int pass, j;
for(pass =0; pass< size;pass++)
{
for(j=0; j<size; j++)
{
if((*upordown)(b[j], b[j+1]))
{
swap(&b[j], &b[j+1]);
}
}
}
}
void swap(int *elt1, int *elt2)
{
int hold;
if(*elt1 > *elt2)
{
hold = *elt1;
*elt1 = *elt2;
*elt2 = hold;
}
}
int ascending(int a, int b)
{
return b<a;
}
int descending(int a, int b)
{
return b>a;
}