diff --git a/include/controller.h b/include/controller.h index 480b667..8405ba5 100644 --- a/include/controller.h +++ b/include/controller.h @@ -30,4 +30,44 @@ int FCFSDirection(); * @return 需要处理的请求 */ bus_query_t *FCFSQuery(); + +/** + * 获得在SSTF策略下应该处理的请求 + * @return 指向需要处理的请求的指针 + */ +bus_query_t *SSTFGetQuery(); + +/** + * 根据指定的请求获得前进的方向,也就是前往指定的请求最近的方向 + * 在SSTF策略中使用 + * @param target_query 指定完成的请求 + * @return 前进的方向 + */ +int SSTFDirection(bus_query_t* target_query); + +/** + * 在当前站上可以顺便服务的请求 + * @return 服务的请求指针 + */ +bus_query_t *SSTFBTWQuery(); + +/** + * 获得在SCAN策略下应该处理的请求 + * @return 指向需要处理的请求的指针 + */ +bus_query_t *SCANGetQuery(); + +/** + * 根据指定的请求获得前进的方向 + * 在SCAN策略中使用 + * @param target_query 指定完成的请求 + * @return 前进的方向 + */ +int SCANDirection(bus_query_t *target_query); + +/** + * 在当前站上可以顺便服务的请求 + * @return 服务的请求指针 + */ +bus_query_t *SCANBTWQuery(); #endif //AUTO_PILOT_BUS_CONTROLLER_H diff --git a/include/query.h b/include/query.h index af2ee14..0ce19c8 100644 --- a/include/query.h +++ b/include/query.h @@ -37,6 +37,7 @@ extern bus_query_t *queries; * 创建请求链表节点 * @param type 请求的类型 * @param node 请求产生/指向的站点 + * @return 当前创建的链表节点地址 */ bus_query_t *CreateQuery(int type, rail_node_t *node); diff --git a/main.c b/main.c index b477b9f..ec20da9 100644 --- a/main.c +++ b/main.c @@ -58,7 +58,6 @@ int main() // 如果到站,处理请求和 if(JudgeOnStation() == BUS_TRUE) { - direction = FCFSDirection(); finished_query = FCFSQuery(); if(finished_query != NULL) // 有请求就处理请求 @@ -69,6 +68,9 @@ int main() DeleteQuery(finished_query); finished_query = FCFSQuery(); } + + // 请求处理完再进行方向的判断 + direction = FCFSDirection(); } else //如果没有请求就继续前进 { @@ -81,10 +83,83 @@ int main() } break; case BUS_SSTF: + // 如果没有指定的请求就获得指定的请求 + if(target_query == NULL) + { + target_query = SSTFGetQuery(); + direction = SSTFDirection(target_query); + } + + if(JudgeOnStation() == BUS_TRUE) + { + // 如果到达目标的站点 + if(the_bus->rail_node_pos == target_query->node) + { + DeleteQuery(target_query); + target_query = NULL; + } + else + { + target_query = SSTFBTWQuery(); + if(target_query != NULL) + { + while (target_query != NULL) + { + DeleteQuery(target_query); + target_query = SSTFBTWQuery(); + } + } + else + { + RunBus(direction); + } + } + } + else + { + RunBus(direction); + } break; case BUS_SCAN: + // 如果没有指定的请求就获得指定的请求 + if(target_query == NULL) + { + target_query = SCANGetQuery(); + direction = SSTFDirection(target_query); + } + + if(JudgeOnStation() == BUS_TRUE) + { + // 如果到达目标的站点 + if(the_bus->rail_node_pos == target_query->node) + { + DeleteQuery(target_query); + target_query = NULL; + } + else + { + target_query = SCANGetQuery(); + if(target_query != NULL) + { + while (target_query != NULL) + { + DeleteQuery(target_query); + target_query = SCANBTWQuery(); + } + } + else + { + RunBus(direction); + } + } + } + else + { + RunBus(direction); + } break; default: + // 这个分支只是为了符合代码规范而存在,理论上不会用到这个分支 break; } PrintState(output); @@ -93,6 +168,9 @@ int main() else if(result == IO_END) { printf("end\n"); + + FreeRails(rails); + break; } else @@ -100,4 +178,6 @@ int main() //在读取到创建请求的情况下,不做任何事 } } + + return 0; } \ No newline at end of file diff --git a/src/bus.c b/src/bus.c index c45b5b9..d896235 100644 --- a/src/bus.c +++ b/src/bus.c @@ -1,6 +1,7 @@ #include "bus.h" -#include "math.h" + bus_t *the_bus = NULL; + void RunBus(int direction) { if(direction == BUS_CLOCK_WISE)//顺时针 @@ -11,21 +12,25 @@ void RunBus(int direction) { the_bus->distance--; } - } + int GetBusPosition() { int a, b, c; b = 0; rail_node_t *p = rails; a = the_bus->rail_node_pos->id;//指向站点的指针以及这个指针对应的站台id + while (p->id != a){ b += p->next_node_distance; p = p->next_node; } + c = b + (the_bus->distance); + return c; } + int JudgeOnStation() { if (abs(the_bus->distance) == rails->last_node_distance)//表示逆时针 diff --git a/src/controller.c b/src/controller.c index 130c710..15eca77 100644 --- a/src/controller.c +++ b/src/controller.c @@ -48,3 +48,33 @@ bus_query_t *FCFSQuery() { return NULL; } + +bus_query_t *SSTFGetQuery() +{ + return NULL; +} + +int SSTFDirection(bus_query_t* target_query) +{ + return BUS_STOP; +} + +bus_query_t *SSTFBTWQuery() +{ + return NULL; +} + +bus_query_t *SCANGetQuery() +{ + return NULL; +} + +int SCANDirection(bus_query_t *target_query) +{ + return BUS_STOP; +} + +bus_query_t *SCANBTWQuery() +{ + return NULL; +} \ No newline at end of file