图书馆占座系统带预约时间的c语言
时间: 2023-12-22 21:05:14 浏览: 100
以下是一个简单的图书馆占座系统带预约时间的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_SEATS 10
#define MAX_NAME_LEN 20
typedef struct {
char name[MAX_NAME_LEN];
time_t reserved_time;
} Seat;
Seat seats[NUM_SEATS];
void print_seats() {
printf("Current seat reservations:\n");
for (int i = 0; i < NUM_SEATS; i++) {
if (seats[i].reserved_time == 0) {
printf("Seat %d: vacant\n", i+1);
} else {
printf("Seat %d: reserved by %s until %s", i+1, seats[i].name, ctime(&seats[i].reserved_time));
}
}
}
int main() {
char input[MAX_NAME_LEN];
int seat_num;
// initialize seats
for (int i = 0; i < NUM_SEATS; i++) {
seats[i].reserved_time = 0;
}
// main loop
while (1) {
print_seats();
// get user input
printf("Enter your name to reserve a seat (or 'quit' to exit): ");
fgets(input, MAX_NAME_LEN, stdin);
if (strcmp(input, "quit\n") == 0) {
break;
}
input[strlen(input)-1] = '\0'; // remove newline character
printf("Enter seat number to reserve (1-%d): ", NUM_SEATS);
scanf("%d", &seat_num);
getchar(); // consume newline character
// check if seat is already reserved
if (seats[seat_num-1].reserved_time != 0) {
printf("Sorry, seat %d is already reserved.\n", seat_num);
continue;
}
// reserve the seat
seats[seat_num-1].reserved_time = time(NULL) + 60*30; // reserve for 30 minutes
strcpy(seats[seat_num-1].name, input);
printf("Seat %d reserved by %s.\n", seat_num, input);
}
printf("Goodbye!\n");
return 0;
}
```
该程序使用了一个简单的结构体来表示座位,其中包含了座位预约者的姓名和预约时间。程序提供了一个 `print_seats()` 函数来打印当前的座位预约情况。在主循环中,程序会要求用户输入姓名和座位号来进行预约。程序会检查该座位是否已经被预约,如果是,则提示用户重新选择。如果该座位尚未被预约,则程序会将该座位的预约时间设置为当前时间加上30分钟,并将预约者的姓名保存在座位数据结构中。程序会一直循环直到用户输入 `quit` 为止。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.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)