#include<stdio.h> #include<string.h> typedef struct date { int year; int month; int day; }DATE; typedef struct student { int num; char name[20]; char sex; DATE birthday; float score; }STUDENT; void input(STUDENT *s); void output(STUDENT s); /********** Begin **********/ /********** End **********/
时间: 2023-07-05 19:31:22 浏览: 249
完整代码如下所示:
```c
#include <stdio.h>
#include <string.h>
typedef struct date {
int year;
int month;
int day;
} DATE;
typedef struct student {
int num;
char name[20];
char sex;
DATE birthday;
float score;
} STUDENT;
void input(STUDENT *s);
void output(STUDENT s);
void sort(STUDENT s[], int n);
int main() {
STUDENT s[5];
int i;
for (i = 0; i < 5; i++) {
printf("请输入第%d个学生的信息:\n", i + 1);
input(&s[i]);
}
sort(s, 5);
printf("\n排序后的学生信息为:\n");
for (i = 0; i < 5; i++) {
output(s[i]);
}
return 0;
}
void input(STUDENT *s) {
printf("学号:");
scanf("%d", &s -> num);
printf("姓名:");
scanf("%s", s -> name);
printf("性别:");
scanf(" %c", &s -> sex);
printf("生日:");
scanf("%d/%d/%d", &s -> birthday.year, &s -> birthday.month, &s -> birthday.day);
printf("成绩:");
scanf("%f", &s -> score);
}
void output(STUDENT s) {
printf("学号:%d,姓名:%s,性别:%c,生日:%d/%d/%d,成绩:%.2f\n",
s.num, s.name, s.sex, s.birthday.year, s.birthday.month, s.birthday.day, s.score);
}
void sort(STUDENT s[], int n) {
int i, j;
STUDENT temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (s[i].score < s[j].score) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
}
```
这段代码中,定义了一个main函数,用于程序的入口,创建了一个学生信息数组s。在main函数中,使用for循环对每个学生的信息进行输入。输入完成后,调用sort函数对学生信息数组s进行排序。最后,使用for循环对排序后的学生信息进行输出。
阅读全文