📄 train_booking_trainop.c
字号:
// train_booking_trainop.c -- 包含列车数据操作函数
//
/////////////////////////////////////////////////////////////////////////////
#ifndef TRAIN_BOOKING_TRAINOP_C_
#define TRAIN_BOOKING_TRAINOP_C_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <time.h>
#include "train_booking_declare.h"
#include "train_booking_const.h"
#include "train_booking_unit.h"
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 检测某车厢里是否还有空闲软卧座位
//
// 函数参数: sbed_info_pt:指向单个软卧车厢结构体的指针
// begin:搜索区间起始数
// end:搜索区间结束数
// seat:成功返回搜索到的座位号,失败返回0
//
/////////////////////////////////////////////////////////////////////////////
status check_sbed_remained(sbed_info *sbed_info_pt, int begin, int end, int *seat)
{
int i, j, tseat = 0;
for (i = 0; i < DOT_PER_SB_CAR; i++) // 依次搜索每个座位是否满足条件
{
for (j = begin; j <= end; j++) // 搜索乘车区间是否已有人占座
{
if (1 == sbed_info_pt->info[i][j])
{
break; // 某个座位有人占座就放弃
}
}
if (j == end) // 判断是否全部区间都可坐,若是则相等
{
*seat = i + 1; // 返回找到的第一个满足条件的座位号
return OK;
}
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 检测某车厢里是否还有空闲硬卧座位
//
// 函数参数: hbed_info_pt:指向单个硬卧车厢结构体的指针
// begin:搜索区间起始数
// end:搜索区间结束数
// seat:成功返回搜索到的座位号,失败返回0
//
/////////////////////////////////////////////////////////////////////////////
status check_hbed_remained(sbed_info *hbed_info_pt, int begin, int end, int *seat)
{
int i, j, tseat = 0;
for (i = 0; i < DOT_PER_HB_CAR; i++) // 依次搜索每个座位是否满足条件
{
for (j = begin; j <= end; j++) // 搜索乘车区间是否已有人占座
{
if (1 == hbed_info_pt->info[i][j])
{
break; // 某个座位有人占座就放弃
}
}
if (j == end) // 判断是否全部区间都可坐,若是则相等
{
*seat = i + 1; // 返回找到的第一个满足条件的座位号
return OK;
}
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 检测某车厢里是否还有空闲硬座座位
//
// 函数参数: hseat_info_pt:指向单个硬座车厢结构体的指针
// begin:搜索区间起始数
// end:搜索区间结束数
// seat:成功返回搜索到的座位号,失败返回0
//
/////////////////////////////////////////////////////////////////////////////
status check_hseat_remained(sbed_info *hseat_info_pt, int begin, int end, int *seat)
{
int i, j, tseat = 0;
for (i = 0; i < DOT_PER_HS_CAR; i++) // 依次搜索每个座位是否满足条件
{
for (j = begin; j <= end; j++) // 搜索乘车区间是否已有人占座
{
if (1 == hseat_info_pt->info[i][j])
{
break; // 某个座位有人占座就放弃
}
}
if (j == end) // 判断是否全部区间都可坐,若是则相等
{
*seat = i + 1; // 返回找到的第一个满足条件的座位号
return OK;
}
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 计算每个车站的抵达时间
//
// 函数参数: target_tm:将计算结果存入此结构体
// start_tm:起始时间
// distance:路程
// speed:车速
//
/////////////////////////////////////////////////////////////////////////////
status get_train_reach_time(struct tm *target_tm, struct tm *start_tm,
int distance, int speed)
{
time_t std_start_tm, std_passed_tm, std_target_tm;
struct tm t_start_tm, t_target_tm;
struct tm *t_target_tm_pt = &t_target_tm;
double t_distance, t_speed;
t_target_tm = *target_tm;
t_start_tm = *start_tm;
// 将起始时间转换成标准时间
t_start_tm.tm_year = start_tm->tm_year - 1900;
t_start_tm.tm_mon = start_tm->tm_mon - 1;
std_start_tm = mktime(&t_start_tm);
// 计算行程所需时间(秒)转换为m/s计算,满足要求,提升精度
t_distance = distance * 1000;
t_speed = (speed * 1000) / 3600;
std_passed_tm = t_distance / t_speed;
// 将行程时间加上起始时间并转换为日历时间,并写入目标时间
std_target_tm = std_start_tm + std_passed_tm;
t_target_tm_pt = localtime(&std_target_tm);
t_target_tm.tm_year += 1900;
t_target_tm.tm_mon += 1;
*target_tm = t_target_tm;
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 转换时间结构体为时间标识字符串
//
// 函数参数: target_tm:将计算结果存入此结构体
// start_tm:起始时间
// distance:路程
// speed:车速
//
/////////////////////////////////////////////////////////////////////////////
status get_train_time_id(char *target_time_id, struct tm *time)
{
char t_id[10];
int tmp;
tmp = time->tm_year;
strcpy(t_id, "");
itoa(time->tm_year, t_id, 10); // 将年份转为字串
strcpy(target_time_id, t_id); // 复制年份
itoa(time->tm_mon, t_id, 10); // 将月份转为字串
if (time->tm_mon < 10) // 判断补0情况
{
strcat(target_time_id, "0");
}
strcat(target_time_id, t_id); // 连接月份串
itoa(time->tm_mday, t_id, 10); // 将日期转为字串
if (time->tm_mday < 10) // 判断补0情况
{
strcat(target_time_id, "0");
}
strcat(target_time_id, t_id); // 连接日期串
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 软卧车厢占座/退座
//
// 函数参数; sbed_info_adr:指向特定软卧车厢信息数组元素地址
// car_order:车厢序号,从0开始编号
// seat_order:座位号
// begin:占座区间起始号
// end:占座区间结束号
// optype:操作类型:1为占座,0为退座
//
// 函数说明: 1、内部使用函数,不用参数检测
// 2、因参数过多,不包含更新文件功能
//
/////////////////////////////////////////////////////////////////////////////
status deal_sb_seat(sbed_info *sbed_info_adr, int car_order,
int seat_order, int begin, int end, int optype)
{
int i;
for (i = begin; i <= end; i++)
{
sbed_info_adr[car_order].info[seat_order][i] = optype;
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 硬卧车厢占座/退座
//
// 函数参数; hbed_info_adr:指向特定硬卧车厢信息数组元素地址
// car_order:车厢序号,从0开始编号
// seat_order:座位号
// begin:占座区间起始号
// end:占座区间结束号
// optype:操作类型:1为占座,0为退座
//
// 函数说明: 1、内部使用函数,不用参数检测
// 2、因参数过多,不包含更新文件功能
//
/////////////////////////////////////////////////////////////////////////////
status deal_hb_seat(hbed_info *hbed_info_adr, int car_order,
int seat_order, int begin, int end, int optype)
{
int i;
for (i = begin; i <= end; i++)
{
hbed_info_adr[car_order].info[seat_order][i] = optype;
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 硬座车厢占座/退座
//
// 函数参数; hseat_info_adr:指向特定硬座车厢信息数组元素地址
// car_order:车厢序号,从0开始编号
// seat_order:座位号
// begin:占座区间起始号
// end:占座区间结束号
// optype:操作类型:1为占座,0为退座
//
// 函数说明: 1、内部使用函数,不用参数检测
// 2、因参数过多,不包含更新文件功能
//
/////////////////////////////////////////////////////////////////////////////
status deal_hs_seat(hseat_info *hseat_info_adr, int car_order,
int seat_order, int begin, int end, int optype)
{
int i;
for (i = begin; i <= end; i++)
{
hseat_info_adr[car_order].info[seat_order][i] = optype;
}
return OK;
}
/////////////////////////////////////////////////////////////////////////////
// 函数功能: 根据始、终点确定列车行程区间的开始、结束序号
//
// 函数参数: station_info_head:站点信息链表头结点指针
// begin_station:要搜索的起始站(注意不是始发站,而是区间起点)
// end_station:要搜索的结束站(不是终点站,而是区间末点)
// begin_order:返回起始站序号
// end_order:返回结束站序号
//
// 函数说明: 返回的序号可直接用于搜索车厢区间座位信息
//
/////////////////////////////////////////////////////////////////////////////
status get_span_order(station_info *station_info_head, char *begin_station,
char *end_station, int *begin_order, int *end_order)
{
station_info *station_info_head_reset;
char ch;
int tmp;
*begin_order = 0;
*end_order = 0;
station_info_head_reset = station_info_head; // 存储指针备用
// 如果没有车站
if (!(station_info_head->next))
{
message(ERROR, "这趟列车尚无站点信息!", NULL);
message(WARN, "确定列车行程开始结束序号失败!", WC);
return FAIL;
}
// 查找起始站的位置
while (station_info_head->next)
{
station_info_head = station_info_head->next;
// 如果找到匹配的起始站
if (!strcmp(begin_station, station_info_head->station_id))
{
break;
}
*begin_order++;
}
if (!(station_info_head->next))
{
message(ERROR, "该趟列车中没有您要查找的起始站!", NULL);
message(WARN, "确定列车行程开始号失败!", WC);
return FAIL;
}
station_info_head = station_info_head_reset; // 恢复指针
// 查找结束站点位置
while (station_info_head->next)
{
station_info_head = station_info_head->next;
// 如果找到匹配的起始站
if (!strcmp(end_station, station_info_head->station_id))
{
break;
}
*end_order++;
}
if (!(station_info_head->next))
{
message(ERROR, "该趟列车中没有您要查找的结束站!", NULL);
message(WARN, "确定列车行程结束号失败!", WC);
return FAIL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -