📄 train_booking_mgrop.c
字号:
// train_booking_mgrop.c -- 包含管理员管理系统操作函数
//
/////////////////////////////////////////////////////////////////////////////
#ifndef TRAIN_BOOKING_MGROP_C_
#define TRAIN_BOOKING_MGROP_C_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include "train_booking_declare.h"
#include "train_booking_const.h"
#include "train_booking_unit.h"
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 为新车站准备资料夹
//
/////////////////////////////////////////////////////////////////////////////
status prepare_system_dir(void)
{
// 准备系统信息资料夹
build_i_dir(SYS_INFO_DIR, NULL);
// 准备列车信息资料夹
build_i_dir(TRAIN_INFO_DIR, NULL);
// 准备用户信息资料夹
build_i_dir(USER_INFO_DIR, NULL);
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 为新车站准备空文件
//
/////////////////////////////////////////////////////////////////////////////
status prepare_system_file(void)
{
build_i_dir(SYS_INFO_DIR, SYS_INFO_FILE); // 系统信息文件
build_i_dir(SYS_INFO_DIR, SQ_INFO_FILE); // 车次信息文件
build_i_dir(SYS_INFO_DIR, PSNGR_ITEM_FILE); // 用户信息文件
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 开放站点
//
// 函数参数: system_info_pt:系统信息结构体指针
//
/////////////////////////////////////////////////////////////////////////////
status open_station(system_info *system_info_pt)
{
system_info_pt->open = 1;
if (save_system_info(system_info_pt))
{
message(TIP, "成功开放站点!", W);
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 关闭站点
//
// 函数参数: system_info_pt:系统信息结构体指针
//
/////////////////////////////////////////////////////////////////////////////
status close_station(system_info *system_info_pt)
{
system_info_pt->open = 0;
if (save_system_info(system_info_pt))
{
message(TIP, "成功关闭站点!", W);
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 锁定系统信息,防止共享冲突
//
// 函数参数: system_info_pt:系统信息结构体指针
//
/////////////////////////////////////////////////////////////////////////////
status lock_system(system_info *system_info_pt)
{
system_info_pt->locked = 1;
if (!(save_system_info(system_info_pt)))
{
message(ERROR, "锁定系统信息失败!", W);
return FAIL;
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 解锁系统信息,防止共享冲突
//
// 函数参数: system_info_pt:系统信息结构体指针
//
/////////////////////////////////////////////////////////////////////////////
status unlock_system(system_info *system_info_pt)
{
system_info_pt->locked = 0;
if (!(save_system_info(system_info_pt)))
{
message(ERROR, "解锁系统信息失败!", W);
return FAIL;
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 为站点重命名
//
// 函数参数: system_info_pt:系统信息结构体指针
//
/////////////////////////////////////////////////////////////////////////////
status name_station(system_info *system_info_pt)
{
actitle("重命名本地站点名称");
printf("【提示】原站点名为:(%s)\n\n", system_info_pt->location);
if (!(get_str("新站点名称", system_info_pt->location, 1, MAX_STATION_ID_LEN)))
{
CLS;
return FAIL;
}
if (save_system_info(system_info_pt))
{
message(INFO, "成功修改站点信息!", W);
}
else
{
message(ERROR, "修改站点信息失败!", W);
return FAIL;
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 设置每公里软卧票价
//
// 函数参数: system_info_pt:系统信息结构体指针
//
/////////////////////////////////////////////////////////////////////////////
status set_price_sbed_per_km(system_info *system_info_pt)
{
actitle("设置每公里软卧票价");
printf("【提示】原每公里软卧票价为 %d/km\n", system_info_pt->price_sbed_per_km);
if (!get_float(&system_info_pt->price_sbed_per_km, "新的每公里软卧票价"))
{
CLS;
return FAIL;
}
if (save_system_info(system_info_pt))
{
message(INFO, "成功修改站点每公里软卧票价!", W);
}
else
{
message(ERROR, "修改站点每公里软卧票价失败!", W);
return FAIL;
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 设置每公里硬卧票价
//
// 函数参数: system_info_pt:系统信息结构体指针
//
/////////////////////////////////////////////////////////////////////////////
status set_price_hbed_per_km(system_info *system_info_pt)
{
actitle("设置每公里硬卧票价");
printf("【提示】原每公里硬卧票价为 %d/km\n", system_info_pt->price_hbed_per_km);
if (!get_float(&system_info_pt->price_hbed_per_km, "新的每公里硬卧票价"))
{
CLS;
return FAIL;
}
if (save_system_info(system_info_pt))
{
message(INFO, "成功修改站点每公里硬卧票价!", W);
}
else
{
message(ERROR, "修改站点每公里硬卧票价失败!", W);
return FAIL;
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 设置每公里硬座票价
//
// 函数参数: system_info_pt:系统信息结构体指针
//
/////////////////////////////////////////////////////////////////////////////
status set_price_hseat_per_km(system_info *system_info_pt)
{
actitle("设置每公里硬座票价");
printf("【提示】原每公里硬座票价为 %d/km\n", system_info_pt->price_hseat_per_km);
if (!get_float(&system_info_pt->price_hseat_per_km, "新的每公里硬座票价"))
{
CLS;
return FAIL;
}
if (save_system_info(system_info_pt))
{
message(INFO, "成功修改站点每公里硬座票价!", W);
}
else
{
message(ERROR, "修改站点每公里硬座票价失败!", W);
return FAIL;
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 设置退票打折
//
// 函数参数: system_info_pt:系统信息结构体指针
//
/////////////////////////////////////////////////////////////////////////////
status set_bounce_off_ratio(system_info *system_info_pt)
{
actitle("设置旅客退票打折率");
printf("【提示】原旅客退票打折率为 %d/km\n", system_info_pt->bounce_off);
if (!get_float(&system_info_pt->bounce_off, "新的旅客退票打折率"))
{
CLS;
return FAIL;
}
if (save_system_info(system_info_pt))
{
message(INFO, "成功修改旅客退票打折率!", W);
}
else
{
message(ERROR, "修改旅客退票打折率失败!", W);
return FAIL;
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 设置特殊旅客购票打折率
//
// 函数参数: system_info_pt:系统信息结构体指针
//
/////////////////////////////////////////////////////////////////////////////
status set_special_off_ratio(system_info *system_info_pt)
{
actitle("设置特殊旅客购票打折率");
printf("【提示】原特殊旅客购票打折率为 %d/km\n", system_info_pt->special_off);
if (!get_float(&system_info_pt->special_off, "新的特殊旅客购票打折率"))
{
CLS;
return FAIL;
}
if (save_system_info(system_info_pt))
{
message(INFO, "成功修改特殊旅客购票打折率!", W);
}
else
{
message(ERROR, "修改特殊旅客购票打折率失败!", W);
return FAIL;
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 添加新的车次
//
// 函数参数: system_info_pt:指向系统信息结构体的指针
//
// 函数说明: 本函数要求系统信息结构体已链上车次信息链表完整
//
/////////////////////////////////////////////////////////////////////////////
status train_sq_add(system_info *system_info_pt)
{
char t_sq_id[MIN_TRING_ID_LEN * 2];
char filepath[128];
train_sq_info *train_sq_info_head, *train_sq_info_pt;
FILE *fp;
int t_days_per_train;
actitle("添加新的车次");
if (!get_str("新的车次名", t_sq_id, 4, MIN_TRING_ID_LEN))
{
message(WARN, "添加新的车次失败!", WC);
return FAIL;
}
// 验证该车次是否已经存在
create_i_dir(filepath, SYS_INFO_DIR, SQ_INFO_FILE);
if (!(fp = fopen(filepath, "rb")))
{
message(ERROR, "验证车次信息时打开文件失败!", NULL);
message(WARN, "添加新的车次信息失败!", WC);
return FAIL;
}
fclose(fp);
train_sq_info_head = &system_info_pt->train_sq_list; // 取得车次信息链表头指针
while (train_sq_info_head->next) // 开始检测
{
if (!(strcmp(t_sq_id, train_sq_info_head->train_sq_id)))
{
printf("【错误】车次名(%s)已存在,不能重复添加相同的车次!\n", t_sq_id);
message(TIP, "您可加从“添加列车”功能向该车次添加列车。", NULL);
message(WARN, "添加新车次失败!", WC);
return FAIL;
}
train_sq_info_head = train_sq_info_head->next;
}
if (!(train_sq_info_pt = (train_sq_info *)malloc(sizeof(train_sq_info))))
{
message(ERROR, "添加新车次时申请内存失败!", NULL);
message(WARN, "添加新车次失败!", WC);
return FAIL;
}
if (!get_int("该车次运营列车间隔天数", t_days_per_train, 1, 4))
{
message(WARN, "添加新的车次失败!", WC);
return FAIL;
}
// 向系统注册此新加的车次信息
train_sq_info_pt->running_amount = 0;
train_sq_info_pt->running_amount_valid = 0;
train_sq_info_pt->days_per_train = t_days_per_train;
train_sq_info_pt->offset = system_info_pt->total_train_sq - 1;
strcpy(train_sq_info_pt->train_sq_id, t_sq_id);
strcpy(train_sq_info_pt->master, system_info_pt->location);
save_train_sq_info(train_sq_info_pt); // 将车次信息写入文件
// 以下几步的目的是为了能让添加的车次马上能发挥作用
train_sq_info_head = &system_info_pt->train_sq_list; // 取得车次信息链表头指针
while (train_sq_info_head->next) // 取得尾部指针
{
train_sq_info_head = train_sq_info_head->next;
}
train_sq_info_head->next = train_sq_info_pt; // 链上车次链表尾
// 更新系统信息
system_info_pt->total_train_sq++;
save_system_info(system_info_pt); // 将系统信息写入文件
build_ii_dir(TRAIN_INFO_DIR, t_sq_id, NULL); // 为新的车次准备资料夹和空文件
message(INFO, "添加新的车次成功!", NULL);
printf("【信息】您已经成功向系统添加了新的车次(%s)信息!\n", t_sq_id);
message(INFO, "以后您就可以在这个车次中添加/删除/编辑列车信息了!", WC);
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 从文件为某车次增加新的列车
//
// 函数参数: train_sq_info_head:已存列车信息链表头结点指针
//
// 函数说明: 1、具体输入格式要求样例请看 sample.txt 文件
// 2、每个文本文件限定只输入一趟列车信息(包括所有车站)
// 3、某些敏感信息不由文件输入
//
/////////////////////////////////////////////////////////////////////////////
status train_add_file(train_sq_info *train_sq_info_head)
{
char t_sq_id[MAX_TRAIN_ID_LEN * 3];
train_sq_info *train_sq_info_head_reset;
train_info *train_info_pt;
station_info *station_info_head, *station_info_pt;
char train_time_id[16];
FILE *fp;
char filepath[128];
int i, j, k, tmp = 0;
struct tm current_time;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -