用C语言编写一个图书馆占座管理系统
时间: 2023-08-20 07:05:59 浏览: 102
好的,我将为您提供一个简单的图书馆占座管理系统的C语言代码,仅供参考。
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_USER 100
#define MAX_SEAT 50
#define MAX_NAME_LEN 20
typedef struct {
int seat_id;
char user_name[MAX_NAME_LEN];
int start_time; // 占座开始时间
int end_time; // 占座结束时间
} Seat;
typedef struct {
char user_name[MAX_NAME_LEN];
char password[MAX_NAME_LEN];
} User;
int login(User users[], int user_num) { // 登录函数
char name[MAX_NAME_LEN];
char password[MAX_NAME_LEN];
printf("请输入用户名:\n");
scanf("%s", name);
printf("请输入密码:\n");
scanf("%s", password);
for (int i = 0; i < user_num; i++) {
if (strcmp(users[i].user_name, name) == 0 && strcmp(users[i].password, password) == 0) {
printf("登录成功!\n");
return i; // 返回用户在数组中的索引
}
}
printf("用户名或密码错误!\n");
return -1;
}
void view_seats(Seat seats[], int seat_num) { // 查看座位状态函数
printf("座位状态如下:\n");
for (int i = 0; i < seat_num; i++) {
printf("座位%d:", seats[i].seat_id);
if (seats[i].user_name[0] != '\0') {
printf("%s占用,占座时间:%d~%d\n", seats[i].user_name, seats[i].start_time, seats[i].end_time);
} else {
printf("空闲\n");
}
}
}
void reserve_seat(Seat seats[], int seat_num, int user_index) { // 预约座位函数
int seat_id;
printf("请输入要预约的座位号:\n");
scanf("%d", &seat_id);
for (int i = 0; i < seat_num; i++) {
if (seats[i].seat_id == seat_id) {
if (seats[i].user_name[0] != '\0') { // 座位已被占用
printf("该座位已被%s占用,占座时间:%d~%d\n", seats[i].user_name, seats[i].start_time, seats[i].end_time);
return;
} else { // 预约座位
printf("预约座位成功!\n");
strcpy(seats[i].user_name, users[user_index].user_name);
printf("请输入占座开始时间:\n");
scanf("%d", &seats[i].start_time);
printf("请输入占座结束时间:\n");
scanf("%d", &seats[i].end_time);
return;
}
}
}
printf("座位号不存在!\n");
}
void cancel_reserve(Seat seats[], int seat_num, int user_index) { // 取消预约函数
int seat_id;
printf("请输入要取消预约的座位号:\n");
scanf("%d", &seat_id);
for (int i = 0; i < seat_num; i++) {
if (seats[i].seat_id == seat_id) {
if (strcmp(seats[i].user_name, users[user_index].user_name) == 0) { // 取消预约
printf("取消预约成功!\n");
seats[i].user_name[0] = '\0';
seats[i].start_time = 0;
seats[i].end_time = 0;
return;
} else { // 该座位不是该用户预约的
printf("该座位不是您预约的!\n");
return;
}
}
}
printf("座位号不存在!\n");
}
int main() {
User users[MAX_USER] = {{"user1", "123456"}, {"user2", "123456"}}; // 用户名和密码
Seat seats[MAX_SEAT];
int user_num = 2;
int seat_num = 10;
for (int i = 0; i < seat_num; i++) {
seats[i].seat_id = i + 1;
seats[i].user_name[0] = '\0';
seats[i].start_time = 0;
seats[i].end_time = 0;
}
int user_index = login(users, user_num); // 用户登录
if (user_index == -1) {
return 0;
}
while (1) {
printf("请选择操作:\n1、查看座位状态\n2、预约座位\n3、取消预约\n4、退出\n");
int choice;
scanf("%d", &choice);
switch (choice) { // 根据用户选择进行相应操作
case 1:
view_seats(seats, seat_num);
break;
case 2:
reserve_seat(seats, seat_num, user_index);
break;
case 3:
cancel_reserve(seats, seat_num, user_index);
break;
case 4:
printf("退出系统!\n");
return 0;
default:
printf("输入有误,请重新输入!\n");
break;
}
}
return 0;
}
```
以上代码仅为示例,实际开发中还需根据具体需求进行修改和完善。
阅读全文