多媒体教室借用系统的c程序代码
时间: 2023-12-22 07:04:58 浏览: 40
以下是一个简单的多媒体教室借用系统的 C 程序代码,它可以帮助您进行参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_ROOMS 10
#define MAX_NAME_LENGTH 50
#define MAX_TIME_SLOTS 5
typedef struct {
int id;
char name[MAX_NAME_LENGTH];
int time_slots[MAX_TIME_SLOTS];
} Room;
Room rooms[MAX_ROOMS];
void init_rooms() {
for (int i = 0; i < MAX_ROOMS; i++) {
rooms[i].id = i + 1;
sprintf(rooms[i].name, "Room %d", i + 1);
for (int j = 0; j < MAX_TIME_SLOTS; j++) {
rooms[i].time_slots[j] = 0;
}
}
}
void print_rooms() {
printf("Room\tName\t\tTime Slots\n");
for (int i = 0; i < MAX_ROOMS; i++) {
printf("%d\t%s\t", rooms[i].id, rooms[i].name);
for (int j = 0; j < MAX_TIME_SLOTS; j++) {
printf("%d ", rooms[i].time_slots[j]);
}
printf("\n");
}
}
void reserve_room(int room_id, int time_slot) {
if (room_id < 1 || room_id > MAX_ROOMS) {
printf("Invalid room ID.\n");
return;
}
if (time_slot < 1 || time_slot > MAX_TIME_SLOTS) {
printf("Invalid time slot.\n");
return;
}
if (rooms[room_id - 1].time_slots[time_slot - 1] != 0) {
printf("The room is already reserved at this time.\n");
return;
}
rooms[room_id - 1].time_slots[time_slot - 1] = 1;
printf("Room %d is reserved at time slot %d.\n", room_id, time_slot);
}
void release_room(int room_id, int time_slot) {
if (room_id < 1 || room_id > MAX_ROOMS) {
printf("Invalid room ID.\n");
return;
}
if (time_slot < 1 || time_slot > MAX_TIME_SLOTS) {
printf("Invalid time slot.\n");
return;
}
if (rooms[room_id - 1].time_slots[time_slot - 1] == 0) {
printf("The room is not reserved at this time.\n");
return;
}
rooms[room_id - 1].time_slots[time_slot - 1] = 0;
printf("Room %d is released at time slot %d.\n", room_id, time_slot);
}
int main() {
init_rooms();
int choice = 0;
while (1) {
printf("\n1. Reserve a room\n");
printf("2. Release a room\n");
printf("3. Display all rooms\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
int room_id, time_slot;
printf("Enter the room ID (1-%d): ", MAX_ROOMS);
scanf("%d", &room_id);
printf("Enter the time slot (1-%d): ", MAX_TIME_SLOTS);
scanf("%d", &time_slot);
reserve_room(room_id, time_slot);
break;
}
case 2: {
int room_id, time_slot;
printf("Enter the room ID (1-%d): ", MAX_ROOMS);
scanf("%d", &room_id);
printf("Enter the time slot (1-%d): ", MAX_TIME_SLOTS);
scanf("%d", &time_slot);
release_room(room_id, time_slot);
break;
}
case 3: {
print_rooms();
break;
}
case 4: {
printf("Exiting...\n");
exit(0);
}
default: {
printf("Invalid choice.\n");
break;
}
}
}
return 0;
}
```
这个程序包含了多个函数,其中 `init_rooms()` 函数初始化了所有的房间,`print_rooms()` 函数打印了所有的房间信息,`reserve_room()` 函数用于预约房间,`release_room()` 函数用于释放房间。在 `main()` 函数中,通过一个简单的菜单来调用这些函数。您可以根据您的具体需求进行修改和扩展。