From 1cfa8612b9f34f573ac157ca4ad4e14fd2f6e8b4 Mon Sep 17 00:00:00 2001 From: nvhaizi1 Date: Thu, 2 Jun 2022 08:50:17 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=86=99bus=5Fquery=5Ft=20*SSTFGetQuery?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/src/controller.c b/src/controller.c index 889c829..d83fb05 100644 --- a/src/controller.c +++ b/src/controller.c @@ -71,7 +71,61 @@ bus_query_t *FCFSQuery() bus_query_t *SSTFGetQuery() { - return NULL; + int a,b,length,min=10;//最多有10个站点 + a = the_bus->rail_node_pos->id; //是当前公交车的位置,而不是站点位置 + b=rails->last_node->id;//总的站点数 + if (1 <= a <= b/2) + { + if(abs(a+b-queries->node->id) < abs(a-queries->node->id) ) + { + min=abs(a+b-queries->node->id); + } + else + { + min=abs(a-queries->node->id); + } + } + else if (b/2 < a <=b) + { + if (abs(queries->node->id+b-a)node->id)) + { + min=abs(queries->node->id+b-a); + } + else + { + min=abs(a-queries->node->id); + } + } + bus_query_t *p =queries->next_node; + bus_query_t *result=NULL; + while(queries->time != bus_time) + { + if (1 <= a <= b/2) + { + 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; + p=p->next_node; + } + } + } + else if (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; + } + } + } + } + return result; } int SSTFDirection(bus_query_t* query) From 2459ccb8f5ec42f03f41b583631c5b94854554ae Mon Sep 17 00:00:00 2001 From: nvhaizi1 Date: Thu, 2 Jun 2022 09:02:04 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9SSTFGetQuery=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller.c | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/controller.c b/src/controller.c index d83fb05..a8a4e38 100644 --- a/src/controller.c +++ b/src/controller.c @@ -71,44 +71,45 @@ bus_query_t *FCFSQuery() bus_query_t *SSTFGetQuery() { - int a,b,length,min=10;//最多有10个站点 + int a,b,length,min; a = the_bus->rail_node_pos->id; //是当前公交车的位置,而不是站点位置 - b=rails->last_node->id;//总的站点数 + b = rails->last_node->id;//总的站点数 if (1 <= a <= b/2) { - if(abs(a+b-queries->node->id) < abs(a-queries->node->id) ) + if (abs(a+b-queries->node->id) < abs(a-queries->node->id) ) { - min=abs(a+b-queries->node->id); + min = abs(a+b-queries->node->id); } else { - min=abs(a-queries->node->id); + min = abs(a-queries->node->id); } } else if (b/2 < a <=b) { - if (abs(queries->node->id+b-a)node->id)) + if (abs(queries->node->id+b-a) < abs(a-queries->node->id)) { - min=abs(queries->node->id+b-a); + min = abs(queries->node->id+b-a); } else { - min=abs(a-queries->node->id); + min = abs(a-queries->node->id); } } - bus_query_t *p =queries->next_node; - bus_query_t *result=NULL; - while(queries->time != bus_time) + //通过上诉把第一个请求的距离置为最小值min + bus_query_t *p = queries->next_node; + bus_query_t *result = queries;//如果第一个请求即最近的那一个,result=第一个请求 + while (p->time <= bus_time)//在当前的时间找到所有请求中最近的一个 { - if (1 <= a <= b/2) + if (1 <= a <= b/2)//对当前站点所在的位置进行分类讨论 { - if(abs(a+b-p->node->id) <= abs(a-p->node->id) ) + if (abs(a+b-p->node->id) <= abs(a-p->node->id) ) { - if(min >=abs(a+b-p->node->id)) + if(min >= abs(a+b-p->node->id)) { - min=abs(a+b-p->node->id); - result=p; - p=p->next_node; + min = abs(a+b-p->node->id); + result = p;//把最小距离所对应的请求指针传给result + p = p->next_node; } } } @@ -118,14 +119,14 @@ bus_query_t *SSTFGetQuery() { if (min >= abs(p->node->id+b-a)) { - min=abs(p->node->id+b-a); - result=p; - p=p->next_node; + min = abs(p->node->id+b-a); + result = p; + p = p->next_node; } } } } - return result; + return result;//需不需要返回空指针的情况呢? } int SSTFDirection(bus_query_t* query) From 2213147beec851f845a3cdbec30e01c66755610b Mon Sep 17 00:00:00 2001 From: nvhaizi1 Date: Thu, 2 Jun 2022 09:37:29 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=86=99SSTFDirection=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/controller.c b/src/controller.c index a8a4e38..9cef1da 100644 --- a/src/controller.c +++ b/src/controller.c @@ -2,6 +2,7 @@ // Created by ricardo on 2022/5/6. // #include "controller.h" +#include "math.h" bus_query_t *target_query = NULL; int chosen_strategy = -1; @@ -72,11 +73,11 @@ bus_query_t *FCFSQuery() bus_query_t *SSTFGetQuery() { int a,b,length,min; - a = the_bus->rail_node_pos->id; //是当前公交车的位置,而不是站点位置 + a = the_bus->rail_node_pos->id; //是当前公交车的位置,而不是站点位置,应该两者差别不大 b = rails->last_node->id;//总的站点数 - if (1 <= a <= b/2) + if (1 <= a <= b/2)//判断当前站点的位置 { - if (abs(a+b-queries->node->id) < abs(a-queries->node->id) ) + if (abs(a+b-queries->node->id) < abs(a-queries->node->id) ) //因为是环形轨道,用id相减的方式来计算距离的话,相减的方法不同 { min = abs(a+b-queries->node->id); } @@ -131,7 +132,17 @@ bus_query_t *SSTFGetQuery() int SSTFDirection(bus_query_t* query) { + if (query != NULL) + { + if (query->type != BUS_TARGET ) + { + return query->type; + } + } + else + { return BUS_STOP; + } } bus_query_t *SSTFBTWQuery()