diff --git a/include/StrategyFactory.h b/include/StrategyFactory.h new file mode 100644 index 0000000..c99bfa5 --- /dev/null +++ b/include/StrategyFactory.h @@ -0,0 +1,22 @@ +// +// Created by ricardo on 2022/6/27. +// + +#ifndef AUTO_BUS_GUI_STRATEGY_FACTORY_H +#define AUTO_BUS_GUI_STRATEGY_FACTORY_H +#include "cstdio" +#include "QByteArray" + +#include "BusStrategyBase.h" +#include "BusFCFSStrategy.h" +#include "BusSSTFStrategy.h" +#include "BusSCANStrategy.h" + +class StrategyFactory +{ +public: + static BusStrategyBase *GetStrategy(const QString& file_name); +}; + + +#endif //AUTO_BUS_GUI_STRATEGY_FACTORY_H diff --git a/include/railsModel.h b/include/railsModel.h index f138507..0b8d110 100644 --- a/include/railsModel.h +++ b/include/railsModel.h @@ -45,6 +45,8 @@ public: */ int node_num; + int total_distance; + explicit RailsModel(); ~RailsModel(); diff --git a/src/StrategyFactory.cpp b/src/StrategyFactory.cpp new file mode 100644 index 0000000..5c33efe --- /dev/null +++ b/src/StrategyFactory.cpp @@ -0,0 +1,144 @@ +// +// Created by ricardo on 2022/6/27. +// + +#include "StrategyFactory.h" + +BusStrategyBase *StrategyFactory::GetStrategy(const QString& file_name) +{ + QByteArray bytes = file_name.toLatin1(); + + FILE *config_file = NULL; + char buffer[30]; + int total_station = 0; + int distance = 0; + int chosen_strategy = 0; + + fopen_s(&config_file, bytes.data(), "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) == '\n' || *(p + 1) == '\0') + { + total_station = *p - 48; + } + else + { + total_station = (*p - 48) * 10 + *(p + 1) - 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' || *(p + 1) == '\0') + { + distance = *p - 48; + } + + break; + default: + continue; + } + } + + // 处理参数去缺省值的情况 + if (distance == 0) + { + distance = 2; + } + if (total_station == 0) + { + total_station = 5; + } + if(chosen_strategy == -1) + { + chosen_strategy = BUS_FCFS; + } + + BusStrategyBase *controller; + + switch (chosen_strategy) + { + case BUS_FCFS: + controller = new BusFCFSStrategy; + break; + case BUS_SSTF: + controller = new BusSSTFStrategy; + break; + case BUS_SCAN: + controller = new BusSCANStrategy; + default: + controller = nullptr; + break; + } + + if(controller != nullptr) + { + controller->rails_model->CreateRails(distance, total_station); + } + return controller; +} diff --git a/src/railsModel.cpp b/src/railsModel.cpp index 12f9a30..f257a9d 100644 --- a/src/railsModel.cpp +++ b/src/railsModel.cpp @@ -8,6 +8,7 @@ RailsModel::RailsModel() { rails = nullptr; node_num = 0; + total_distance = 0; } RailsModel::~RailsModel() @@ -76,6 +77,7 @@ void RailsModel::CreateRails(int distance, int node_number) rails = head; node_num = node_number; + total_distance = node_number * distance; } void RailsModel::FreeRails()