From a0ba048cbf53ea63926a858144a0830aa930db2d Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 29 May 2022 21:21:49 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99=E7=9A=84=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E5=87=BD=E6=95=B0=EF=BC=8C=E9=87=8D=E5=86=99?= =?UTF-8?q?=E4=BA=86=E9=A1=BA=E9=80=86=E6=97=B6=E9=92=88=E8=B7=AF=E7=A8=8B?= =?UTF-8?q?=E8=AE=A1=E6=95=B0=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/controller.h | 8 ++++---- src/controller.c | 49 +++++++++++++++++++++++++++----------------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/include/controller.h b/include/controller.h index 8405ba5..c39a6b3 100644 --- a/include/controller.h +++ b/include/controller.h @@ -40,10 +40,10 @@ bus_query_t *SSTFGetQuery(); /** * 根据指定的请求获得前进的方向,也就是前往指定的请求最近的方向 * 在SSTF策略中使用 - * @param target_query 指定完成的请求 + * @param query 指定完成的请求 * @return 前进的方向 */ -int SSTFDirection(bus_query_t* target_query); +int SSTFDirection(bus_query_t* query); /** * 在当前站上可以顺便服务的请求 @@ -60,10 +60,10 @@ bus_query_t *SCANGetQuery(); /** * 根据指定的请求获得前进的方向 * 在SCAN策略中使用 - * @param target_query 指定完成的请求 + * @param query 指定完成的请求 * @return 前进的方向 */ -int SCANDirection(bus_query_t *target_query); +int SCANDirection(bus_query_t *query); /** * 在当前站上可以顺便服务的请求 diff --git a/src/controller.c b/src/controller.c index 03bb162..889c829 100644 --- a/src/controller.c +++ b/src/controller.c @@ -19,17 +19,29 @@ int FCFSDirection() { int clockwise = 0; int counterclockwise = 0; //用于顺,逆时针方向所经站台计数 - - while(p->node != p->next_node->node) + + /** + * 公交车当前所在位置 + */ + rail_node_t *now_position = the_bus->rail_node_pos; + /** + * 公交车应该前进的方向 + */ + rail_node_t *target_position = p->node; + + rail_node_t *pos = now_position; + while (pos != target_position) //顺时针计数 { - p->node = p->node->next_node; clockwise++; - }//顺时针方向计数 - while(p->node != p->next_node->node) + pos = pos->next_node; + } + + pos = now_position; + while (pos != target_position) //逆时针计数 { - p->next_node->node = p->next_node->node->next_node; counterclockwise++; - }//逆时针方向计数 + pos = pos->last_node; + } if(clockwise <= counterclockwise) { @@ -40,22 +52,21 @@ int FCFSDirection() return BUS_COUNTER_CLOCK_WISE; }//若逆时针距离短,公交车逆时针运行 } - - } bus_query_t *FCFSQuery() { - bus_query_t *p = queries; - if (the_bus->rail_node_pos == p->node) + bus_query_t *result = NULL; + + if(queries != NULL) { - queries = queries->next_node; - return p; - } - else - { - return NULL; + if(the_bus->rail_node_pos == queries->node) + { + result = queries; + } } + + return result; } bus_query_t *SSTFGetQuery() @@ -63,7 +74,7 @@ bus_query_t *SSTFGetQuery() return NULL; } -int SSTFDirection(bus_query_t* target_query) +int SSTFDirection(bus_query_t* query) { return BUS_STOP; } @@ -78,7 +89,7 @@ bus_query_t *SCANGetQuery() return NULL; } -int SCANDirection(bus_query_t *target_query) +int SCANDirection(bus_query_t *query) { return BUS_STOP; }