Problem1898--【高级语言程序设计】 9.7 通用的排序函数

1898: 【高级语言程序设计】 9.7 通用的排序函数

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1  Solved: 1
[Submit] [Status] [Web Board] [Creator:]

Description

使用函数指针做函数参数,编写一个通用的排序函数,重写第 8 章 例8.6 的代码,使其既能对卫星记录数据按载重量进行升序排序,也能对卫星记录数据按载重量进行降序排序。

Input

先输入 n 个数据元素,格式为:序号 元素,以-1 -1结束输入。

Output

先输出总共输入几个数据,即 n ;再输出排序后的元素(两种方式),按元素的从小到大从大到小顺序输出。格式为:序号 元素。 其中序号和格式都占4位宽度:%4d

Sample Input Copy

1 90
3 70
2 80
4 60
-1 -1

Sample Output Copy

Total satellites are 4
Sorted results in ascending order:
   4  60
   3  70
   2  80
   1  90
Sorted results in descending order:
   1  90
   2  80
   3  70
   4  60

HINT

#include <stdio.h>
#define N 40
int ReadRecord(int num[], int weight[]);
void ExchangeSort(int num[], int weight[], int n, int (*compare)(int,int));
int Ascending(int a, int b);
int Descending(int a, int b);
void PrintRecord(int num[], int weight[], int n);
void  Swap(int *x, int *y);
//主函数
int main(void)
{
    //=======begin=======




    //========end========
}
//函数功能:输入卫星的编号及其载重量,当输入负值时,结束输入,返回卫星总数
int ReadRecord(int num[], int weight[])
{
    //=======begin=======





    //========end========
}
//函数功能:按交换法,对卫星记录数据按载重量进行升序排序或降序排序
void ExchangeSort(int num[], int weight[], int n, int (*compare)(int,int))
{
    //=======begin=======





    //========end========
}
//函数功能:返回值为真则升序
int Ascending(int a, int b)
{
    //=======begin=======

    //========end========
}
//函数功能:返回值为真则降序
int Descending(int a, int b)
{
    //=======begin=======


    //========end========
}
//函数功能:打印所有卫星记录数据
void PrintRecord(int num[], int weight[], int n)
{
    //=======begin=======


    //========end========
}
//函数功能:交换两个数x和y
void  Swap(int *x, int *y)
{
    //=======begin=======




    //========end========
}

Source/Category