2022-05-20 15:00:04 +08:00
|
|
|
|
#include "bus_io.h"
|
2022-04-29 10:37:33 +08:00
|
|
|
|
|
2022-05-04 16:24:05 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 程序的主函数
|
|
|
|
|
*
|
|
|
|
|
* @return int 程序结束的状态,0表示成功
|
|
|
|
|
*/
|
2022-04-29 10:37:33 +08:00
|
|
|
|
int main()
|
|
|
|
|
{
|
2022-05-20 15:00:04 +08:00
|
|
|
|
/**
|
|
|
|
|
* 输入的字符串
|
|
|
|
|
*/
|
|
|
|
|
char input[30];
|
|
|
|
|
/**
|
|
|
|
|
* the_bus指针的本体
|
|
|
|
|
*/
|
|
|
|
|
bus_t main_bus;
|
|
|
|
|
/**
|
|
|
|
|
* 公交车前进的方向
|
|
|
|
|
*/
|
|
|
|
|
int direction;
|
|
|
|
|
/**
|
|
|
|
|
* 完成的请求
|
|
|
|
|
*/
|
|
|
|
|
bus_query_t *finished_query;
|
|
|
|
|
|
|
|
|
|
// 读取配置文件
|
|
|
|
|
rails = ReadConfigFile();
|
|
|
|
|
|
|
|
|
|
// 制造公交车
|
|
|
|
|
the_bus = &main_bus;
|
|
|
|
|
the_bus->distance = 0;
|
|
|
|
|
the_bus->rail_node_pos = FindNode(rails, 1);
|
|
|
|
|
|
|
|
|
|
// 开始时公交车应该是停下的
|
|
|
|
|
direction = BUS_STOP;
|
|
|
|
|
|
2022-06-03 12:41:44 +08:00
|
|
|
|
PrintState();
|
2022-05-20 15:00:04 +08:00
|
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
|
{
|
|
|
|
|
fgets(input, sizeof input, stdin);
|
|
|
|
|
|
|
|
|
|
int result = ReadInput(input);
|
|
|
|
|
if(result == IO_CLOCK)
|
|
|
|
|
{
|
|
|
|
|
// 时间流动
|
|
|
|
|
AddTime();
|
|
|
|
|
switch (chosen_strategy)
|
|
|
|
|
{
|
|
|
|
|
case BUS_FCFS:
|
|
|
|
|
// 如果到站,处理请求和
|
|
|
|
|
if(JudgeOnStation() == BUS_TRUE)
|
|
|
|
|
{
|
2022-05-29 21:20:39 +08:00
|
|
|
|
direction = FCFSDirection(); //在开始得判断一次方向
|
2022-05-20 15:00:04 +08:00
|
|
|
|
finished_query = FCFSQuery();
|
|
|
|
|
|
|
|
|
|
if(finished_query != NULL) // 有请求就处理请求
|
|
|
|
|
{
|
|
|
|
|
// 循环处理所有可以处理的请求,总共消耗一秒
|
|
|
|
|
while (finished_query != NULL)
|
|
|
|
|
{
|
|
|
|
|
DeleteQuery(finished_query);
|
|
|
|
|
finished_query = FCFSQuery();
|
|
|
|
|
}
|
2022-05-28 22:20:03 +08:00
|
|
|
|
|
|
|
|
|
// 请求处理完再进行方向的判断
|
|
|
|
|
direction = FCFSDirection();
|
2022-05-20 15:00:04 +08:00
|
|
|
|
}
|
|
|
|
|
else //如果没有请求就继续前进
|
|
|
|
|
{
|
|
|
|
|
RunBus(direction);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
RunBus(direction);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case BUS_SSTF:
|
2022-05-28 22:20:03 +08:00
|
|
|
|
// 如果没有指定的请求就获得指定的请求
|
|
|
|
|
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
|
|
|
|
|
{
|
2022-06-03 21:36:32 +08:00
|
|
|
|
// 处理可以顺便处理的请求
|
|
|
|
|
bus_query_t *btw_query = SSTFBTWQuery(direction);
|
|
|
|
|
if (btw_query != NULL)
|
2022-05-28 22:20:03 +08:00
|
|
|
|
{
|
2022-06-03 21:36:32 +08:00
|
|
|
|
while (btw_query != NULL)
|
2022-05-28 22:20:03 +08:00
|
|
|
|
{
|
2022-06-03 21:36:32 +08:00
|
|
|
|
DeleteQuery(btw_query);
|
|
|
|
|
btw_query = SSTFBTWQuery(direction);
|
2022-05-28 22:20:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
RunBus(direction);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
RunBus(direction);
|
|
|
|
|
}
|
2022-05-20 15:00:04 +08:00
|
|
|
|
break;
|
|
|
|
|
case BUS_SCAN:
|
2022-05-28 22:20:03 +08:00
|
|
|
|
// 如果没有指定的请求就获得指定的请求
|
|
|
|
|
if(target_query == NULL)
|
|
|
|
|
{
|
2022-06-03 21:36:32 +08:00
|
|
|
|
target_query = SCANGetQuery(direction);
|
2022-05-28 22:26:56 +08:00
|
|
|
|
direction = SCANDirection(target_query);
|
2022-05-28 22:20:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(JudgeOnStation() == BUS_TRUE)
|
|
|
|
|
{
|
|
|
|
|
// 如果到达目标的站点
|
|
|
|
|
if(the_bus->rail_node_pos == target_query->node)
|
|
|
|
|
{
|
|
|
|
|
DeleteQuery(target_query);
|
|
|
|
|
target_query = NULL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-06-03 21:36:32 +08:00
|
|
|
|
bus_query_t *btw_query = SCANBTWQuery();
|
|
|
|
|
// 处理可以顺路处理的请求
|
|
|
|
|
if(btw_query != NULL)
|
2022-05-28 22:20:03 +08:00
|
|
|
|
{
|
2022-06-03 21:36:32 +08:00
|
|
|
|
while (btw_query != NULL)
|
2022-05-28 22:20:03 +08:00
|
|
|
|
{
|
2022-06-03 21:36:32 +08:00
|
|
|
|
DeleteQuery(btw_query);
|
|
|
|
|
btw_query = SCANBTWQuery();
|
2022-05-28 22:20:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
RunBus(direction);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
RunBus(direction);
|
|
|
|
|
}
|
2022-05-20 15:00:04 +08:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2022-05-28 22:20:03 +08:00
|
|
|
|
// 这个分支只是为了符合代码规范而存在,理论上不会用到这个分支
|
2022-05-20 15:00:04 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2022-06-03 12:41:44 +08:00
|
|
|
|
PrintState();
|
2022-05-20 15:00:04 +08:00
|
|
|
|
}
|
|
|
|
|
else if(result == IO_END)
|
|
|
|
|
{
|
|
|
|
|
printf("end\n");
|
2022-05-28 22:20:03 +08:00
|
|
|
|
|
|
|
|
|
FreeRails(rails);
|
2022-06-03 12:41:44 +08:00
|
|
|
|
FreeQueries(queries);
|
2022-05-20 15:00:04 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//在读取到创建请求的情况下,不做任何事
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-28 22:20:03 +08:00
|
|
|
|
|
|
|
|
|
return 0;
|
2022-04-29 10:37:33 +08:00
|
|
|
|
}
|