用C语言写一个学生信息管理系统。
时间: 2023-12-30 13:03:32 浏览: 51
用C语言写的 学生信息管理系统
好的,下面是一个简单的学生信息管理系统的C语言代码示例:
```c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_NAME_LEN 20
#define MAX_STUDENT_NUM 100
struct student {
char name[MAX_NAME_LEN];
int id;
int 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 s;
printf("Enter student name: ");
scanf("%s", s.name);
printf("Enter student id: ");
scanf("%d", &s.id);
printf("Enter student score: ");
scanf("%d", &s.score);
students[student_num++] = s;
printf("Student added successfully!\n");
}
// 查找学生信息
void find_student() {
int id;
printf("Enter student id to search: ");
scanf("%d", &id);
int i;
for (i = 0; i < student_num; i++) {
if (students[i].id == id) {
printf("Student found!\n");
printf("Name: %s\n", students[i].name);
printf("Id: %d\n", students[i].id);
printf("Score: %d\n", students[i].score);
return;
}
}
printf("Student not found!\n");
}
// 修改学生信息
void modify_student() {
int id;
printf("Enter student id to modify: ");
scanf("%d", &id);
int i;
for (i = 0; i < student_num; i++) {
if (students[i].id == id) {
printf("Enter new student name (press Enter to skip): ");
char name[MAX_NAME_LEN];
getchar(); // 把之前的回车符吃掉
fgets(name, MAX_NAME_LEN, stdin);
if (strlen(name) > 1) {
name[strlen(name)-1] = '\0'; // 把最后的回车符替换为字符串结束符
strcpy(students[i].name, name);
}
printf("Enter new student score (press Enter to skip): ");
char score_str[10];
fgets(score_str, 10, stdin);
if (strlen(score_str) > 1) {
students[i].score = atoi(score_str);
}
printf("Student modified successfully!\n");
return;
}
}
printf("Student not found!\n");
}
// 删除学生信息
void delete_student() {
int id;
printf("Enter student id to delete: ");
scanf("%d", &id);
int i;
for (i = 0; i < student_num; i++) {
if (students[i].id == id) {
// 把该学生之后的所有学生信息向前移动一个位置
int j;
for (j = i; j < student_num-1; j++) {
students[j] = students[j+1];
}
student_num--;
printf("Student deleted successfully!\n");
return;
}
}
printf("Student not found!\n");
}
// 显示所有学生信息
void show_all_students() {
int i;
for (i = 0; i < student_num; i++) {
printf("==================\n");
printf("Name: %s\n", students[i].name);
printf("Id: %d\n", students[i].id);
printf("Score: %d\n", students[i].score);
}
}
int main() {
char command;
while (1) {
printf("==================\n");
printf("Commands:\n");
printf("a: Add student information\n");
printf("f: Find student information\n");
printf("m: Modify student information\n");
printf("d: Delete student information\n");
printf("s: Show all student information\n");
printf("q: Quit\n");
printf("Enter command: ");
scanf("%s", &command);
switch(command) {
case 'a': add_student(); break;
case 'f': find_student(); break;
case 'm': modify_student(); break;
case 'd': delete_student(); break;
case 's': show_all_students(); break;
case 'q': return 0;
default: printf("Invalid command!\n"); break;
}
}
return 0;
}
```
这个程序可以添加、查找、修改、删除和显示所有学生信息。你可以根据需要进一步扩展功能。
阅读全文