#include <iostream>
#include <sstream>
using namespace std;
class Command {
private:
//输入命令
string command;
//配置命令
static const string c1, c2, c3, c4, c5, c6;
public:
//构造函数
Command(const string s);
//析构函数
~Command();
//匹配命令
const string matchCommand(void)const;
//输出结果
void print(const string s)const;
//配置文件恢复
void recovery(void)const;
//匹配reset
const bool matchReset(const string s)const;
//匹配board
const bool matchBoard(const string s)const;
//匹配add
const bool matchAdd(const string s)const;
//匹配delete
const bool matchDelete(const string s)const;
//匹配reboot
const bool matchReboot(const string s)const;
//匹配backplane
const bool matchBackplane(const string s)const;
//匹配abort
const bool matchAbort(const string s)const;
//匹配两个单词
const string match2(const string s1, const string s2)const;
};
//初始化配置命令
const string Command::c1 = "reset";
const string Command::c2 = "reset board";
const string Command::c3 = "board add";
const string Command::c4 = "board delete";
const string Command::c5 = "reboot backplane";
const string Command::c6 = "backplane abort";
Command::Command(const string s) {
this->command = s;
return;
}
Command::~Command() {
}
const string Command::matchCommand(void) const {
//如果输入的是完整命令,直接返回
if ((this->command == this->c1) || (this->command == this->c2) ||
(this->command == this->c3) || (this->command == this->c4) ||
(this->command == this->c5) || (this->command == this->c6))
return this->command;
//输入重定向到字符串
string s1, s2;
istringstream iss(this->command);
iss >> s1 >> s2;
//如果只有一字串,只有能匹配"reset"时能匹配成功
if (s2 == "") {
if (this->matchReset(s1))
return this->c1;
else return string();
}
//如果输入的是两字串
//如果输入的是完整命令的一部分,返回完整命令
if (this->command == this->c2.substr(0, this->command.size()))
return this->c2;
if (this->command == this->c3.substr(0, this->command.size()))
return this->c3;
if (this->command == this->c4.substr(0, this->command.size()))
return this->c4;
if (this->command == this->c5.substr(0, this->command.size()))
return this->c5;
if (this->command == this->c6.substr(0, this->command.size()))
return this->c6;
//如果输入的两字串分别匹配命令的两个单词且唯一
return this->match2(s1, s2);
}
void Command::print(const string s) const {
if (s == this->c1) {
cout << "reset what" << endl;
return;
}
if (s == this->c2) {
cout << "board fault" << endl;
return;
}
if (s == this->c3) {
cout << "where to add" << endl;
return;
}
if (s == this->c4) {
cout << "no board at all" << endl;
return;
}
if (s == this->c5) {
cout << "impossible" << endl;
return;
}
if (s == this->c6) {
cout << "install first" << endl;
return;
}
cout << "unknown command" << endl;
return;
}
void Command::recovery(void) const {
this->print(this->matchCommand());
return;
}
const bool Command::matchReset(const string s) const {
if (s == this->c1.substr(0, s.size()))
return true;
return false;
}
const bool Command::matchBoard(const string s) const {
if (s == string("board").substr(0, s.size()))
return true;
return false;
}
const bool Command::matchAdd(const string s) const {
if (s == string("add").substr(0, s.size()))
return true;
return false;
}
const bool Command::matchDelete(const string s) const {
if (s == string("delete").substr(0, s.size()))
return true;
return false;
}
const bool Command::matchReboot(const string s) const {
if (s == string("reboot").substr(0, s.size()))
return true;
return false;
}
const bool Command::matchBackplane(const string s) const {
if (s == string("backplane").substr(0, s.size()))
return true;
return false;
}
const bool Command::matchAbort(const string s) const {
if (s == string("abort").substr(0, s.size()))
return true;
return false;
}
const string Command::match2(const string s1, const string s2) const {
//匹配reset board
if (this->matchReset(s1))
if (this->matchBoard(s2)) {
//如果匹配reset board却不匹配reboot backplane
if (!this->matchReboot(s1))
return this->c2;
if (!this->matchBackplane(s2))
return this->c2;
//如果匹配reset board和reboot backplane
return string();
}
//匹配board add
if (this->matchBoard(s1))
if (this->matchAdd(s2)) {
//如果匹配board add却不匹配backplane abort
if (!this->matchBackplane(s1))
return this->c3;
if (!this->matchAbort(s2))
return this->c3;
//如果匹配board add和backplane abort
return string();
}
//匹配board delete
if (this->matchBoard(s1))
if (this->matchDelete(s2))
return this->c4;
//匹配reboot backplane却不匹配reset board
if (this->matchReboot(s1))
if (this->matchBackplane(s2))
return this->c5;
//匹配backplane abort却不匹配board add
if (this->matchBackplane(s1))
if (this->matchAbort(s2))
this->c6;
//现有配置命令都不匹配
return string();
}
int main() {
string a;
while (getline(cin, a)) { // 注意 while 处理多个 case
Command(a).recovery();
}
}
// 64 位输出请用 printf("%lld")