diff --git a/include/bus.h b/include/bus.h index 91c69d8..acc3b17 100644 --- a/include/bus.h +++ b/include/bus.h @@ -28,14 +28,13 @@ extern bus_t *the_bus; /** * 每个时刻使公交车前进 - * @param rails 轨道链表 * @param direction 公交车前进的方向 */ void RunBus(int direction); /** * 判断公交车是否到站 - * @return + * @return BUS_TRUE为到站,BUS_FALSE为未到站 */ int JudgeOnStation(); @@ -44,4 +43,12 @@ int JudgeOnStation(); * @return 公交车当前所在的位置 */ int GetBusPosition(); + +/** + * 给出在指定的方向下,指定的请求于公交车当前位置的距离 + * @param query 指定的请求 + * @param orientation 指定的方向 BUS_CLOCK_WISE BUS_COUNTER_CLOCK_WISE + * @return 距离 + */ +int GetQueryDistance(bus_query_t *query, int orientation); #endif //AUTO_PILOT_BUS_BUS_H diff --git a/include/bus_io.h b/include/bus_io.h index f9d6c23..c7c3735 100644 --- a/include/bus_io.h +++ b/include/bus_io.h @@ -26,8 +26,6 @@ int ReadInput(char* inputString); /** * 打印当前的状态 - * @param rails 轨道链表 - * @return 返回需输出的字符串 */ void PrintState(); diff --git a/include/rail.h b/include/rail.h index 0ab4964..7ff4f07 100644 --- a/include/rail.h +++ b/include/rail.h @@ -35,6 +35,11 @@ typedef struct rail_node rail_node_t; */ extern rail_node_t *rails; +/** + * 轨道的总长度 + */ +extern int all_distance; + /** * 全局的计时器 */ diff --git a/src/bus.c b/src/bus.c index 6f5d566..6c49450 100644 --- a/src/bus.c +++ b/src/bus.c @@ -82,4 +82,30 @@ int JudgeOnStation() { return BUS_FAlSE; } +} + +int GetQueryDistance(bus_query_t *query, int orientation) +{ + rail_node_t *target_node = query->node; + rail_node_t *now_node = the_bus->rail_node_pos; + int distance = 0; + + if(orientation == BUS_CLOCK_WISE) + { + while (now_node != target_node) + { + distance += now_node->next_node_distance; + now_node = now_node->next_node; + } + } + else if(orientation == BUS_COUNTER_CLOCK_WISE) + { + while (now_node != target_node) + { + distance += now_node->last_node_distance; + now_node = now_node->last_node; + } + } + + return distance; } \ No newline at end of file diff --git a/src/bus_io.c b/src/bus_io.c index 0375dc8..4ba1135 100644 --- a/src/bus_io.c +++ b/src/bus_io.c @@ -151,6 +151,7 @@ rail_node_t *ReadConfigFile() chosen_strategy = BUS_FCFS; } + all_distance = distance * total_station; rail_node_t *head = CreateRails(distance, total_station); return head; } diff --git a/src/rail.c b/src/rail.c index a620f24..d6cf67d 100644 --- a/src/rail.c +++ b/src/rail.c @@ -2,6 +2,7 @@ rail_node_t *rails = NULL; int bus_time = 0; +int all_distance = 0; rail_node_t *CreateRails(int length, int node_num) {