diff --git a/src/controller.c b/src/controller.c index 028ee0d..b41ff36 100644 --- a/src/controller.c +++ b/src/controller.c @@ -71,62 +71,43 @@ bus_query_t *FCFSQuery() bus_query_t *SSTFGetQuery() { - int a,b,length,min; - a = the_bus->rail_node_pos->id; //是当前公交车的位置,而不是站点位置,应该两者差别不大 - b = rails->last_node->id;//总的站点数 - if (a >= 1 && a <= (b/2))//判断当前站点的位置 + // 当前没有请求 + if(queries == NULL) { - if (abs(a+b-queries->node->id) < abs(a-queries->node->id) ) //因为是环形轨道,用id相减的方式来计算距离的话,相减的方法不同 - { - min = abs(a+b-queries->node->id); - } - else - { - min = abs(a-queries->node->id); - } + return NULL; } - else if (a > (b/2) && a <=b) + int distance = 9999; + bus_query_t *query = NULL; + bus_query_t *p = queries; + + // 遍历顺时针方向 + // 在两个方向路程相同时选择顺时针方向 + // 所以先遍历顺时针方向 + while (p != NULL) { - if (abs(queries->node->id+b-a) < abs(a-queries->node->id)) + int temp = GetQueryDistance(p, BUS_CLOCK_WISE); + if(temp < distance) { - min = abs(queries->node->id+b-a); - } - else - { - min = abs(a-queries->node->id); + distance = temp; + query = p; } + p = p->next_node; } - //通过上诉把第一个请求的距离置为最小值min - bus_query_t *p = queries->next_node; - bus_query_t *result = queries;//如果第一个请求即最近的那一个,result=第一个请求 - while (p->time <= bus_time)//在当前的时间找到所有请求中最近的一个 + + // 遍历逆时针方向 + p = queries; + while (p != NULL) { - if (a >= 1 && a <= (b/2))//对当前站点所在的位置进行分类讨论 + int temp = GetQueryDistance(p, BUS_COUNTER_CLOCK_WISE); + if(temp < distance) { - if (abs(a+b-p->node->id) <= abs(a-p->node->id) ) - { - if(min >= abs(a+b-p->node->id)) - { - min = abs(a+b-p->node->id); - result = p;//把最小距离所对应的请求指针传给result - p = p->next_node; - } - } - } - else if (a > (b/2) && a <=b) - { - if (abs(p->node->id+b-a) <= abs(a-p->node->id)) - { - if (min >= abs(p->node->id+b-a)) - { - min = abs(p->node->id+b-a); - result = p; - p = p->next_node; - } - } + distance = temp; + query = p; } + p = p->next_node; } - return result;//需不需要返回空指针的情况呢? + + return query; } int SSTFDirection(bus_query_t* query)