c语言代码,图书馆自习位预约系统更加精确
时间: 2023-09-23 18:11:07 浏览: 37
以下是一个简单的C语言代码,可以实现图书馆自习位预约系统的更加精确:
```c
#include <stdio.h>
#include <stdbool.h>
#define MAX_SEATS 100
#define MAX_TIME 24
bool seats[MAX_TIME][MAX_SEATS];
void displayMenu();
void reserveSeat();
void cancelReservation();
void displaySeats();
int main() {
int choice;
do {
displayMenu();
scanf("%d", &choice);
switch(choice) {
case 1:
reserveSeat();
break;
case 2:
cancelReservation();
break;
case 3:
displaySeats();
break;
case 4:
printf("Thank you for using our reservation system!\n");
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
} while (choice != 4);
return 0;
}
void displayMenu() {
printf("\nWelcome to the Library Seat Reservation System!\n");
printf("1. Reserve a seat\n");
printf("2. Cancel a reservation\n");
printf("3. Display available seats\n");
printf("4. Exit\n");
printf("Enter your choice (1-4): ");
}
void reserveSeat() {
int time, seat;
printf("\nEnter the desired time slot (0-23): ");
scanf("%d", &time);
if (time < 0 || time >= MAX_TIME) {
printf("Invalid time slot! Please try again.\n");
return;
}
printf("Enter the desired seat number (1-%d): ", MAX_SEATS);
scanf("%d", &seat);
if (seat < 1 || seat > MAX_SEATS) {
printf("Invalid seat number! Please try again.\n");
return;
}
if (seats[time][seat-1]) {
printf("Sorry, this seat is already reserved! Please choose another one.\n");
return;
}
seats[time][seat-1] = true;
printf("Seat %d at time slot %d has been reserved!\n", seat, time);
}
void cancelReservation() {
int time, seat;
printf("\nEnter the time slot (0-23) of the reservation to be cancelled: ");
scanf("%d", &time);
if (time < 0 || time >= MAX_TIME) {
printf("Invalid time slot! Please try again.\n");
return;
}
printf("Enter the seat number (1-%d) of the reservation to be cancelled: ", MAX_SEATS);
scanf("%d", &seat);
if (seat < 1 || seat > MAX_SEATS) {
printf("Invalid seat number! Please try again.\n");
return;
}
if (!seats[time][seat-1]) {
printf("Sorry, this seat was not reserved at this time slot!\n");
return;
}
seats[time][seat-1] = false;
printf("Reservation for seat %d at time slot %d has been cancelled!\n", seat, time);
}
void displaySeats() {
printf("\nAvailable seats:\n");
printf("Time Slot:\t");
for (int i = 0; i < MAX_TIME; i++) {
printf("%d\t", i);
}
printf("\n");
for (int j = 0; j < MAX_SEATS; j++) {
printf("Seat %d:\t", j+1);
for (int i = 0; i < MAX_TIME; i++) {
printf("%c\t", seats[i][j] ? 'X' : '_');
}
printf("\n");
}
}
```
这个程序使用了一个二维布尔数组`seats`,表示每个时间和座位是否已被预约。`reserveSeat()`函数用于预约座位,`cancelReservation()`函数用于取消预约,`displaySeats()`函数用于显示所有可用的座位。