用C语言完成:采用结构体数组对学生的学号、姓名、各科成绩的存储,按成绩排序,按学号、姓名查找
时间: 2023-08-16 12:08:36 浏览: 400
使用结构体数组,利用函数实现按学生学号和姓名的查询操作.c
好的,以下是用C语言实现对学生信息的存储、排序和查找的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT 100 // 最大学生数
// 学生结构体
struct student {
int id; // 学号
char name[20]; // 姓名
float score[3]; // 三科成绩
float total; // 总分
};
// 学生数组
struct student students[MAX_STUDENT];
// 学生数
int num_students = 0;
// 比较函数:按总分排序
int cmp_by_total(const void *a, const void *b) {
struct student *s1 = (struct student *)a;
struct student *s2 = (struct student *)b;
return (s2->total - s1->total) > 0 ? 1 : -1;
}
// 比较函数:按学号排序
int cmp_by_id(const void *a, const void *b) {
struct student *s1 = (struct student *)a;
struct student *s2 = (struct student *)b;
return s1->id - s2->id;
}
// 比较函数:按姓名排序
int cmp_by_name(const void *a, const void *b) {
struct student *s1 = (struct student *)a;
struct student *s2 = (struct student *)b;
return strcmp(s1->name, s2->name);
}
// 添加学生
void add_student() {
if (num_students >= MAX_STUDENT) {
printf("已达到最大学生数,无法添加!\n");
return;
}
printf("请输入学号:");
scanf("%d", &students[num_students].id);
printf("请输入姓名:");
scanf("%s", students[num_students].name);
printf("请输入三科成绩(用空格分隔):");
scanf("%f %f %f", &students[num_students].score[0], &students[num_students].score[1], &students[num_students].score[2]);
students[num_students].total = students[num_students].score[0] + students[num_students].score[1] + students[num_students].score[2];
num_students++;
printf("添加成功!\n");
}
// 显示学生信息
void show_students() {
if (num_students == 0) {
printf("学生信息为空!\n");
return;
}
printf("学号\t姓名\t成绩1\t成绩2\t成绩3\t总分\n");
for (int i = 0; i < num_students; i++) {
printf("%d\t%s\t%.1f\t%.1f\t%.1f\t%.1f\n", students[i].id, students[i].name, students[i].score[0], students[i].score[1], students[i].score[2], students[i].total);
}
}
// 按总分排序
void sort_by_total() {
qsort(students, num_students, sizeof(struct student), cmp_by_total);
printf("按总分排序成功!\n");
}
// 按学号排序
void sort_by_id() {
qsort(students, num_students, sizeof(struct student), cmp_by_id);
printf("按学号排序成功!\n");
}
// 按姓名排序
void sort_by_name() {
qsort(students, num_students, sizeof(struct student), cmp_by_name);
printf("按姓名排序成功!\n");
}
// 按学号查找
void search_by_id() {
int id;
printf("请输入要查找的学号:");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("学号\t姓名\t成绩1\t成绩2\t成绩3\t总分\n");
printf("%d\t%s\t%.1f\t%.1f\t%.1f\t%.1f\n", students[i].id, students[i].name, students[i].score[0], students[i].score[1], students[i].score[2], students[i].total);
return;
}
}
printf("未找到该学生!\n");
}
// 按姓名查找
void search_by_name() {
char name[20];
printf("请输入要查找的姓名:");
scanf("%s", name);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("学号\t姓名\t成绩1\t成绩2\t成绩3\t总分\n");
printf("%d\t%s\t%.1f\t%.1f\t%.1f\t%.1f\n", students[i].id, students[i].name, students[i].score[0], students[i].score[1], students[i].score[2], students[i].total);
return;
}
}
printf("未找到该学生!\n");
}
int main() {
int choice;
while (1) {
printf("\n");
printf("1. 添加学生\n");
printf("2. 显示学生信息\n");
printf("3. 按总分排序\n");
printf("4. 按学号排序\n");
printf("5. 按姓名排序\n");
printf("6. 按学号查找\n");
printf("7. 按姓名查找\n");
printf("8. 退出\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
show_students();
break;
case 3:
sort_by_total();
break;
case 4:
sort_by_id();
break;
case 5:
sort_by_name();
break;
case 6:
search_by_id();
break;
case 7:
search_by_name();
break;
case 8:
printf("再见!\n");
return 0;
default:
printf("无效的选择!\n");
break;
}
}
return 0;
}
```
该程序使用结构体数组存储学生信息,支持添加、显示、排序和查找操作。可以按学号、姓名或总分进行排序,按学号或姓名进行查找。
阅读全文