配置文件恢复

本文介绍了命令行匹配的基本规则,遵循最短唯一匹配原则。当输入字符串时,系统会根据输入匹配相应的命令。如果输入的字符串能唯一对应一个命令,则执行该命令;否则,可能因不唯一或不存在匹配命令而输出'unkown command'。文中通过实例展示了匹配过程,包括单个字符命令、部分字符串不匹配、两个关键字的匹配情况等。

输入命令   执行命令

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 <iostream>
#include <string>
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<<inputString<<endl;
		temp = inputString;
		
		if(temp.find(' ',0) == string::npos)  //只有一个字符串,只匹配“reset”
		{
			if(temp.size() > command[0].cmd.size() || temp == "")  
			{
				cout<<UK<<endl;
				continue;
			}

			for (j = 0; j < temp.size() && j < command[0].cmd.size(); ++j)
			{
				if(temp[j] != command[0].cmd[j])
				{
					flag = false;
					break;
				}
			}

			if(flag)
			{
				cout<<command[0].op<<endl;
				continue;
			}
			else
			{
				cout<<UK<<endl;
				continue;
			}
		}
		

		if(temp.find(' ',0) != string::npos)  //两个字符串
		{
			index = temp.find(' ',0);
			//cout<<index<<endl;
			temp1.assign(temp,0,index);
			temp2.assign(temp,index+1,temp.size()-index);

			//cout<<temp1.c_str()<<endl;
			//cout<<temp2<<endl;
			for (i = 1; i < 6; ++i)
			{
				if(command[i].cmd.find(temp1.c_str(),0,temp1.size()) != 0)  //第一关键字匹配失败
				{
					//cout<<"no"<<endl;
					continue;
				}
				else 
				{
					//cout<<i<<" "<<command[i].cmd.find(temp1.c_str(),0,temp1.size())<<endl;
					idx1[count1++] = i;   //记录匹配次数
				}
			}

			//cout<<idx1[0]<<endl;
			if(count1 == 0)  //第一关键字匹配失败
			{
				cout<<UK<<endl;
				continue;
			}
			else if(count1 == 1)   //第一关键字匹配唯一
			{
				commandindex = command[idx1[0]].cmd.find(' ',0);
				if(temp2.size() + commandindex + 1> command[idx1[0]].cmd.size()  || temp2 == "")   //第二关键字匹配失败
				{
					cout<<UK<<endl;
					continue;		
				}

				if(command[idx1[0]].cmd.find(temp2.c_str(),commandindex+1,temp2.size()) != commandindex+1)
				{
					cout<<UK<<endl;
					continue;
				}

				cout<<command[idx1[0]].op<<endl;
				continue;
			}
			else  //第一关键字匹配不唯一   
			{
				for (i = 0; i < count1; ++i)
				{
					commandindex = command[idx1[i]].cmd.find(' ',0);
					if(temp2.size() + commandindex + 1> 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<<UK<<endl;
				continue;
			}
			else if(count2 == 1)
			{
				cout<<command[idx2[0]].op<<endl;
				continue;
			}
			
		}

	}
	
	return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值