定义一个结构体类型student来表示学生的信息,学生的信息有:学生学号、姓名、性别、年龄、入学日期、成绩1、成绩2。要求入学日期使用上题所定义的结构体。 建立一个数组stu[5],输入5个人的资料,并按成绩1从小到大排序输出。(10分) 学生数据为:{ {{2020B200301},”刘滨”,”男”,18,{2020,9,20},70,85}, {{2020B200302},”阚美娟”,”女”,17,{2020,9,20},87,95, {{2020B200303},”胡大勇”,”男”,17,{2020,9,20},69,80}, {{2020B200305},”黎丽”,”女”,18,{2020,9,20},68,88}, {{2020B200309},”金鑫”,”男”,19,{2019,9,1},90,85} }
时间: 2023-06-24 11:08:14 浏览: 89
```c
#include <stdio.h>
#include <string.h>
struct date {
int year;
int month;
int day;
};
struct student {
char id[12];
char name[20];
char sex[6];
int age;
struct date admission_date;
int score1;
int score2;
};
int main() {
struct student stu[5] = {
{"2020B200301", "刘滨", "男", 18, {2020, 9, 20}, 70, 85},
{"2020B200302", "阚美娟", "女", 17, {2020, 9, 20}, 87, 95},
{"2020B200303", "胡大勇", "男", 17, {2020, 9, 20}, 69, 80},
{"2020B200305", "黎丽", "女", 18, {2020, 9, 20}, 68, 88},
{"2020B200309", "金鑫", "男", 19, {2019, 9, 1}, 90, 85}
};
// 冒泡排序
int i, j;
struct student temp;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4 - i; j++) {
if (stu[j].score1 > stu[j+1].score1) {
temp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = temp;
}
}
}
// 输出排序后的结果
printf("学号\t姓名\t性别\t年龄\t入学日期\t成绩1\t成绩2\n");
for (i = 0; i < 5; i++) {
printf("%s\t%s\t%s\t%d\t%d-%d-%d\t%d\t%d\n", stu[i].id, stu[i].name, stu[i].sex, stu[i].age,
stu[i].admission_date.year, stu[i].admission_date.month, stu[i].admission_date.day,
stu[i].score1, stu[i].score2);
}
return 0;
}
```