图书馆座位管理系统c语言
时间: 2023-12-30 11:24:34 浏览: 96
以下是一个简单的C语言图书馆座位管理系统的示例:
```c
#include <stdio.h>
#define MAX_SEATS 100
// 座位状态
typedef enum {
EMPTY,
OCCUPIED
} SeatStatus;
// 座位结构体
typedef struct {
int seatNumber;
SeatStatus status;
char occupantName[50];
} Seat;
// 初始化座位
void initializeSeats(Seat seats[], int numSeats) {
for (int i = 0; i < numSeats; i++) {
seats[i].seatNumber = i + 1;
seats[i].status = EMPTY;
}
}
// 显示座位状态
void displaySeatStatus(Seat seats[], int numSeats) {
printf("座位状态:\n");
for (int i = 0; i < numSeats; i++) {
printf("座位号: %d\t状态: %s\t占用者姓名: %s\n", seats[i].seatNumber, seats[i].status == EMPTY ? "空闲" : "已占用", seats[i].occupantName);
}
}
// 预定座位
void reserveSeat(Seat seats[], int numSeats) {
int seatNumber;
char occupantName[50];
printf("请输入要预定的座位号: ");
scanf("%d", &seatNumber);
if (seatNumber < 1 || seatNumber > numSeats) {
printf("无效的座位号!\n");
return;
}
if (seats[seatNumber - 1].status == OCCUPIED) {
printf("该座位已被占用!\n");
return;
}
printf("请输入占用者姓名: ");
scanf("%s", occupantName);
seats[seatNumber - 1].status = OCCUPIED;
strcpy(seats[seatNumber - 1].occupantName, occupantName);
printf("座位预定成功!\n");
}
// 释放座位
void releaseSeat(Seat seats[], int numSeats) {
int seatNumber;
printf("请输入要释放的座位号: ");
scanf("%d", &seatNumber);
if (seatNumber < 1 || seatNumber > numSeats) {
printf("无效的座位号!\n");
return;
}
if (seats[seatNumber - 1].status == EMPTY) {
printf("该座位已是空闲状态!\n");
return;
}
seats[seatNumber - 1].status = EMPTY;
strcpy(seats[seatNumber - 1].occupantName, "");
printf("座位释放成功!\n");
}
int main() {
Seat seats[MAX_SEATS];
int numSeats;
printf("请输入座位数量: ");
scanf("%d", &numSeats);
initializeSeats(seats, numSeats);
int choice;
do {
printf("\n图书馆座位管理系统菜单:\n");
printf("1. 显示座位状态\n");
printf("2. 预定座位\n");
printf("3. 释放座位\n");
printf("0. 退出\n");
printf("请输入选项: ");
scanf("%d", &choice);
switch (choice) {
case 1:
displaySeatStatus(seats, numSeats);
break;
case 2:
reserveSeat(seats, numSeats);
break;
case 3:
releaseSeat(seats, numSeats);
break;
case 0:
printf("感谢使用图书馆座位管理系统!\n");
break;
default:
printf("无效的选项!\n");
break;
}
} while (choice != 0);
return 0;
}
```
阅读全文