学生公寓管理系统 完成一个学生公寓管理系统。要求包含链表创建及增删查改操作,要求有文件操作。 要求起码包含如下信息:班级、学号、姓名、宿舍号、床位、手机号、入住时间、搬出时间。 基本功能如下: 导入已有文件建立链表 更新好保存到文件 新生入住 换寝室 大四生毕业、休学/退学 查询(能按班级查找学生的宿舍情况、能按宿舍号查询学生信息、能按学生学号或姓名查到学生宿舍,查空寝室,查有空位的寝室……) 信息字段可增加,功能可增加,请用C语言完成
时间: 2023-12-11 14:03:54 浏览: 87
学生公寓管理系统
好的,我可以为你提供一个简单的学生公寓管理系统的示例代码,包含链表操作和文件操作,其中包含的信息有班级、学号、姓名、宿舍号、床位、手机号、入住时间和搬出时间。
代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 50
typedef struct Student {
char class[MAX_LEN];
char id[MAX_LEN];
char name[MAX_LEN];
char dormitory[MAX_LEN];
char bed[MAX_LEN];
char phone[MAX_LEN];
char in_time[MAX_LEN];
char out_time[MAX_LEN];
struct Student *next;
} Student;
// 从文件中读取学生信息
void read_students(Student **head, const char *filename) {
FILE *fp = fopen(filename, "r");
if (!fp) {
printf("打开文件失败!\n");
return;
}
char buffer[MAX_LEN];
while (fgets(buffer, MAX_LEN, fp)) {
Student *student = (Student *)malloc(sizeof(Student));
sscanf(buffer, "%s %s %s %s %s %s %s %s", student->class, student->id, student->name, student->dormitory, student->bed, student->phone, student->in_time, student->out_time);
student->next = *head;
*head = student;
}
fclose(fp);
}
// 将学生信息写入文件
void write_students(const Student *head, const char *filename) {
FILE *fp = fopen(filename, "w");
if (!fp) {
printf("打开文件失败!\n");
return;
}
for (const Student *student = head; student; student = student->next) {
fprintf(fp, "%s %s %s %s %s %s %s %s\n", student->class, student->id, student->name, student->dormitory, student->bed, student->phone, student->in_time, student->out_time);
}
fclose(fp);
}
// 输出学生信息
void print_student(const Student *student) {
printf("班级:%s 学号:%s 姓名:%s 宿舍号:%s 床位:%s 手机号:%s 入住时间:%s 搬出时间:%s\n", student->class, student->id, student->name, student->dormitory, student->bed, student->phone, student->in_time, student->out_time);
}
// 输出所有学生信息
void print_students(const Student *head) {
for (const Student *student = head; student; student = student->next) {
print_student(student);
}
}
// 根据学号查找学生
Student *find_student(const Student *head, const char *id) {
for (const Student *student = head; student; student = student->next) {
if (strcmp(student->id, id) == 0) {
return (Student *)student;
}
}
return NULL;
}
// 根据宿舍号查找学生
void find_students_by_dormitory(const Student *head, const char *dormitory) {
int count = 0;
for (const Student *student = head; student; student = student->next) {
if (strcmp(student->dormitory, dormitory) == 0) {
print_student(student);
count++;
}
}
if (count == 0) {
printf("没有找到该宿舍的学生!\n");
}
}
// 根据班级查找学生
void find_students_by_class(const Student *head, const char *class) {
int count = 0;
for (const Student *student = head; student; student = student->next) {
if (strcmp(student->class, class) == 0) {
print_student(student);
count++;
}
}
if (count == 0) {
printf("没有找到该班级的学生!\n");
}
}
// 添加学生
void add_student(Student **head) {
Student *student = (Student *)malloc(sizeof(Student));
printf("请输入学生信息(班级 学号 姓名 宿舍号 床位 手机号 入住时间 搬出时间):");
scanf("%s %s %s %s %s %s %s %s", student->class, student->id, student->name, student->dormitory, student->bed, student->phone, student->in_time, student->out_time);
student->next = *head;
*head = student;
}
// 修改学生信息
void modify_student(Student *student) {
printf("请输入学生信息(班级 学号 姓名 宿舍号 床位 手机号 入住时间 搬出时间):");
scanf("%s %s %s %s %s %s %s %s", student->class, student->id, student->name, student->dormitory, student->bed, student->phone, student->in_time, student->out_time);
}
// 删除学生
void delete_student(Student **head, const char *id) {
Student *prev = NULL;
for (Student *student = *head; student; prev = student, student = student->next) {
if (strcmp(student->id, id) == 0) {
if (prev) {
prev->next = student->next;
} else {
*head = student->next;
}
free(student);
printf("删除成功!\n");
return;
}
}
printf("没有找到该学生!\n");
}
// 换寝室
void change_dormitory(Student *student) {
printf("请输入新的宿舍号和床位号:");
scanf("%s %s", student->dormitory, student->bed);
}
int main() {
Student *head = NULL;
read_students(&head, "students.txt");
int choice;
while (1) {
printf("请选择操作:\n");
printf("1. 显示所有学生信息\n");
printf("2. 添加学生\n");
printf("3. 修改学生信息\n");
printf("4. 删除学生\n");
printf("5. 查找学生\n");
printf("6. 换寝室\n");
printf("7. 退出程序\n");
scanf("%d", &choice);
switch (choice) {
case 1:
print_students(head);
break;
case 2:
add_student(&head);
break;
case 3: {
char id[MAX_LEN];
printf("请输入学生学号:");
scanf("%s", id);
Student *student = find_student(head, id);
if (student) {
modify_student(student);
} else {
printf("没有找到该学生!\n");
}
break;
}
case 4: {
char id[MAX_LEN];
printf("请输入学生学号:");
scanf("%s", id);
delete_student(&head, id);
break;
}
case 5: {
int sub_choice;
printf("请选择查找方式:\n");
printf("1. 按学号查找\n");
printf("2. 按宿舍号查找\n");
printf("3. 按班级查找\n");
scanf("%d", &sub_choice);
switch (sub_choice) {
case 1: {
char id[MAX_LEN];
printf("请输入学生学号:");
scanf("%s", id);
Student *student = find_student(head, id);
if (student) {
print_student(student);
} else {
printf("没有找到该学生!\n");
}
break;
}
case 2: {
char dormitory[MAX_LEN];
printf("请输入宿舍号:");
scanf("%s", dormitory);
find_students_by_dormitory(head, dormitory);
break;
}
case 3: {
char class[MAX_LEN];
printf("请输入班级:");
scanf("%s", class);
find_students_by_class(head, class);
break;
}
default:
printf("无效的选项!\n");
break;
}
break;
}
case 6: {
char id[MAX_LEN];
printf("请输入学生学号:");
scanf("%s", id);
Student *student = find_student(head, id);
if (student) {
change_dormitory(student);
} else {
printf("没有找到该学生!\n");
}
break;
}
case 7:
write_students(head, "students.txt");
return 0;
default:
printf("无效的选项!\n");
break;
}
}
}
```
该程序中,使用链表来存储学生信息,使用文件来读取和保存学生信息,实现了添加、修改、删除、查找、换寝室等功能。
阅读全文