完成了SSTFBTWQuery,SCANGetQuery函数

调整了main函数中SSTF,SCAN顺便处理部分的逻辑
This commit is contained in:
jackfiled 2022-06-03 21:36:32 +08:00
parent 3b6d7933a7
commit 9e73aa8488
4 changed files with 143 additions and 30 deletions

View File

@ -82,7 +82,7 @@ int main()
// 如果到站,处理请求和 // 如果到站,处理请求和
if(JudgeOnStation() == BUS_TRUE) if(JudgeOnStation() == BUS_TRUE)
{ {
direction = FCFSDirection(); // 在开始也得进行一次方向的判断 direction = FCFSDirection(); //在开始得判断一次方向
finished_query = FCFSQuery(); finished_query = FCFSQuery();
if(finished_query != NULL) // 有请求就处理请求 if(finished_query != NULL) // 有请求就处理请求
@ -125,13 +125,14 @@ int main()
} }
else 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); DeleteQuery(btw_query);
target_query = SSTFBTWQuery(); btw_query = SSTFBTWQuery(direction);
} }
} }
else else
@ -149,7 +150,7 @@ int main()
// 如果没有指定的请求就获得指定的请求 // 如果没有指定的请求就获得指定的请求
if(target_query == NULL) if(target_query == NULL)
{ {
target_query = SCANGetQuery(); target_query = SCANGetQuery(direction);
direction = SCANDirection(target_query); direction = SCANDirection(target_query);
} }
@ -163,13 +164,14 @@ int main()
} }
else else
{ {
target_query = SCANBTWQuery(); bus_query_t *btw_query = SCANBTWQuery();
if(target_query != NULL) // 处理可以顺路处理的请求
if(btw_query != NULL)
{ {
while (target_query != NULL) while (btw_query != NULL)
{ {
DeleteQuery(target_query); DeleteQuery(btw_query);
target_query = SCANBTWQuery(); btw_query = SCANBTWQuery();
} }
} }
else else

View File

@ -47,15 +47,16 @@ int SSTFDirection(bus_query_t* query);
/** /**
* 便 * 便
* @param direction
* @return * @return
*/ */
bus_query_t *SSTFBTWQuery(); bus_query_t *SSTFBTWQuery(int direction);
/** /**
* SCAN策略下应该处理的请求 * SCAN策略下应该处理的请求
* @return * @return
*/ */
bus_query_t *SCANGetQuery(); bus_query_t *SCANGetQuery(int direction);
/** /**
* *

24
main.c
View File

@ -95,13 +95,14 @@ int main()
} }
else 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); DeleteQuery(btw_query);
target_query = SSTFBTWQuery(); btw_query = SSTFBTWQuery(direction);
} }
} }
else else
@ -119,7 +120,7 @@ int main()
// 如果没有指定的请求就获得指定的请求 // 如果没有指定的请求就获得指定的请求
if(target_query == NULL) if(target_query == NULL)
{ {
target_query = SCANGetQuery(); target_query = SCANGetQuery(direction);
direction = SCANDirection(target_query); direction = SCANDirection(target_query);
} }
@ -133,13 +134,14 @@ int main()
} }
else else
{ {
target_query = SCANBTWQuery(); bus_query_t *btw_query = SCANBTWQuery();
if(target_query != NULL) // 处理可以顺路处理的请求
if(btw_query != NULL)
{ {
while (target_query != NULL) while (btw_query != NULL)
{ {
DeleteQuery(target_query); DeleteQuery(btw_query);
target_query = SCANBTWQuery(); btw_query = SCANBTWQuery();
} }
} }
else else

View File

@ -2,7 +2,6 @@
// Created by ricardo on 2022/5/6. // Created by ricardo on 2022/5/6.
// //
#include "controller.h" #include "controller.h"
#include "math.h"
bus_query_t *target_query = NULL; bus_query_t *target_query = NULL;
int chosen_strategy = -1; 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) int SCANDirection(bus_query_t *query)