声明:该编译器为vs,所以输入函数写为scanf_s形式!
代码如下:
#include <stdio.h>#define N 40int ReadScore(int score[],long num[]);//函数原型;void DataSort(int score[], int n, long num[]);//函数原型;void PrintfScore(int score[], int n, long num[]);//函数原型;int main() {int score[N], n;long num[N];n = ReadScore(score,num);printf("Total students are %d\n", n);DataSort(score, n,num);printf("Sorted scores:\n");PrintfScore(score, n,num);return 0;}//函数功能:读入该门课的成绩:int ReadScore(int score[], long num[]) {int i = -1;printf("Input student't ID and score:\n");do {i++;scanf_s("%ld %d",&num[i], &score[i]);} while (num[i]>0&&score[i] >= 0);return i;}//函数功能:将该门课的成绩按照高低排序;void DataSort(int score[], int n, long num[]) {int i, j, temp1, k;long temp2;for (i = 0; i < n - 1; i++) {k = i;for (j = i + 1; j < n; j++) {if (score[j] > score[k]) {k = j;//记录最大的下标位置}}if (k != i) {//若最大数的下标位置不在下标位置i;temp1 = score[k];score[k] = score[i];score[i] = temp1;temp2 = num[k]; num[k] = num[i]; num[i] = temp2;}}}//函数功能:打印该门课的成绩;void PrintfScore(int score[], int n, long num[]) {int i;for (i = 0; i < n; i++) {printf("%d%4d\n",num[i],score[i]);}}