📄 main.cpp
字号:
#include <iostream>
#include <string>
#include "building.h"
using namespace std;
extern Building *get_Stack();
extern Building *get_Queue();
extern Building *get_Opt();
void printErrMes(); //print out error message if the command line arguments are not legal
void printHelp(); //print out help message
int main(int argc, char *argv[])
{
Building* theBuilding;
string infilename="building.txt", outfilename="escape.txt";
bool legal = 0;
for (int i=0; i<argc; i++) //Read command line, check if the infile and outfile are specified
{
if (!strcmp(argv[i], "-H"))
printHelp();
if (!strcmp(argv[i], "-I"))
{
infilename=argv[++i];
for (int j=i+1; j<argc; j++)
{
if( !strcmp(argv[j],"-H")||!strcmp(argv[j],"-I")||!strcmp(argv[j],"-O")
||!strcmp(argv[j],"Stack")||!strcmp(argv[j],"Queue")||!strcmp(argv[j],"Opt"))
break;
else
infilename+=" ";
infilename+=argv[j];
}
}
if (!strcmp(argv[i], "O"))
{
outfilename=argv[++i];
for (int j=i+1; j<argc; j++)
{
if( !strcmp(argv[j],"-H")||!strcmp(argv[j],"-I")||!strcmp(argv[j],"-O")
||!strcmp(argv[j],"Stack")||!strcmp(argv[j],"Queue")||!strcmp(argv[j],"Opt"))
break;
else
outfilename+=" ";
outfilename+=argv[j];
}
}
}
//Read the command line, choose the strategy: stack, queue or opt?
//Use the parameter of file name(if there is one), construct a specified derived class
//to the base class Building, pointing "theBuilding" to it
for (int i=0; i<argc; i++)
{
char str_stack[6], str_queue[6], str_opt[4];
strcpy(str_stack, "Stack");strcpy(str_queue, "Queue");strcpy(str_opt, "Opt");
if (!strcmp(argv[i], str_stack)) //strcmp 当argv[i]与str_stack 相等时返回0
{
if(legal)
printErrMes(); //若legal==1, 已经找到指定的三种策略之一,重复指定为非法
theBuilding =get_Stack();
if (infilename!="building.txt")
*theBuilding = Stack_Building(infilename);
legal=1;
}
if (!strcmp(argv[i], str_queue))
{
if(legal)
printErrMes();
theBuilding=get_Queue();
if (infilename!="building.txt")
*theBuilding =Queue_Building(infilename);
legal = 1;
}
if (!strcmp(argv[i], str_opt))
{
if(legal)
printErrMes();
theBuilding=get_Queue();
if (infilename!="building.txt")
*theBuilding = Queue_Building(infilename); //用queue的算法作为最优算法
legal = 1;
}
}
if(!legal)
printErrMes();
if (!theBuilding->escape())
theBuilding->printMap(outfilename);
else
{
cout<<endl<<"Escaping route is:\n\n";
theBuilding->printMap(outfilename);
}
return 0;
}
void printErrMes()
{
cout<<"Illegal command line arguments!\n";
exit (1);
}
void printHelp()
{
cout<<"INTRODUCTION:\n"
<<"This program finds the way to escape from a building\n"
<<"Start point is \"S\", exit points are \"H\" and \"P\", the route is in the output file, marking each step with the direction.\n"
<<"If there is no way out, a message of failure will be printed.\n"
<<"For more detailed introduction, refer to the project description file.\n\n";
cout <<"Command \"Stack\" will use the stack-based routing scheme"<<endl
<<"Command \"Queue will use the queue-based routing scheme"<<endl
<<"Command \"Opt\" will use the optimal routing scheme"<<endl
<<"The input filename is after the \"I\" flag, if it does not appear, the file \"building.txt\" will be used."<<endl
<<"The output filename is after the \"O\" flag, if it does not appear, the file \"escape.txt\" will be used."<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -