From 05230bc0da30fffd9ec531173f7b32ca2292da77 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 6 May 2022 11:55:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E6=A6=82=E8=A6=81=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- include/bus.h | 22 +++++++++++++ include/controller.h | 37 ++++++++++++++++++++++ include/define.h | 13 ++++++++ include/io.h | 52 +++++++++++++++++++++++++++++++ include/query.h | 74 ++++++++++++++++++++++++++++++++++++++++++++ include/rail.h | 14 +++++++++ src/controller.c | 4 +++ src/io.c | 4 +++ src/query.c | 8 +++++ src/rail.c | 4 ++- 11 files changed, 232 insertions(+), 2 deletions(-) create mode 100644 include/controller.h create mode 100644 include/define.h create mode 100644 include/io.h create mode 100644 include/query.h create mode 100644 src/controller.c create mode 100644 src/io.c create mode 100644 src/query.c diff --git a/README.md b/README.md index 70d53f6..9dc71cf 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ auto_pilot_bus | +-include // 头文件文件夹 | -+-lib // 源代码文件夹 ++-src // 源代码文件夹 | +-main.c // 程序入口 +-CMakeLists.txt // CMake文件 diff --git a/include/bus.h b/include/bus.h index 431b541..7ae4291 100644 --- a/include/bus.h +++ b/include/bus.h @@ -1,7 +1,9 @@ #ifndef AUTO_PILOT_BUS_BUS_H #define AUTO_PILOT_BUS_BUS_H +#include "define.h" #include "rail.h" +#include "query.h" struct { /** @@ -19,4 +21,24 @@ struct { */ typedef struct bus bus_t; +/** + * 每个时刻使公交车前进 + * @param rails 轨道链表 + * @param direction 公交车前进的方向 + * @return 公交车是否到达站点 + */ +int RunBus(rail_node_t* rails, int direction); + +/** + * 处理上车请求 + * @param rail_node 当前所在的站点 + */ +void UpBus(rail_node_t* rail_node); + +/** + * 处理下车请求 + * @param rail_node 当前所在的站点 + */ +void DownBus(rail_node_t* rail_node); + #endif //AUTO_PILOT_BUS_BUS_H diff --git a/include/controller.h b/include/controller.h new file mode 100644 index 0000000..e6096f9 --- /dev/null +++ b/include/controller.h @@ -0,0 +1,37 @@ +// +// Created by ricardo on 2022/5/6. +// + +#ifndef AUTO_PILOT_BUS_CONTROLLER_H +#define AUTO_PILOT_BUS_CONTROLLER_H +#include "rail.h" +#include "query.h" + +/** + * 先来先服务的策略控制函数 + * @param rails 轨道链表 + * @param up_queries 上车请求队列 + * @param down_queries 下车请求队列 + * @return 车前进的方向 + */ +int FCFSFunction(rail_node_t rails, up_bus_t up_queries, down_bus_t down_queries); + +/** + * 最短寻找时间优先的策略控制函数 + * @param rails 轨道链表 + * @param up_queries 上车请求队列 + * @param down_queries 下车请求队列 + * @return 车前进的方向 + */ +int SSTFFunction(rail_node_t rails, up_bus_t up_queries, down_bus_t down_queries); + +/** + * 顺便服务的策略控制函数 + * @param rails 轨道链表 + * @param up_queries 上车请求队列 + * @param down_queries 下车请求队列 + * @return 车前进的方向 + */ +int SCANFunction(rail_node_t rails, up_bus_t up_queries, down_bus_t down_queries); + +#endif //AUTO_PILOT_BUS_CONTROLLER_H diff --git a/include/define.h b/include/define.h new file mode 100644 index 0000000..f66d79c --- /dev/null +++ b/include/define.h @@ -0,0 +1,13 @@ +// +// Created by ricardo on 2022/5/6. +// + +#ifndef AUTO_PILOT_BUS_DEFINE_H +#define AUTO_PILOT_BUS_DEFINE_H + +#define CLOCK_WISE 0 +#define COUNTER_CLOCK_WISE 1 +#define TRUE 1 +#define FAlSE 0 + +#endif //AUTO_PILOT_BUS_DEFINE_H diff --git a/include/io.h b/include/io.h new file mode 100644 index 0000000..435fd5a --- /dev/null +++ b/include/io.h @@ -0,0 +1,52 @@ +// +// Created by ricardo on 2022/5/6. +// + +#ifndef AUTO_PILOT_BUS_IO_H +#define AUTO_PILOT_BUS_IO_H +#include "rail.h" +#include "query.h" + +enum read_state{ + /** + * 时钟 + */ + clock = 0, + /** + * 读取中 + */ + reading, + /** + * 结束 + */ + end +}; + +/** + * 读取输入的状态 + */ +typedef enum read_state read_state_t; + +/** + * 读取配置文件,创建轨道链表,同时读取需要使用的策略 + * @return 指向轨道链表的指针 + */ +rail_node_t* ReadConfigFile(); + +/** + * 读取标准输入流中的输入 + * @param up_queries 上车的请求队列 + * @param down_queries 下车的请求队列 + * @return 当前读取的状态 + */ +read_state_t ReadInput(up_bus_t* up_queries, down_bus_t* down_queries); + +/** + * 打印当前的状态 + * @param up_queries 上车的请求队列 + * @param down_queries 下车的请求队列 + * @param rails 轨道链表 + */ +void PrintState(up_bus_t* up_queries, down_bus_t* down_queries, rail_node_t* rails); + +#endif //AUTO_PILOT_BUS_IO_H diff --git a/include/query.h b/include/query.h new file mode 100644 index 0000000..bc1c6da --- /dev/null +++ b/include/query.h @@ -0,0 +1,74 @@ +// +// Created by ricardo on 2022/5/6. +// + +#ifndef AUTO_PILOT_BUS_QUERY_H +#define AUTO_PILOT_BUS_QUERY_H +#include "rail.h" + +struct { + /** + * 请求的序号 + */ + int id; + /** + * 请求前往的方向 + */ + int direction; + /** + * 请求上车的地点 + */ + rail_node_t* target; + struct up_bus* next; +} up_bus; + +struct { + /** + * 请求的序号 + */ + int id; + /** + * 请求下车的地点 + */ + rail_node_t* target; + struct down_bus* next; +} down_bus; + +/** + * 表示上车请求的结构体 + */ +typedef struct up_bus up_bus_t; +/** + * 表示下车请求的结构体 + */ +typedef struct down_bus down_bus_t; + +extern up_bus_t *up_queries; +extern down_bus_t *down_queries; + +/** + * 创建一个上车请求 + * @param target 上车的地点 + * @param direction 需要前往的方向 + */ +void CreateUpBusQuery(rail_node_t* target, int direction); + +/** + * 删除一个上车请求 + * @param id 需要删除的请求编号 + */ +void DeleteUpBusQuery(int id); + +/** + * 创建一个下车请求 + * @param target 需要下车的地点 + */ +void CreateDownBusQuery(rail_node_t *target); + +/** + * 删除一个下车请求 + * @param id 需要删除的请求编号 + */ +void DeleteDownBusQuery(int id); + +#endif //AUTO_PILOT_BUS_QUERY_H diff --git a/include/rail.h b/include/rail.h index d49e0a6..2878939 100644 --- a/include/rail.h +++ b/include/rail.h @@ -1,5 +1,6 @@ #ifndef AUTO_PILOT_BUS_RAIL_H #define AUTO_PILOT_BUS_RAIL_H +#include "bus.h" struct { /** @@ -14,6 +15,10 @@ struct { * 距离下一个站点的距离 */ int next_node_distance; + /** + * 指向公交车的指针 + */ + bus_t* bus; /** * 指向上一个站点的指针 */ @@ -29,4 +34,13 @@ struct { */ typedef struct rail_node rail_node_t; +extern rail_node_t* rails; + +/** + * 查找指定编号的站点指针 + * @param id 需要查找的站点编号 + * @return 需要查找站点指针 + */ +rail_node_t *FindNode(int id); + #endif //AUTO_PILOT_BUS_RAIL_H diff --git a/src/controller.c b/src/controller.c new file mode 100644 index 0000000..4444f4a --- /dev/null +++ b/src/controller.c @@ -0,0 +1,4 @@ +// +// Created by ricardo on 2022/5/6. +// +#include "controller.h" \ No newline at end of file diff --git a/src/io.c b/src/io.c new file mode 100644 index 0000000..1544ebd --- /dev/null +++ b/src/io.c @@ -0,0 +1,4 @@ +// +// Created by ricardo on 2022/5/6. +// +#include "io.h" diff --git a/src/query.c b/src/query.c new file mode 100644 index 0000000..e3e149e --- /dev/null +++ b/src/query.c @@ -0,0 +1,8 @@ +// +// Created by ricardo on 2022/5/6. +// +#include "query.h" + +up_bus_t* up_queries; +down_bus_t *down_queries; + diff --git a/src/rail.c b/src/rail.c index 025011d..e7ebe6f 100644 --- a/src/rail.c +++ b/src/rail.c @@ -1 +1,3 @@ -#include "rail.h" \ No newline at end of file +#include "rail.h" + +rail_node_t *rails; \ No newline at end of file