diff --git a/all_test/main.c b/all_test/main.c index 4016eb4..8b14198 100644 --- a/all_test/main.c +++ b/all_test/main.c @@ -82,7 +82,7 @@ int main() // 如果到站,处理请求和 if(JudgeOnStation() == BUS_TRUE) { - direction = FCFSDirection(); // 在开始也得进行一次方向的判断 + direction = FCFSDirection(); //在开始得判断一次方向 finished_query = FCFSQuery(); if(finished_query != NULL) // 有请求就处理请求 @@ -125,13 +125,14 @@ int main() } else { - target_query = SSTFBTWQuery(); - if(target_query != NULL) + // 处理可以顺便处理的请求 + bus_query_t *btw_query = SSTFBTWQuery(direction); + if (btw_query != NULL) { - while (target_query != NULL) + while (btw_query != NULL) { - DeleteQuery(target_query); - target_query = SSTFBTWQuery(); + DeleteQuery(btw_query); + btw_query = SSTFBTWQuery(direction); } } else @@ -149,7 +150,7 @@ int main() // 如果没有指定的请求就获得指定的请求 if(target_query == NULL) { - target_query = SCANGetQuery(); + target_query = SCANGetQuery(direction); direction = SCANDirection(target_query); } @@ -163,13 +164,14 @@ int main() } else { - target_query = SCANBTWQuery(); - if(target_query != NULL) + bus_query_t *btw_query = SCANBTWQuery(); + // 处理可以顺路处理的请求 + if(btw_query != NULL) { - while (target_query != NULL) + while (btw_query != NULL) { - DeleteQuery(target_query); - target_query = SCANBTWQuery(); + DeleteQuery(btw_query); + btw_query = SCANBTWQuery(); } } else diff --git a/include/controller.h b/include/controller.h index c39a6b3..3f1e666 100644 --- a/include/controller.h +++ b/include/controller.h @@ -47,15 +47,16 @@ int SSTFDirection(bus_query_t* query); /** * 在当前站上可以顺便服务的请求 + * @param direction 当前公交车前进的方向 * @return 服务的请求指针 */ -bus_query_t *SSTFBTWQuery(); +bus_query_t *SSTFBTWQuery(int direction); /** * 获得在SCAN策略下应该处理的请求 * @return 指向需要处理的请求的指针 */ -bus_query_t *SCANGetQuery(); +bus_query_t *SCANGetQuery(int direction); /** * 根据指定的请求获得前进的方向 diff --git a/main.c b/main.c index 9de99a7..6875e9d 100644 --- a/main.c +++ b/main.c @@ -95,13 +95,14 @@ int main() } else { - target_query = SSTFBTWQuery(); - if(target_query != NULL) + // 处理可以顺便处理的请求 + bus_query_t *btw_query = SSTFBTWQuery(direction); + if (btw_query != NULL) { - while (target_query != NULL) + while (btw_query != NULL) { - DeleteQuery(target_query); - target_query = SSTFBTWQuery(); + DeleteQuery(btw_query); + btw_query = SSTFBTWQuery(direction); } } else @@ -119,7 +120,7 @@ int main() // 如果没有指定的请求就获得指定的请求 if(target_query == NULL) { - target_query = SCANGetQuery(); + target_query = SCANGetQuery(direction); direction = SCANDirection(target_query); } @@ -133,13 +134,14 @@ int main() } else { - target_query = SCANBTWQuery(); - if(target_query != NULL) + bus_query_t *btw_query = SCANBTWQuery(); + // 处理可以顺路处理的请求 + if(btw_query != NULL) { - while (target_query != NULL) + while (btw_query != NULL) { - DeleteQuery(target_query); - target_query = SCANBTWQuery(); + DeleteQuery(btw_query); + btw_query = SCANBTWQuery(); } } else diff --git a/src/controller.c b/src/controller.c index 9cef1da..e468269 100644 --- a/src/controller.c +++ b/src/controller.c @@ -2,7 +2,6 @@ // Created by ricardo on 2022/5/6. // #include "controller.h" -#include "math.h" bus_query_t *target_query = NULL; int chosen_strategy = -1; @@ -145,14 +144,123 @@ int SSTFDirection(bus_query_t* query) } } -bus_query_t *SSTFBTWQuery() +bus_query_t *SSTFBTWQuery(int direction) { - return NULL; + bus_query_t *query = queries; + bus_query_t *allow_query = NULL; + rail_node_t *now_node = the_bus->rail_node_pos; + + while (query != NULL) + { + if(query->node == now_node) + { + int type = query->type; + + if(type == direction || type == BUS_TARGET) + { + allow_query = query; + break; + } + } + query = query->next_node; + } + + return allow_query; } -bus_query_t *SCANGetQuery() +bus_query_t *SCANGetQuery(int direction) { - return NULL; + // 当前没有请求 + if(queries == NULL) + { + return NULL; + } + + if(direction == BUS_STOP) + { + // 在停止的状态下第一次开始选择方向 + int distance = 9999; + bus_query_t *query = NULL; + bus_query_t *p = queries; + + // 遍历顺时针方向 + // 在两个方向路程相同时选择顺时针方向 + // 所以先遍历顺时针方向 + while (p != NULL) + { + int temp = GetQueryDistance(p, BUS_CLOCK_WISE); + if(temp < distance) + { + distance = temp; + query = p; + } + p = p->next_node; + } + + // 遍历逆时针方向 + p = queries; + while (p != NULL) + { + int temp = GetQueryDistance(p, BUS_COUNTER_CLOCK_WISE); + if(temp < distance) + { + distance = temp; + query = p; + } + p = p->next_node; + } + + return query; + } + else + { + // 在已经有方向的情况下处理方向 + + int opposite_direction; + int distance = 9999; + bus_query_t *query = NULL; + bus_query_t *p = queries; + + // 先指出反方向 + // 可以减少后面的代码量 + if(direction == BUS_CLOCK_WISE) + { + opposite_direction = BUS_COUNTER_CLOCK_WISE; + } + else + { + opposite_direction = BUS_CLOCK_WISE; + } + + while (p != NULL) + { + int temp = GetQueryDistance(p, direction); + if(temp < distance) + { + query = p; + distance = temp; + } + p = p->next_node; + } + + // 在距离超过轨道一半的情况下 + if(distance > all_distance / 2) + { + p = queries; + while (p != NULL) + { + int temp = GetQueryDistance(p, opposite_direction); + if(temp < distance) + { + query = p; + distance = temp; + } + p = p->next_node; + } + } + + return query; + } } int SCANDirection(bus_query_t *query)