题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
#include <stdio.h>
#include <string.h>
char command[6][50]={"reset","reset board","board add","board delete","reboot backplane","backplane abort"},
execution[7][50]={"reset what","board fault","where to add","no board at all","impossible","install first","unknown command"};
int spacecount[6]={0,1,1,1,1,1}; //空格數,即關鍵詞數
int main() {
char order[21];
while (scanf("%[^\n]", order)!=EOF) {
getchar(); //因為上面讀取到回車符就停止了,所以這裡要讀取掉上面剩下的回車符
char (*pCol)[50]=command;
int i, j, j0, possible_index[6], possible_cnt=0, len=strlen(order), leni, spaceflag=0;
for (i=0; i<6; i++) {
spaceflag=0;
leni=strlen((char*)(pCol+i));
// printf("leni=%d\n", leni);
for (j=0,j0=0; j<=len&&j0<=leni; j++,j0++) {
// printf("j=%d j0=%d order:%c;comm:%c\n", j,j0,order[j],*(*(pCol+i)+j0));
if (order[j]==' ' && j!=0) {
spaceflag++; //記錄輸入指令的空格數
while (*(*(pCol+i)+j0)!=' ' && *(*(pCol+i)+j0)!='\0') {
j0++; //command跳轉到下一個關鍵詞首字母
}
}
else if (order[j]=='\0') {
if (spaceflag!=spacecount[i]) break; //若關鍵詞個數匹配不上則循環中斷
possible_index[possible_cnt]=i; //記錄疑似指令字典
possible_cnt++; //疑似指令字典數+1
break;
}
else if (order[j]==*(*(pCol+i)+j0)) {
continue;
}
else if (order[j]!=*(*(pCol+i)+j0) || *(*(pCol+i)+j0)=='\0') {
break; //若對不上則循環中斷
}
}
}
if (possible_cnt!=1) {
printf("%s\n", execution[6]); //若無匹配則打印未知指令
} else {
printf("%s\n", execution[possible_index[0]]); //打印對應指令
}
}
return 0;
}
