学生管理系统源代码C语言
时间: 2023-08-24 08:07:03 浏览: 44
以下是一个简单的学生管理系统的源代码,使用C语言编写:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
typedef struct {
char name[20];
int score;
} student;
void print_menu();
int add_student(student students[], int count);
void display_students(student students[], int count);
void search_student(student students[], int count);
void modify_student(student students[], int count);
void delete_student(student students[], int count);
int main() {
student students[MAX_STUDENTS];
int count = 0;
int choice;
while (1) {
print_menu();
scanf("%d", &choice);
switch (choice) {
case 1:
count = add_student(students, count);
break;
case 2:
display_students(students, count);
break;
case 3:
search_student(students, count);
break;
case 4:
modify_student(students, count);
break;
case 5:
delete_student(students, count);
break;
case 6:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
break;
}
}
return 0;
}
void print_menu() {
printf("\n===== Student Management System =====\n");
printf("1. Add student\n");
printf("2. Display all students\n");
printf("3. Search student\n");
printf("4. Modify student\n");
printf("5. Delete student\n");
printf("6. Exit\n");
printf("Enter your choice: ");
}
int add_student(student students[], int count) {
if (count >= MAX_STUDENTS) {
printf("Maximum number of students reached.\n");
return count;
}
student new_student;
printf("Enter student name: ");
scanf("%s", new_student.name);
printf("Enter student score: ");
scanf("%d", &new_student.score);
students[count] = new_student;
count++;
printf("Student added successfully.\n");
return count;
}
void display_students(student students[], int count) {
if (count == 0) {
printf("No students to display.\n");
return;
}
printf("Name\tScore\n");
for (int i = 0; i < count; i++) {
printf("%s\t%d\n", students[i].name, students[i].score);
}
}
void search_student(student students[], int count) {
if (count == 0) {
printf("No students to search for.\n");
return;
}
char search_name[20];
printf("Enter student name to search: ");
scanf("%s", search_name);
for (int i = 0; i < count; i++) {
if (strcmp(students[i].name, search_name) == 0) {
printf("Student found. Score: %d\n", students[i].score);
return;
}
}
printf("Student not found.\n");
}
void modify_student(student students[], int count) {
if (count == 0) {
printf("No students to modify.\n");
return;
}
char modify_name[20];
printf("Enter student name to modify: ");
scanf("%s", modify_name);
for (int i = 0; i < count; i++) {
if (strcmp(students[i].name, modify_name) == 0) {
printf("Enter new score: ");
scanf("%d", &students[i].score);
printf("Student modified successfully.\n");
return;
}
}
printf("Student not found.\n");
}
void delete_student(student students[], int count) {
if (count == 0) {
printf("No students to delete.\n");
return;
}
char delete_name[20];
printf("Enter student name to delete: ");
scanf("%s", delete_name);
for (int i = 0; i < count; i++) {
if (strcmp(students[i].name, delete_name) == 0) {
for (int j = i; j < count - 1; j++) {
students[j] = students[j + 1];
}
count--;
printf("Student deleted successfully.\n");
return;
}
}
printf("Student not found.\n");
}
```
这个学生管理系统支持添加、显示、搜索、修改和删除学生记录。在代码中使用了一个结构体`student`来存储学生的姓名和分数,并使用`typedef`定义了这个结构体的别名。同时,使用了一个预处理器常量`MAX_STUDENTS`来定义学生记录的最大数量。使用`switch`语句实现了菜单功能,用户可以根据提示输入数字来选择不同的操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)