C语言学生管理系统代码
时间: 2023-12-20 15:30:53 浏览: 36
C语言学生管理系统代码是一个用C语言编写的程序,用于管理学生信息和成绩。以下是一个简单的C语言学生管理系统代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100
struct student {
char name[20];
int id;
float score;
};
struct student students[MAX_STUDENT_NUM];
int student_num = 0;
void add_student() {
if (student_num >= MAX_STUDENT_NUM) {
printf("Error: student list is full.\n");
return;
}
struct student new_student;
printf("Please enter the name of the student: ");
scanf("%s", new_student.name);
printf("Please enter the ID of the student: ");
scanf("%d", &new_student.id);
printf("Please enter the score of the student: ");
scanf("%f", &new_student.score);
students[student_num] = new_student;
student_num++;
printf("Student added successfully.\n");
}
void print_student(struct student s) {
printf("Name: %s, ID: %d, Score: %.2f\n", s.name, s.id, s.score);
}
void print_all_students() {
if (student_num == 0) {
printf("No students found.\n");
return;
}
printf("All students:\n");
for (int i = 0; i < student_num; i++) {
print_student(students[i]);
}
}
void search_student() {
if (student_num == 0) {
printf("No students found.\n");
return;
}
int search_id;
printf("Please enter the ID of the student you want to search: ");
scanf("%d", &search_id);
for (int i = 0; i < student_num; i++) {
if (students[i].id == search_id) {
print_student(students[i]);
return;
}
}
printf("No student found with ID %d.\n", search_id);
}
void delete_student() {
if (student_num == 0) {
printf("No students found.\n");
return;
}
int delete_id;
printf("Please enter the ID of the student you want to delete: ");
scanf("%d", &delete_id);
for (int i = 0; i < student_num; i++) {
if (students[i].id == delete_id) {
for (int j = i; j < student_num - 1; j++) {
students[j] = students[j + 1];
}
student_num--;
printf("Student deleted successfully.\n");
return;
}
}
printf("No student found with ID %d.\n", delete_id);
}
int main() {
int choice;
while (1) {
printf("**************************\n");
printf("**\t学生管理系统\t**\n");
printf("**************************\n");
printf("**\t1.录入学生信息\t**\n");
printf("**\t2.查找学生信息\t**\n");
printf("**\t3.删除学生信息\t**\n");
printf("**\t4.显示所有学生\t**\n");
printf("**\t0.退出系统\t**\n");
printf("**************************\n");
printf("Please enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
search_student();
break;
case 3:
delete_student();
break;
case 4:
print_all_students();
break;
case 0:
printf("Goodbye!\n");
exit(0);
default:
printf("Invalid choice.\n");
break;
}
}
return 0;
}
```
该程序包含以下功能:
1.录入学生信息
2.查找学生信息
3.删除学生信息
4.显示所有学生
用户可以根据自己的需要选择相应的功能。该程序使用结构体存储学生信息,可以存储最多100个学生的信息。