添加了读取指定配置文件的功能

添加了官方的测试用例
This commit is contained in:
2022-05-22 13:58:02 +08:00
parent 8a70c8255d
commit fa13a63fd0
23 changed files with 1597 additions and 2 deletions

View File

@@ -57,4 +57,128 @@ int CheckOutput(char *program_output, char *read_output)
{
return BUS_FAlSE;
}
}
rail_node_t *ChooseConfigFile(int index)
{
char root_path[] = "./test_cases/";
char config_path[] = "/dict.dic";
char case_path[3];
sprintf(case_path, "%d", index);
char file_path[30];
strcat(file_path, root_path);
strcat(file_path, case_path);
strcat(file_path, config_path);
return ReadChosenConfigFile(file_path);
}
rail_node_t *ReadChosenConfigFile(char *config_file_path)
{
FILE *config_file = NULL;
char buffer[30];
int total_station = 0;
int distance = 0;
config_file = fopen(config_file_path, "r");
// 循环读取文件的每一行
while (fgets(buffer, sizeof buffer, config_file) != NULL)
{
char first_char = buffer[0];
char *p;
switch (first_char)
{
case '#':
// 如果读取到#什么都不做
break;
case 'T':
// TOTAL_STATION
p = buffer;
// 把数字前面的所有字符全部干掉
while (*p < '0' || *p > '9')
{
p++;
}
if (*p == '1' && *(p + 1) != '\n')
{
total_station = 10;
}
else if (*(p + 1) == '\n')
{
total_station = *p - 48;
}
break;
case 'S':
// STRATEGY
p = buffer;
// 将=前的字符全部略去
while (*p != '=')
{
p++;
}
// =也去掉
p++;
// =和策略之间的空格也去掉
while (*p == ' ')
{
p++;
}
if (*p == 'F' && *(p + 1) == 'C') //FCFS
{
chosen_strategy = BUS_FCFS;
}
else if (*p == 'S' && *(p + 1) == 'S') //SSTF
{
chosen_strategy = BUS_SSTF;
}
else if (*p == 'S' && *(p + 1) == 'C') //SCAN
{
chosen_strategy = BUS_SCAN;
}
else
{
// 读取失败
chosen_strategy = -1;
}
break;
case 'D':
// DISTANCE
p = buffer;
// 把数字前面的所有字符全部干掉
while (*p < '0' || *p > '9')
{
p++;
}
if (*(p + 1) == '\n')
{
distance = *p - 48;
}
break;
default:
continue;
}
}
if (distance != 0 && total_station != 0)
{
return CreateRails(distance, total_station);
}
else
{
return NULL;
}
}