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; }