From 56e97ae4db8e9f803bc3f064cc56912c94db33bd Mon Sep 17 00:00:00 2001 From: Yerolling <2911328695@qq.com> Date: Sat, 4 Jun 2022 09:58:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86SCANDirection?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=92=8CSCANBTWQuery=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/src/controller.c b/src/controller.c index bc2d25c..028ee0d 100644 --- a/src/controller.c +++ b/src/controller.c @@ -265,10 +265,64 @@ bus_query_t *SCANGetQuery(int direction) int SCANDirection(bus_query_t *query) { - return BUS_STOP; + if(query == NULL) + { + return BUS_STOP; + } //如果没有请求,公交车停止 + + else + { + int clockwise = 0; + int counterclockwise = 0; //用于顺,逆时针方向所经站台计数 + + /** + * 公交车当前所在位置 + */ + rail_node_t *now_position = the_bus->rail_node_pos; + /** + * 公交车应该前进的方向 + */ + rail_node_t *target_position = query->node; + + rail_node_t *pos = now_position; + while (pos != target_position) //顺时针计数 + { + clockwise++; + pos = pos->next_node; + } + + pos = now_position; + while (pos != target_position) //逆时针计数 + { + counterclockwise++; + pos = pos->last_node; + } + + if(clockwise <= counterclockwise) + { + return BUS_CLOCK_WISE; + }//若顺时针距离短(或顺逆相等),公交车顺时针运行 + else + { + return BUS_COUNTER_CLOCK_WISE; + }//若逆时针距离短,公交车逆时针运行 + } } bus_query_t *SCANBTWQuery() { + rail_node_t *now_position = the_bus->rail_node_pos; + //获取公交车当前所在站点 + bus_query_t *p = queries; + + while(p != NULL) + { + if(p->node == now_position) + { + return p; + } + p = p->next_node; + }//遍历请求链表,判断是否有可以顺便处理的请求 + return NULL; } \ No newline at end of file