写一个C语言学生宿舍管理系统
时间: 2023-12-29 11:23:39 浏览: 47
好的,以下是一个简单的学生宿舍管理系统的C语言代码实现,供您参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 50
#define MAX_NAME_LEN 20
#define MAX_ROOM_NUM 5
typedef struct Student {
char name[MAX_NAME_LEN];
int room_num;
} Student;
void add_student(Student* students, int* num_students) {
if (*num_students >= MAX_STUDENTS) {
printf("Error: Too many students.\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 successfully.\n");
}
void remove_student(Student* students, int* num_students) {
char name[MAX_NAME_LEN];
printf("Enter student name: ");
scanf("%s", name);
for (int i = 0; i < *num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
for (int j = i + 1; j < *num_students; j++) {
students[j - 1] = students[j];
}
(*num_students)--;
printf("Student removed successfully.\n");
return;
}
}
printf("Error: Student not found.\n");
}
void display_students(Student* students, int num_students) {
if (num_students == 0) {
printf("No students in the system.\n");
return;
}
printf("Students:\n");
for (int i = 0; i < num_students; i++) {
printf("%s - Room %d\n", students[i].name, students[i].room_num);
}
}
void display_rooms(Student* students, int num_students) {
int room_counts[MAX_ROOM_NUM] = {0};
for (int i = 0; i < num_students; i++) {
room_counts[students[i].room_num - 1]++;
}
printf("Room counts:\n");
for (int i = 0; i < MAX_ROOM_NUM; i++) {
printf("Room %d: %d\n", i + 1, room_counts[i]);
}
}
int main() {
Student students[MAX_STUDENTS];
int num_students = 0;
while (1) {
printf("\nMenu:\n");
printf("1. Add student\n");
printf("2. Remove student\n");
printf("3. Display students\n");
printf("4. Display room counts\n");
printf("5. Quit\n");
int choice;
printf("\nEnter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(students, &num_students);
break;
case 2:
remove_student(students, &num_students);
break;
case 3:
display_students(students, num_students);
break;
case 4:
display_rooms(students, num_students);
break;
case 5:
printf("Goodbye!\n");
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}
```
这个系统可以实现添加学生、删除学生、显示学生和显示房间数量的功能。当然,这只是一个简单的实现,您可以根据自己的需求进行修改和扩展。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)