学生信息管理系统c语言代码
c#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_NAME_LENGTH 50
#define MAX_ID_LENGTH 20
struct Student {
char name[MAX_NAME_LENGTH];
char id[MAX_ID_LENGTH];
int age;
};
struct Student students[MAX_STUDENTS];
int numStudents = 0;
void addStudent() {
if (numStudents < MAX_STUDENTS) {
struct Student newStudent;
printf("Enter student name: ");
scanf("%s", newStudent.name);
printf("Enter student ID: ");
scanf("%s", newStudent.id);
printf("Enter student age: ");
scanf("%d", &newStudent.age);
students[numStudents] = newStudent;
numStudents++;
printf("Student added successfully.\n");
} else {
printf("Maximum number of students reached.\n");
}
}
void displayStudents() {
printf("Student List:\n");
printf("Name\t\tID\t\tAge\n");
for (int i = 0; i < numStudents; i++) {
printf("%s\t\t%s\t\t%d\n", students[i].name, students[i].id, students[i].age);
}
}
void searchStudentById() {
char searchId[MAX_ID_LENGTH];
printf("Enter student ID to search: ");
scanf("%s", searchId);
int found = 0;
for (int i = 0; i < numStudents; i++) {
if (strcmp(students[i].id, searchId) == 0) {
printf("Student found:\n");
printf("Name: %s\n", students[i].name);
printf("ID: %s\n", students[i].id);
printf("Age: %d\n", students[i].age);
found = 1;
break;
}
}
if (!found) {
printf("Student not found.\n");
}
}
int main() {
int choice;
do {
printf("\nStudent Information Management System\n");
printf("1. Add Student\n");
printf("2. Display Students\n");
printf("3. Search Student by ID\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent();
break;
case 2:
displayStudents();
break;
case 3:
searchStudentById();
break;
case 4:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 4);
return 0;
}
这个程序允许用户通过简单的文本菜单执行添加学生信息。显示所有学生信息。通过学生ID搜索学生信息。退出程序。
删除学生信息: 允许用户删除特定学生的信息。编辑学生信息: 允许用户编辑特定学生的信息。数据持久化: 将学生信息保存到文件中,以便在程序重新运行时保留信息。排序: 按照姓名、ID或年龄等字段对学生信息进行排序显示。输入验证: 对用户输入进行验证,防止程序崩溃或产生意外行为。界面美化: 添加一些界面设计,使用户交互更友好。
cvoid deleteStudentById() {
char deleteId[MAX_ID_LENGTH];
printf("Enter student ID to delete: ");
scanf("%s", deleteId);
int found = 0;
for (int i = 0; i < numStudents; i++) {
if (strcmp(students[i].id, deleteId) == 0) {
found = 1;
for (int j = i; j < numStudents - 1; j++) {
students[j] = students[j + 1];
}
numStudents--;
printf("Student deleted successfully.\n");
break;
}
}
if (!found) {
printf("Student not found.\n");
}
}
void editStudentById() {
char editId[MAX_ID_LENGTH];
printf("Enter student ID to edit: ");
scanf("%s", editId);
int found = 0;
for (int i = 0; i < numStudents; i++) {
if (strcmp(students[i].id, editId) == 0) {
found = 1;
printf("Enter new name: ");
scanf("%s", students[i].name);
printf("Enter new ID: ");
scanf("%s", students[i].id);
printf("Enter new age: ");
scanf("%d", &students[i].age);
printf("Student information updated successfully.\n");
break;
}
}
if (!found) {
printf("Student not found.\n");
}
}
在主菜单中添加选项,让用户能够调用这两个函数:
ccase 5:
deleteStudentById();
break;
case 6:
editStudentById();
break;