2022-05-06 11:55:30 +08:00
|
|
|
|
//
|
|
|
|
|
// Created by ricardo on 2022/5/6.
|
|
|
|
|
//
|
2022-05-08 11:48:04 +08:00
|
|
|
|
#include "bus_io.h"
|
2022-05-19 22:39:45 +08:00
|
|
|
|
|
2022-05-20 19:31:12 +08:00
|
|
|
|
int ReadInput(char *inputString)
|
2022-05-19 22:39:45 +08:00
|
|
|
|
{
|
2022-05-20 19:31:12 +08:00
|
|
|
|
|
2022-05-19 22:39:45 +08:00
|
|
|
|
char src[20];
|
|
|
|
|
int num;
|
2022-05-20 19:31:12 +08:00
|
|
|
|
sscanf_s(inputString, "%[a-z] %d", src, &num);
|
|
|
|
|
if (0 == strcmp("clock", src))
|
2022-05-19 22:39:45 +08:00
|
|
|
|
{
|
|
|
|
|
return IO_CLOCK;
|
|
|
|
|
}
|
2022-05-20 19:31:12 +08:00
|
|
|
|
else if (0 == strcmp("counterclockwise", src))
|
2022-05-19 22:39:45 +08:00
|
|
|
|
{
|
2022-05-20 19:31:12 +08:00
|
|
|
|
CreateQuery(BUS_COUNTER_CLOCK_WISE, FindNode(rails, num));
|
2022-05-19 22:39:45 +08:00
|
|
|
|
return IO_READING;
|
|
|
|
|
}
|
2022-05-20 19:31:12 +08:00
|
|
|
|
else if (0 == strcmp("clockwise", src))
|
2022-05-19 22:39:45 +08:00
|
|
|
|
{
|
2022-05-20 19:31:12 +08:00
|
|
|
|
CreateQuery(BUS_CLOCK_WISE, FindNode(rails, num));
|
2022-05-19 22:39:45 +08:00
|
|
|
|
return IO_READING;
|
|
|
|
|
}
|
2022-05-20 19:31:12 +08:00
|
|
|
|
else if (0 == strcmp("target", src))
|
2022-05-19 22:39:45 +08:00
|
|
|
|
{
|
2022-05-20 19:31:12 +08:00
|
|
|
|
CreateQuery(BUS_TARGET, FindNode(rails, num));
|
2022-05-19 22:39:45 +08:00
|
|
|
|
return IO_READING;
|
|
|
|
|
}
|
2022-05-20 19:31:12 +08:00
|
|
|
|
else if (0 == strcmp("end", src))
|
2022-05-19 22:39:45 +08:00
|
|
|
|
{
|
|
|
|
|
return IO_END;
|
|
|
|
|
}
|
2022-05-20 08:52:58 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// 匹配失败则返回-1
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2022-05-20 11:18:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rail_node_t *ReadConfigFile()
|
|
|
|
|
{
|
|
|
|
|
FILE *config_file = NULL;
|
|
|
|
|
char buffer[30];
|
|
|
|
|
int total_station = 0;
|
|
|
|
|
int distance = 0;
|
|
|
|
|
|
|
|
|
|
config_file = fopen("dict.dic", "r");
|
|
|
|
|
|
|
|
|
|
// 循环读取文件的每一行
|
2022-05-20 19:31:12 +08:00
|
|
|
|
while (fgets(buffer, sizeof buffer, config_file) != NULL)
|
2022-05-20 11:18:12 +08:00
|
|
|
|
{
|
|
|
|
|
char first_char = buffer[0];
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
switch (first_char)
|
|
|
|
|
{
|
|
|
|
|
case '#':
|
|
|
|
|
// 如果读取到#什么都不做
|
|
|
|
|
break;
|
|
|
|
|
case 'T':
|
|
|
|
|
// TOTAL_STATION
|
|
|
|
|
p = buffer;
|
|
|
|
|
|
|
|
|
|
// 把数字前面的所有字符全部干掉
|
|
|
|
|
while (*p < '0' || *p > '9')
|
|
|
|
|
{
|
|
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-22 14:31:10 +08:00
|
|
|
|
// 讲道理,应该只有两位数,所以就这样处理了
|
|
|
|
|
if (*(p + 1) == '\n')
|
2022-05-20 11:18:12 +08:00
|
|
|
|
{
|
2022-05-22 14:31:10 +08:00
|
|
|
|
total_station = *p - 48;
|
2022-05-20 11:18:12 +08:00
|
|
|
|
}
|
2022-05-22 14:31:10 +08:00
|
|
|
|
else
|
2022-05-20 11:18:12 +08:00
|
|
|
|
{
|
2022-05-22 14:31:10 +08:00
|
|
|
|
total_station = (*p - 48) * 10 + *(p + 1) - 48;
|
2022-05-20 11:18:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case 'S':
|
|
|
|
|
// STRATEGY
|
|
|
|
|
p = buffer;
|
|
|
|
|
// 将=前的字符全部略去
|
2022-05-20 19:31:12 +08:00
|
|
|
|
while (*p != '=')
|
2022-05-20 11:18:12 +08:00
|
|
|
|
{
|
|
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
// =也去掉
|
|
|
|
|
p++;
|
|
|
|
|
// =和策略之间的空格也去掉
|
2022-05-20 19:31:12 +08:00
|
|
|
|
while (*p == ' ')
|
2022-05-20 11:18:12 +08:00
|
|
|
|
{
|
|
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 19:31:12 +08:00
|
|
|
|
if (*p == 'F' && *(p + 1) == 'C') //FCFS
|
2022-05-20 11:18:12 +08:00
|
|
|
|
{
|
|
|
|
|
chosen_strategy = BUS_FCFS;
|
|
|
|
|
}
|
2022-05-20 19:31:12 +08:00
|
|
|
|
else if (*p == 'S' && *(p + 1) == 'S') //SSTF
|
2022-05-20 11:18:12 +08:00
|
|
|
|
{
|
|
|
|
|
chosen_strategy = BUS_SSTF;
|
|
|
|
|
}
|
2022-05-20 19:31:12 +08:00
|
|
|
|
else if (*p == 'S' && *(p + 1) == 'C') //SCAN
|
2022-05-20 11:18:12 +08:00
|
|
|
|
{
|
|
|
|
|
chosen_strategy = BUS_SCAN;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// 读取失败
|
|
|
|
|
chosen_strategy = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case 'D':
|
|
|
|
|
// DISTANCE
|
|
|
|
|
p = buffer;
|
|
|
|
|
|
|
|
|
|
// 把数字前面的所有字符全部干掉
|
|
|
|
|
while (*p < '0' || *p > '9')
|
|
|
|
|
{
|
|
|
|
|
p++;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 19:31:12 +08:00
|
|
|
|
if (*(p + 1) == '\n')
|
2022-05-20 11:18:12 +08:00
|
|
|
|
{
|
|
|
|
|
distance = *p - 48;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 19:31:12 +08:00
|
|
|
|
if (distance != 0 && total_station != 0)
|
2022-05-20 11:18:12 +08:00
|
|
|
|
{
|
|
|
|
|
return CreateRails(distance, total_station);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2022-05-19 22:39:45 +08:00
|
|
|
|
}
|
2022-05-20 17:47:48 +08:00
|
|
|
|
|
2022-05-20 19:31:12 +08:00
|
|
|
|
void PrintState(char *str)
|
2022-05-20 17:47:48 +08:00
|
|
|
|
{
|
2022-05-20 19:31:12 +08:00
|
|
|
|
memset(str, 0, 150);
|
|
|
|
|
|
2022-05-24 21:16:51 +08:00
|
|
|
|
int count, flag=1; //flag用于标记,为使下面第一个循环能够进入
|
2022-05-20 19:31:12 +08:00
|
|
|
|
rail_node_t *p = NULL;
|
|
|
|
|
char target[20], clockwise[20], counterclockwise[20];
|
|
|
|
|
|
2022-05-24 21:16:51 +08:00
|
|
|
|
for (count = 0, p = rails; flag==1 || p != NULL; p = p->next_node, count++)
|
2022-05-20 17:47:48 +08:00
|
|
|
|
{
|
2022-05-24 21:16:51 +08:00
|
|
|
|
flag=0;
|
2022-05-20 19:31:12 +08:00
|
|
|
|
target[count] = '0';
|
|
|
|
|
clockwise[count] = '0';
|
|
|
|
|
counterclockwise[count] = '0';
|
2022-05-20 17:47:48 +08:00
|
|
|
|
} //遍历轨道链表,将所有站点初始化为0,即:无任何请求;
|
|
|
|
|
|
2022-05-20 19:31:12 +08:00
|
|
|
|
bus_query_t *t = NULL;
|
2022-05-20 17:47:48 +08:00
|
|
|
|
int i;
|
2022-05-20 19:31:12 +08:00
|
|
|
|
for (t = queries; t != NULL; t = t->next_node)
|
|
|
|
|
{
|
|
|
|
|
i = t->node->id - 1;
|
|
|
|
|
if (t->type == 0)
|
2022-05-20 17:47:48 +08:00
|
|
|
|
{
|
2022-05-20 19:31:12 +08:00
|
|
|
|
clockwise[i] = '1';
|
2022-05-20 17:47:48 +08:00
|
|
|
|
}
|
2022-05-20 21:19:14 +08:00
|
|
|
|
else if(t->time==BUS_COUNTER_CLOCK_WISE)
|
2022-05-20 17:47:48 +08:00
|
|
|
|
{
|
2022-05-20 19:31:12 +08:00
|
|
|
|
counterclockwise[i] = '1';
|
2022-05-20 17:47:48 +08:00
|
|
|
|
}
|
2022-05-20 21:19:14 +08:00
|
|
|
|
else if(t->type==BUS_TARGET)
|
2022-05-20 17:47:48 +08:00
|
|
|
|
{
|
2022-05-20 19:31:12 +08:00
|
|
|
|
target[i] = '1';
|
2022-05-20 17:47:48 +08:00
|
|
|
|
}
|
|
|
|
|
} //遍历请求链表,将有请求的站点按照不同类型标记为1
|
2022-05-20 19:31:12 +08:00
|
|
|
|
|
|
|
|
|
char line1[100], line2[10], line3[15], line4[100], line5[10], line6[100], line7[100];
|
|
|
|
|
|
|
|
|
|
sprintf(line1, "TIME:%d\n", bus_time);
|
|
|
|
|
sprintf(line2, "BUS:\n");
|
|
|
|
|
sprintf(line3, "position:%d\n", GetBusPosition());
|
|
|
|
|
sprintf(line4, "target:%s\n", target);
|
|
|
|
|
sprintf(line5, "STATION:\n");
|
|
|
|
|
sprintf(line6, "clockwise:%s\n", clockwise);
|
|
|
|
|
sprintf(line7, "counterclockwise:%s\n", counterclockwise); //分别得到每一行的字符串
|
|
|
|
|
|
|
|
|
|
strcat(line1, line2);
|
|
|
|
|
strcat(line1, line3);
|
|
|
|
|
strcat(line1, line4);
|
|
|
|
|
strcat(line1, line5);
|
|
|
|
|
strcat(line1, line6);
|
|
|
|
|
strcat(line1, line7);//将7行字符串合并在一起
|
|
|
|
|
|
|
|
|
|
strcat(str, line1);
|
2022-05-20 17:47:48 +08:00
|
|
|
|
}
|