配置文件恢复

输入命令   执行命令

reset"                              "reset what"
"reset board"                 "board fault"
"board add"                    "where to add"
"board delet"                  "no board at all"
"reboot backplane"        "impossible"
"backplane abort"            "install first"

“he he” unkown command

(he he)不是命令

所谓的“最短唯一匹配原则”,仅仅是看是否以某个字符串为开头而已,不需要考虑所匹配的命令长度是否最短且唯一

1、若输入一字符串,则匹配单个字符串的命令,如输入r,找到匹配的命令reset,执行结果为 reset what;

2、若输入一字符串,则不匹配双个字符串的命令,如输入reb,不匹配命令reboot backplane,执行结果为 unkown command

3、若输入两字串,则先匹配第一关键字,如果有匹配但不唯一,继续匹配第二关键字,如果仍不唯一,匹配失败。例如输入:r b,找到匹配命令reset board,执行结果为:board fault。

4、若输入两字串,则先匹配第一关键字,如果有匹配但不唯一,继续匹配第二关键字,如果唯一,匹配成功。例如输入:b a,无法确定是命令board add还是backplane abort,匹配失败。

5、若匹配失败,则输出 unkown command

#include 
#include 
using namespace std;

struct Node
{
	string cmd;
	string op;
};


int main()
{
	bool flag = true;  //保存匹配是否成功
	int count1 = 0;   //计算关键字1匹配次数
	int count2 = 0;   //计算关键字2匹配次数
	int i = 0;
	int j = 0;
	int index = 0;
	int commandindex = 0;
	int idx1[1024] = {0};
	int idx2[1024] = {0};
	string temp = "";
	string temp1 = "";
	string temp2 = "";
	string UK = "unkown command";
	Node command[6] = {
		{"reset","reset what"},
		{"reset board","board fault"},
		{"board add","where to add"},
		{"board delet","no board at all"},
		{"reboot backplane","impossible"},
		{"backplane abort","install first"}
	};

	char inputString[1024] = {0};
	while (cin.getline(inputString,1024))
	{
		memset(idx1,0,1024);
		memset(idx2,0,1024);
		flag = true;
		temp = "";
		temp1 ="";
		temp2 ="";
		count1 = 0;
		count2 = 0;
		//cout< command[0].cmd.size() || temp == "")  
			{
				cout< command[idx1[0]].cmd.size()  || temp2 == "")   //第二关键字匹配失败
				{
					cout< command[idx1[i]].cmd.size() || temp2 == "")   //第二关键字匹配失败
					{
						continue;
					}

					if(command[idx1[i]].cmd.find(temp2.c_str(),commandindex+1,temp2.size()) != commandindex+1)
					{
						continue;
					}
					else
					{
						idx2[count2++] = idx1[i];
					}
				}
			}

			if(count2 != 1)
			{
				cout<


你可能感兴趣的:(软件训练营)