学生成绩管理系统源代码C语言
时间: 2023-09-29 11:02:52 浏览: 60
以下是一个简单的学生成绩管理系统的 C 语言源代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 1000 // 最大学生数
#define MAX_NAME_LENGTH 50 // 最大名字长度
// 学生结构体
struct student {
char name[MAX_NAME_LENGTH];
int id;
float score;
};
// 学生信息数组
struct student students[MAX_STUDENTS];
// 当前学生数
int num_students = 0;
/* 函数声明 */
// 添加学生信息
void add_student();
// 查找学生信息
void find_student();
// 显示所有学生信息
void show_students();
// 显示菜单
void show_menu();
// 清空缓冲区
void clear_buffer();
/* 主函数 */
int main() {
int choice;
do {
show_menu();
scanf("%d", &choice);
clear_buffer();
switch (choice) {
case 1:
add_student();
break;
case 2:
find_student();
break;
case 3:
show_students();
break;
case 0:
printf("程序已退出。\n");
break;
default:
printf("无效的选项,请重新选择。\n");
}
} while (choice != 0);
return 0;
}
/* 函数定义 */
// 添加学生信息
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("学生信息已满,无法添加。\n");
return;
}
printf("请输入学生姓名:");
fgets(students[num_students].name, MAX_NAME_LENGTH, stdin);
students[num_students].name[strcspn(students[num_students].name, "\n")] = '\0'; // 移除换行符
printf("请输入学生学号:");
scanf("%d", &students[num_students].id);
clear_buffer();
printf("请输入学生分数:");
scanf("%f", &students[num_students].score);
clear_buffer();
num_students++;
printf("学生信息添加成功!\n");
}
// 查找学生信息
void find_student() {
int id;
printf("请输入要查找的学生学号:");
scanf("%d", &id);
clear_buffer();
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("学生姓名:%s\n", students[i].name);
printf("学生学号:%d\n", students[i].id);
printf("学生分数:%.2f\n", students[i].score);
return;
}
}
printf("未找到该学生。\n");
}
// 显示所有学生信息
void show_students() {
if (num_students == 0) {
printf("学生信息为空。\n");
return;
}
printf("学生信息如下:\n");
printf("姓名\t学号\t分数\n");
for (int i = 0; i < num_students; i++) {
printf("%s\t%d\t%.2f\n", students[i].name, students[i].id, students[i].score);
}
}
// 显示菜单
void show_menu() {
printf("\n");
printf("学生成绩管理系统\n");
printf("1. 添加学生信息\n");
printf("2. 查找学生信息\n");
printf("3. 显示所有学生信息\n");
printf("0. 退出程序\n");
printf("请选择操作:");
}
// 清空缓冲区
void clear_buffer() {
while (getchar() != '\n');
}
```
这个程序使用结构体存储学生信息,可以添加、查找和显示学生信息。在主函数中使用了一个 do-while 循环,不断显示菜单并等待用户输入选择,直到用户选择退出程序为止。
注意,这个程序没有进行输入验证,例如当用户输入非法字符时可能会导致程序崩溃。实际使用时,需要对用户输入进行验证和处理。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)