程序代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
struct Student {
char theName[100];
int theNo;
};
int cmpFunc(const void *a, const void*b) {
return (*(struct Student **)b)->theNo - (*(struct Student **)a)->theNo;
}
void sort_array(struct Student **a, int n)
{
int i, j;
struct Student *tmp;
//排序, 选择法排序
for (i = 0; i < n - 1; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (a[i]->theNo - a[j]->theNo > 0)
{
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
}
int main() {
int theNum;
scanf("%d", &theNum);
assert(0 < theNum && theNum < 100);
struct Student **ss = (struct Student**)malloc(theNum * sizeof(struct Student *));
assert(NULL != ss);
for (int i = 0; i < theNum; i++) {
*(ss + i) = (struct Student*)malloc(sizeof(struct Student));
scanf("%s %d", (*(ss+i))->theName, &((*(ss+i))->theNo));
}
//qsort(ss, theNum, sizeof(struct Student *), cmpFunc);
sort_array(ss, theNum);
for (int i = 0; i < theNum; i++) {
printf("%s\n",(*(ss + i))->theName);
free(*(ss + i));
}
free(ss);
return 0;
}