定义一个学生结构体Student(含学号、姓名、成绩)和一个函数sort(struct+Student+*p),该函数使用选择排序法按成绩升序排序。在主函数中输入10个学生的学号、姓名和成绩,调用so
时间: 2024-06-14 13:03:38 浏览: 192
设计一个学生类Student(学生学号、姓名、数学、英语、计算机成绩;)
5星 · 资源好评率100%
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char id[20];
char name[20];
float score;
};
void sort(struct Student *p, int n) {
int i, j, min_index;
struct Student temp;
for (i = 0; i < n - 1; i++) {
min_index = i;
for (j = i + 1; j < n; j++) {
if (p[j].score < p[min_index].score) {
min_index = j;
}
}
if (min_index != i) {
temp = p[i];
p[i] = p[min_index];
p[min_index] = temp;
}
}
}
int main() {
int n = 10;
struct Student *students = (struct Student *)malloc(n * sizeof(struct Student));
int i;
for (i = 0; i < n; i++) {
printf("请输入第%d个学生的学号、姓名和成绩:", i + 1);
scanf("%s%s%f", students[i].id, students[i].name, &students[i].score);
}
sort(students, n);
printf("排序后的学生信息为:\n");
for (i = 0; i < n; i++) {
printf("%s\t%s\t%.2f\n", students[i].id, students[i].name, students[i].score);
}
free(students);
return 0;
}
```
阅读全文