完成了SSTF和SCAN策略的控制函数及流程设计

修复了FCFS策略中连续在同一站的请求处理错误的bug
This commit is contained in:
jackfiled 2022-05-28 22:20:03 +08:00
parent d691021553
commit 4e773ac790
5 changed files with 159 additions and 3 deletions

View File

@ -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

View File

@ -37,6 +37,7 @@ extern bus_query_t *queries;
*
* @param type
* @param node /
* @return
*/
bus_query_t *CreateQuery(int type, rail_node_t *node);

82
main.c
View File

@ -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;
}

View File

@ -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)//表示逆时针

View File

@ -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;
}