用C语言编写一个学生宿舍管理系统
时间: 2023-09-19 10:11:12 浏览: 125
好的,下面是一个简单的学生宿舍管理系统的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_NAME_LEN 20
typedef struct {
char name[MAX_NAME_LEN];
int room_num;
} Student;
Student students[MAX_STUDENTS];
int num_students = 0;
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("Error: Maximum number of students reached.\n");
return;
}
printf("Enter student name: ");
scanf("%s", students[num_students].name);
printf("Enter room number: ");
scanf("%d", &students[num_students].room_num);
num_students++;
printf("Student added.\n");
}
void remove_student() {
char name[MAX_NAME_LEN];
printf("Enter student name: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
found = 1;
for (int j = i; j < num_students - 1; j++) {
strcpy(students[j].name, students[j+1].name);
students[j].room_num = students[j+1].room_num;
}
num_students--;
printf("Student removed.\n");
break;
}
}
if (!found) {
printf("Error: Student not found.\n");
}
}
void list_students() {
printf("Student list:\n");
for (int i = 0; i < num_students; i++) {
printf("%s\t%d\n", students[i].name, students[i].room_num);
}
}
int main() {
int choice;
do {
printf("\n1. Add student\n");
printf("2. Remove student\n");
printf("3. List students\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: add_student(); break;
case 2: remove_student(); break;
case 3: list_students(); break;
case 4: break;
default: printf("Error: Invalid choice.\n");
}
} while (choice != 4);
return 0;
}
```
这个程序包括添加、删除和列出学生的功能。你可以根据实际需要进行修改和扩展。
阅读全文