用c语言编写一个学生管理系统要求录入学生信息包括姓名学号地址年龄电话邮箱等信息到文件中实现录入,浏览修改删除排序查询等功能
时间: 2024-06-09 19:08:19 浏览: 97
C语言学生信息管理系统.txt
5星 · 资源好评率100%
以下是一个简单的学生管理系统的 C 语言代码示例,包括录入、浏览、修改、删除、排序和查询等功能。其中学生信息保存在一个文件中,每个学生的信息占用一行,各个信息之间用逗号分隔。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100 // 最大学生数量
#define MAX_NAME_LEN 20 // 姓名最大长度
#define MAX_ADDRESS_LEN 50 // 地址最大长度
#define MAX_EMAIL_LEN 30 // 邮箱最大长度
#define FILE_PATH "students.txt" // 学生信息文件路径
// 学生信息结构体
typedef struct {
char name[MAX_NAME_LEN];
char id[10];
char address[MAX_ADDRESS_LEN];
int age;
char phone[12];
char email[MAX_EMAIL_LEN];
} Student;
Student students[MAX_STUDENT_NUM]; // 学生数组
int student_num = 0; // 学生数量
// 从文件中读取学生信息
void load_students() {
FILE *fp = fopen(FILE_PATH, "r");
if (fp == NULL) {
return;
}
char line[200], *p;
while (fgets(line, sizeof(line), fp) != NULL) {
p = strtok(line, ",");
strcpy(students[student_num].name, p);
p = strtok(NULL, ",");
strcpy(students[student_num].id, p);
p = strtok(NULL, ",");
strcpy(students[student_num].address, p);
p = strtok(NULL, ",");
students[student_num].age = atoi(p);
p = strtok(NULL, ",");
strcpy(students[student_num].phone, p);
p = strtok(NULL, ",");
strcpy(students[student_num].email, p);
student_num++;
}
fclose(fp);
}
// 保存学生信息到文件
void save_students() {
FILE *fp = fopen(FILE_PATH, "w");
if (fp == NULL) {
printf("Failed to open file.\n");
return;
}
for (int i = 0; i < student_num; i++) {
fprintf(fp, "%s,%s,%s,%d,%s,%s\n", students[i].name, students[i].id,
students[i].address, students[i].age, students[i].phone,
students[i].email);
}
fclose(fp);
}
// 录入学生信息
void input_student() {
if (student_num >= MAX_STUDENT_NUM) {
printf("Too many students.\n");
return;
}
printf("Please input student name: ");
scanf("%s", students[student_num].name);
printf("Please input student id: ");
scanf("%s", students[student_num].id);
printf("Please input student address: ");
scanf("%s", students[student_num].address);
printf("Please input student age: ");
scanf("%d", &students[student_num].age);
printf("Please input student phone: ");
scanf("%s", students[student_num].phone);
printf("Please input student email: ");
scanf("%s", students[student_num].email);
student_num++;
printf("Student added successfully.\n");
}
// 浏览学生信息
void browse_students() {
if (student_num == 0) {
printf("No students found.\n");
return;
}
printf("Name\tID\tAddress\tAge\tPhone\t\tEmail\n");
for (int i = 0; i < student_num; i++) {
printf("%s\t%s\t%s\t%d\t%s\t%s\n", students[i].name, students[i].id,
students[i].address, students[i].age, students[i].phone,
students[i].email);
}
}
// 修改学生信息
void modify_student() {
char id[10];
printf("Please input the id of the student to be modified: ");
scanf("%s", id);
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("Please input new student name: ");
scanf("%s", students[i].name);
printf("Please input new student id: ");
scanf("%s", students[i].id);
printf("Please input new student address: ");
scanf("%s", students[i].address);
printf("Please input new student age: ");
scanf("%d", &students[i].age);
printf("Please input new student phone: ");
scanf("%s", students[i].phone);
printf("Please input new student email: ");
scanf("%s", students[i].email);
printf("Student modified successfully.\n");
return;
}
}
printf("Student not found.\n");
}
// 删除学生信息
void delete_student() {
char id[10];
printf("Please input the id of the student to be deleted: ");
scanf("%s", id);
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].id, id) == 0) {
for (int 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 sort_students_by_name() {
for (int i = 0; i < student_num - 1; i++) {
for (int j = i + 1; j < student_num; j++) {
if (strcmp(students[i].name, students[j].name) > 0) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("Students sorted by name.\n");
}
// 按学号查询学生信息
void query_student_by_id() {
char id[10];
printf("Please input the id to be queried: ");
scanf("%s", id);
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("Name\tID\tAddress\tAge\tPhone\t\tEmail\n");
printf("%s\t%s\t%s\t%d\t%s\t%s\n", students[i].name, students[i].id,
students[i].address, students[i].age, students[i].phone,
students[i].email);
return;
}
}
printf("Student not found.\n");
}
int main() {
load_students();
int choice;
while (1) {
printf("1. Input student\n");
printf("2. Browse students\n");
printf("3. Modify student\n");
printf("4. Delete student\n");
printf("5. Sort students by name\n");
printf("6. Query student by id\n");
printf("0. Exit\n");
printf("Please input your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
input_student();
break;
case 2:
browse_students();
break;
case 3:
modify_student();
break;
case 4:
delete_student();
break;
case 5:
sort_students_by_name();
break;
case 6:
query_student_by_id();
break;
case 0:
save_students();
return 0;
default:
printf("Invalid choice.\n");
}
}
}
```
阅读全文