用c语言编写使用结构体表示,一个学生的信息包括学号,姓名,性别,生日,班级,每门课的成绩,并编写程序完成学生信息的录入,查询信息列表,能按指定项目排序显示的功能
时间: 2024-04-30 12:17:57 浏览: 135
以下是用 C 语言编写使用结构体表示一个学生信息的示例代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_CLASS_LEN 10
#define MAX_COURSE_NUM 5
struct student {
char id[10];
char name[MAX_NAME_LEN];
char gender[5];
char birthday[20];
char class[MAX_CLASS_LEN];
int scores[MAX_COURSE_NUM];
};
void input_student_info(struct student *s);
void print_student_info(struct student *s);
void print_student_list(struct student *students, int count);
void sort_students(struct student *students, int count, int sort_by);
int main() {
int n;
printf("Enter the number of students: ");
scanf("%d", &n);
struct student students[n];
for (int i = 0; i < n; i++) {
printf("\nEnter the information for student #%d:\n", i+1);
input_student_info(&students[i]);
}
printf("\n\nStudent List:\n");
print_student_list(students, n);
printf("\n\nSorted by ID:\n");
sort_students(students, n, 1);
print_student_list(students, n);
printf("\n\nSorted by Name:\n");
sort_students(students, n, 2);
print_student_list(students, n);
printf("\n\nSorted by Average Score:\n");
sort_students(students, n, 3);
print_student_list(students, n);
return 0;
}
void input_student_info(struct student *s) {
printf("ID: ");
scanf("%s", s->id);
printf("Name: ");
scanf("%s", s->name);
printf("Gender: ");
scanf("%s", s->gender);
printf("Birthday: ");
scanf("%s", s->birthday);
printf("Class: ");
scanf("%s", s->class);
for (int i = 0; i < MAX_COURSE_NUM; i++) {
printf("Score for Course #%d: ", i+1);
scanf("%d", &s->scores[i]);
}
}
void print_student_info(struct student *s) {
printf("\nID: %s\n", s->id);
printf("Name: %s\n", s->name);
printf("Gender: %s\n", s->gender);
printf("Birthday: %s\n", s->birthday);
printf("Class: %s\n", s->class);
printf("Scores:");
for (int i = 0; i < MAX_COURSE_NUM; i++) {
printf(" %d", s->scores[i]);
}
printf("\n");
}
void print_student_list(struct student *students, int count) {
for (int i = 0; i < count; i++) {
print_student_info(&students[i]);
}
}
void sort_students(struct student *students, int count, int sort_by) {
for (int i = 0; i < count-1; i++) {
for (int j = 0; j < count-i-1; j++) {
int cmp_result = 0;
switch (sort_by) {
case 1:
cmp_result = strcmp(students[j].id, students[j+1].id);
break;
case 2:
cmp_result = strcmp(students[j].name, students[j+1].name);
break;
case 3: {
int sum_j = 0, sum_j1 = 0;
for (int k = 0; k < MAX_COURSE_NUM; k++) {
sum_j += students[j].scores[k];
sum_j1 += students[j+1].scores[k];
}
cmp_result = sum_j1 - sum_j;
break;
}
}
if (cmp_result < 0) {
struct student temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
}
```
这个程序中,我们定义了一个结构体 `struct student`,它包含了一个学生的所有信息,包括学号、姓名、性别、生日、班级和每门课的成绩。我们在 `main` 函数中定义了一个 `struct student` 数组 `students`,大小为用户输入的学生数量 `n`。
然后,我们使用 `input_student_info` 函数来逐个输入每个学生的信息,使用 `print_student_list` 函数来打印学生列表。最后,我们使用 `sort_students` 函数来实现按照指定项目排序的功能,排序后再使用 `print_student_list` 函数打印排序后的学生列表。
在 `sort_students` 函数中,我们使用了一个 `switch` 语句来根据用户选择的排序项目,进行不同的比较操作。在本例中,我们提供了三种排序方法:按照学号排序、按照姓名排序、按照平均分排序。对于平均分排序,我们先计算出每个学生的平均分,然后按照平均分从高到低排序。
阅读全文