1. 影院票务管理系统设计与实现
作为一名有多年C语言开发经验的程序员,我想分享一下最近完成的一个影院票务管理系统的完整实现过程。这个系统采用纯C语言开发,使用链表数据结构管理各类数据,实现了用户管理、电影管理、座位管理和订单管理等核心功能。
1.1 系统架构设计
整个系统采用分层架构设计,主要分为以下几个模块:
- 实体层(Entity):定义系统核心数据结构
- 数据访问层(DAL):负责数据持久化和基础操作
- 业务逻辑层(BLL):处理核心业务逻辑
- 用户界面层(UI):提供用户交互界面
这种分层设计使得代码结构清晰,各模块职责明确,便于后期维护和扩展。
1.2 核心数据结构设计
系统使用链表管理各类数据,主要数据结构包括:
c复制// 用户结构体
typedef struct user {
int id;
char name[50];
char password[50];
int isAdmin;
char securityQuestion[100];
char securityAnswer[100];
struct user* next;
} user;
// 电影结构体
typedef struct movie {
int id;
char title[50];
float price;
char type[50];
MovieStatus status;
time_t startTime;
time_t endTime;
int duration;
char director[50];
char actor[100];
HallScene fixedHall;
struct movie* next;
} movie;
// 座位结构体
typedef struct seat {
int id;
int hallId;
int row;
int col;
time_t showDateTime;
SeatStatus status;
int orderId;
char remark[100];
time_t updateTime;
int updatedBy;
struct seat* next;
} seat;
// 订单结构体
typedef struct order {
int id;
int userId;
int movieId;
char movieName[50];
int quantity;
float totalPrice;
int hallId;
int timeIdx;
time_t showDateTime;
int row;
int col;
int seatId;
time_t timestamp;
char ticketCode[20];
int isDeleted;
time_t deleteTime;
char deleteReason[50];
float refundAmount;
struct order* next;
} order;
每个结构体都包含必要的字段和指向下一个节点的指针,形成链表结构。这种设计在内存使用上比较灵活,可以动态增删数据。
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
2. 核心功能实现细节
2.1 用户管理模块
用户管理模块实现了用户注册、登录、密码找回等功能。采用链表存储用户信息,使用头插法添加新用户。
2.1.1 用户注册实现
c复制void reg() {
clearScreen();
int admin;
printf("\n====================================\n");
printf(" 用户注册\n");
printf("====================================\n");
printf("0. 管理员\n1. 普通用户\n选择:");
scanf("%d", &admin);
char name[50];
printf("用户名: ");
scanf("%s", name);
user* p = findUserByName(name);
if (p != NULL) {
printf(" 用户名已存在!\n");
getchar(); getchar();
clearScreen();
return;
}
user* newuser = (user*)malloc(sizeof(user));
memset(newuser, 0, sizeof(user));
newuser->isAdmin = (admin == 0) ? 1 : 0;
int count = 0;
p = head;
while (p) { count++; p = p->next; }
newuser->id = count + 1;
char password[50];
printf("设置密码:");
inputPassword(password, 15);
printf("\n");
getchar();
printf("密保问题:");
fgets(newuser->securityQuestion, 100, stdin);
newuser->securityQuestion[strcspn(newuser->securityQuestion, "\n")] = 0;
printf("密保答案:");
fgets(newuser->securityAnswer, 100, stdin);
newuser->securityAnswer[strcspn(newuser->securityAnswer, "\n")] = 0;
strcpy(newuser->name, name);
strcpy(newuser->password, password);
newuser->next = head;
head = newuser;
printf(" 注册成功!\n");
save_to_file();
printf("1. 立即登录\n2. 稍后\n选择:");
int ch;
scanf("%d", &ch);
if (ch == 1) {
clearScreen();
login();
}
else {
getchar(); getchar();
clearScreen();
}
}
注册功能实现了:
- 用户类型选择(管理员/普通用户)
- 用户名查重
- 密码输入(使用安全输入函数)
- 密保问题设置
- 数据保存
2.1.2 用户登录实现
c复制void login() {
clearScreen();
char name[50];
char password[50];
int trycount = 0;
const int maxtry = 5;
user* p = NULL;
while (p == NULL) {
printf("\n===== 用户登录 =====\n");
printf("请输入用户名: ");
scanf("%s", name);
p = findUserByName(name);
if (p == NULL) {
printf(" 用户不存在!请重新输入用户名\n");
}
}
trycount = 0;
while (trycount < maxtry) {
printf("请输入密码: ");
inputPassword(password, 15);
printf("\n");
if (strcmp(p->password, password) == 0) {
printf(" 登录成功!\n");
currentUser = p;
clearScreen();
p->isAdmin ? adminmenu() : usermenu();
return;
}
else {
trycount++;
printf(" 密码错误!剩余 %d 次机会\n", maxtry - trycount);
}
}
printf(" 登录失败!密码错误次数过多\n");
while (getchar() != '\n');
clearScreen();
}
登录功能特点:
- 用户名验证
- 密码尝试次数限制(5次)
- 根据用户类型跳转不同菜单
- 密码输入使用安全函数,不显示明文
2.2 电影管理模块
电影管理模块实现了电影的增删改查功能,支持按状态分类显示(热映、即将上映、已下架)。
2.2.1 电影浏览功能
c复制void browseMovies() {
clearScreen();
time_t now = time(NULL);
printf("\n================== 电影列表 ==================\n\n");
// 正在热映(可购票)
printf("【正在热映】 可购票\n");
printf("------------------------------------------------\n");
int hotCount = 0;
for (movie* p = movieList; p; p = p->next) {
if (now >= p->startTime && now <= p->endTime &&
