代码如下:
#include
#include
#include
#define MAX 50
typedef struct {
char stuId[10];
char name[20];
int score;
}Student;
void input(Student students[MAX], int n)
{
int i;
for (i = 0; i < n; i++) {
scanf("%s", students[i].stuId);
scanf("%s", students[i].name);
scanf("%d", &students[i].score);
}
}
void sort(Student students[MAX], int n)
{
int i, j;
Student temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (students[j].score < students[j + 1].score) {
memcpy(&temp, &students[j], sizeof(Student));
memcpy(&students[j], &students[j + 1], sizeof(Student));
memcpy(&students[j + 1], &temp, sizeof(Student));
}
}
}
}
void print(Student students[MAX], int n)
{
int i;
for (i = 0; i < n; i++) {
printf("%s %s %d\n", students[i].stuId, students[i].name, students[i].score);
}
}
int main()
{
Student students[MAX];
int n;
scanf("%d", &n);
input(students, n);
sort(students, n);
printf("输出:\n");
print(students, n);
system("pause");
return 0;
}
运行结果: