From 312c49e4f0fca6e8ea1d8a37b4493da1c69098f3 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 20 May 2022 15:00:04 +0800 Subject: [PATCH] =?UTF-8?q?main=E5=87=BD=E6=95=B0=E7=9A=84FCFS=E9=83=A8?= =?UTF-8?q?=E5=88=86=E8=AE=BE=E8=AE=A1=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index 0c598dd..7db0da1 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,4 @@ -#include "stdio.h" +#include "bus_io.h" /** * @brief 程序的主函数 @@ -7,6 +7,102 @@ */ int main() { - printf("同志们加油啊,不然要寄啦!\n"); - return 0; + /** + * 输入的字符串 + */ + char input[30]; + /** + * 输出的字符串 + */ + char *output; + /** + * 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; + + output = PrintState(); + printf("%s", output); + + + 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) + { + direction = FCFSDirection(); + finished_query = FCFSQuery(); + + if(finished_query != NULL) // 有请求就处理请求 + { + // 循环处理所有可以处理的请求,总共消耗一秒 + while (finished_query != NULL) + { + DeleteQuery(finished_query); + finished_query = FCFSQuery(); + } + } + else //如果没有请求就继续前进 + { + RunBus(direction); + } + } + else + { + RunBus(direction); + } + break; + case BUS_SSTF: + break; + case BUS_SCAN: + break; + default: + break; + } + output = PrintState(); + } + else if(result == IO_END) + { + printf("end\n"); + break; + } + else + { + output = NULL; + //在读取到创建请求的情况下,不做任何事 + } + + if(output != NULL) + { + printf("%s", output); + } + } } \ No newline at end of file