用c语言写图书馆座位预约管理系统
时间: 2023-10-13 22:07:03 浏览: 149
以下是一个用 C 语言实现的简单图书馆座位预约管理系统示例:
```c
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#define MAX_SEATS 100 // 最多可预约的座位数
#define MAX_RESERVATIONS 1000 // 最多预约记录数
struct Reservation {
int seat; // 座位号
char date[11]; // 预约日期,格式为 YYYY-MM-DD
char time[6]; // 预约时间,格式为 HH:MM
};
int seats[MAX_SEATS]; // 可预约的座位号
struct Reservation reservations[MAX_RESERVATIONS]; // 预约记录数组
int num_reservations = 0; // 预约记录数
bool isValidSeat(int seat) {
for (int i = 0; i < MAX_SEATS; i++) {
if (seats[i] == seat) {
return true;
}
}
return false;
}
bool isValidDate(char *date) {
struct tm tm;
if (strptime(date, "%Y-%m-%d", &tm) == NULL) {
return false;
}
time_t t = mktime(&tm);
return t != -1;
}
bool isValidTime(char *time) {
struct tm tm;
if (strptime(time, "%H:%M", &tm) == NULL) {
return false;
}
return true;
}
bool isReserved(int seat, char *date, char *time) {
for (int i = 0; i < num_reservations; i++) {
struct Reservation r = reservations[i];
if (r.seat == seat && strcmp(r.date, date) == 0) {
if (strcmp(r.time, time) == 0) {
return true;
}
}
}
return false;
}
void reserveSeat(int seat, char *date, char *time) {
struct Reservation r;
r.seat = seat;
strcpy(r.date, date);
strcpy(r.time, time);
reservations[num_reservations] = r;
num_reservations++;
printf("预约成功!\n");
}
void cancelReservation(int index) {
for (int i = index; i < num_reservations - 1; i++) {
reservations[i] = reservations[i + 1];
}
num_reservations--;
printf("取消预约成功!\n");
}
void printReservations() {
printf("预约记录如下:\n");
for (int i = 0; i < num_reservations; i++) {
struct Reservation r = reservations[i];
printf("座位号:%d,预约日期:%s,预约时间:%s\n", r.seat, r.date, r.time);
}
}
int main() {
// 初始化可预约的座位号
for (int i = 0; i < 5; i++) {
seats[i] = i + 1;
}
while (true) {
printf("请输入操作代码(1:预约座位,2:取消预约,3:查询预约记录,0:退出):");
int code;
scanf("%d", &code);
switch (code) {
case 1:
printf("请输入座位号:");
int seat;
scanf("%d", &seat);
if (!isValidSeat(seat)) {
printf("座位号错误!\n");
break;
}
printf("请输入预约日期(格式为 YYYY-MM-DD):");
char date[11];
scanf("%s", date);
if (!isValidDate(date)) {
printf("日期格式错误!\n");
break;
}
printf("请输入预约时间(格式为 HH:MM):");
char time[6];
scanf("%s", time);
if (!isValidTime(time)) {
printf("时间格式错误!\n");
break;
}
if (isReserved(seat, date, time)) {
printf("该时间段已被预约!\n");
break;
}
reserveSeat(seat, date, time);
break;
case 2:
printf("请输入要取消的预约记录编号:");
int index;
scanf("%d", &index);
if (index < 1 || index > num_reservations) {
printf("预约记录编号错误!\n");
break;
}
cancelReservation(index - 1);
break;
case 3:
printReservations();
break;
case 0:
return 0;
default:
printf("操作代码错误!\n");
break;
}
}
}
```
该代码实现了一个简单的图书馆座位预约管理系统,运行后可以按照提示进行座位预约、预约取消和预约记录查询等操作。需要注意的是,该代码只实现了最基本的功能,实际应用中还需要考虑更多的情况,例如用户身份认证、座位状态查询等。
阅读全文