c语言代码 分模块设计一个学生信息库系统。该系统包含学生的基本信息的建立和输出、计算学生平均成绩、按照学生的平均成绩排序以及查询、修改学生的成绩等功能。
时间: 2024-03-22 18:41:49 浏览: 27
好的,下面是分模块设计的C语言代码:
```
// 学生信息库系统
// 模块1:输入和输出学生信息
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100 //最大学生数
#define NAME_LEN 20 // 学生姓名最大长度
#define COURSE_NUM 3 // 学生课程数
// 学生信息结构体
typedef struct {
char name[NAME_LEN];
int id;
int score[COURSE_NUM];
float avg_score;
} Student;
// 学生信息输入函数
void input_student_info(Student *stu, int i) {
printf("请输入第%d个学生的姓名:", i+1);
scanf("%s", stu->name);
printf("请输入第%d个学生的学号:", i+1);
scanf("%d", &stu->id);
printf("请输入第%d个学生的三门课程成绩:", i+1);
for (int j = 0; j < COURSE_NUM; j++) {
scanf("%d", &stu->score[j]);
}
}
// 学生信息输出函数
void output_student_info(Student stu) {
printf("姓名:%s,学号:%d,成绩:", stu.name, stu.id);
for (int j = 0; j < COURSE_NUM; j++) {
printf("%d ", stu.score[j]);
}
printf(",平均分:%.2f\n", stu.avg_score);
}
// 模块2:计算学生平均成绩
void calculate_student_avg_score(Student *stu) {
float sum = 0;
for (int i = 0; i < COURSE_NUM; i++) {
sum += stu->score[i];
}
stu->avg_score = sum / COURSE_NUM;
}
// 模块3:排序学生信息
void sort_student_info(Student *stu, int n) {
Student temp;
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (stu[j].avg_score < stu[j+1].avg_score) {
temp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = temp;
}
}
}
}
// 模块4:查询学生信息
void search_student_info(Student *stu, int n) {
int id;
printf("请输入要查询的学生的学号:");
scanf("%d", &id);
for (int i = 0; i < n; i++) {
if (stu[i].id == id) {
printf("查询结果:");
output_student_info(stu[i]);
return;
}
}
printf("没有找到该学生的信息!\n");
}
// 模块5:修改学生信息
void modify_student_info(Student *stu, int n) {
int id, choice;
printf("请输入要修改的学生的学号:");
scanf("%d", &id);
for (int i = 0; i < n; i++) {
if (stu[i].id == id) {
printf("查询结果:");
output_student_info(stu[i]);
printf("请选择要修改的信息:1.姓名 2.学号 3.成绩\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入新的姓名:");
scanf("%s", stu[i].name);
break;
case 2:
printf("请输入新的学号:");
scanf("%d", &stu[i].id);
break;
case 3:
printf("请输入新的三门课程成绩:");
for (int j = 0; j < COURSE_NUM; j++) {
scanf("%d", &stu[i].score[j]);
}
calculate_student_avg_score(&stu[i]);
break;
default:
printf("输入有误!\n");
}
printf("修改后的信息:");
output_student_info(stu[i]);
return;
}
}
printf("没有找到该学生的信息!\n");
}
int main() {
Student stu_list[MAX_STUDENT_NUM];
int choice, n = 0;
while (1) {
printf("请选择要进行的操作:1.录入学生信息 2.输出学生信息 3.计算学生平均成绩 4.排序学生信息 5.查询学生信息 6.修改学生信息 0.退出\n");
scanf("%d", &choice);
switch (choice) {
case 1:
if (n >= MAX_STUDENT_NUM) {
printf("学生人数已达上限,无法再录入!\n");
break;
}
input_student_info(&stu_list[n], n);
calculate_student_avg_score(&stu_list[n]);
n++;
break;
case 2:
printf("学生信息如下:\n");
for (int i = 0; i < n; i++) {
output_student_info(stu_list[i]);
}
break;
case 3:
for (int i = 0; i < n; i++) {
calculate_student_avg_score(&stu_list[i]);
}
printf("学生平均成绩已计算完成!\n");
break;
case 4:
sort_student_info(stu_list, n);
printf("学生信息已按平均成绩排序!\n");
break;
case 5:
search_student_info(stu_list, n);
break;
case 6:
modify_student_info(stu_list, n);
break;
case 0:
printf("程序已退出!\n");
exit(0);
default:
printf("输入有误!\n");
}
}
return 0;
}
```
上述代码中,我们将学生信息库系统分为了5个模块,分别是:
1. 输入和输出学生信息
2. 计算学生平均成绩
3. 排序学生信息
4. 查询学生信息
5. 修改学生信息
每个模块都有对应的函数实现,并在主函数中根据用户的选择调用相应的函数。这样的设计使得代码更加清晰、易于维护。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)