比较容易想到的办法:取输入字符串第一行和第二行分别与命令串比较,判断是不是其前缀子串。再统计匹配成功次数,成功两次且唯一就输出。
也是因为对C库函数不太了解的原因,老老实实做的,代码写得太多了,最开始的命令串声明改成二维数组存储,代码量会少很多。
/*只能判断串中间最多只有一个空格的字符串*/
#include <stdio.h>
#include <string.h>

char s1[]="reset";
char s2[]="reset board";
char s3[]="board add";
char s4[]="board delete";
char s5[]="reboot backplane";
char s6[]="backplane abort";

int check(char*a,char* b,int n,int step){//判断两个字符串其中n位是否相等
    if(step==1){//只有一个串的情况
        int flag=0;
        for(int i=0;i<n;i++){
            if(a[i]!=b[i]){
                flag=1;
                break;
            }
        }
        if(!flag){
            return 1;
        }
        return 0;
    }
    else{//判断第二个串
        int j;
        if(*&a==&s2 || *&a==&s3 || *&a==&s4){
            j=6;
        }
        else if(*&a==&s5){
            j=7;
        }
        else{
            j=10;
        }
        int flag=0;
        for(int i=0;i<n;i++){
            if(a[i+j]!=b[i]){
                flag=1;
                break;
            }
        }
        if(!flag){
            return 1;
        }
        return 0;
    }
}

int main(){
    char cmd[20];
    while(fgets(cmd,20,stdin)!=NULL){
        int len=strlen(cmd)-1,i;
        char t[20]={0};
        int tab[7]={0};//建立匹配检查表
        int flag=0;//表示是否有两字符串
        for(i=0;i<len;i++){//截取输入字符串cmd[]空格前的字符存入t[]
            if(cmd[i]!=' '){
                t[i]=cmd[i]; 
            }
            else{
                flag=1;
                break;
            }
        }
        if(!flag){//只有一个字符串,对比是否是s1的字串
            int x=check(s1,t,len,1);
            if(x){
                printf("reset what");
            }
            else{
                printf("unknown command");
            }
        }
        else{//有两个字符串时,检查匹配情况
            tab[2]=check(s2,t,i,1);
            tab[3]=check(s3,t,i,1);
            tab[4]=check(s4,t,i,1);
            tab[5]=check(s5,t,i,1);
            tab[6]=check(s6,t,i,1);
            for(int k=2;k<7;k++){//检查第一个串是否相匹配
                int flag1=0;
                if(tab[k]){
                    flag=1;
                    break;
                }
            }
            if(!flag){
                printf("unknown command");
            }
            else{
                for(int k=i+1;k<len;k++){//截取输入字符串cmd[]空格后的字符存入t[]
                    t[k-i-1]=cmd[k];
                }
                i=len-i-1;
                if(check(s2,t,i,2)){
                    tab[2]++;
                }
                if(check(s3,t,i,2)){
                    tab[3]++;
                }
                if(check(s4,t,i,2)){
                    tab[4]++;
                }
                if(check(s5,t,i,2)){
                    tab[5]++;
                }
                if(check(s6,t,i,2)){
                    tab[6]++;
                }
                int temp,flag1=0;
                for(int k=2;k<7;k++){//检查第二个串是否相匹配
                    if(tab[k]==2){
                        if(++flag1==2){
                            printf("unknown command");
                            break;
                        }
                        temp=k;
                    }
                }
                if(flag1==1){
                    switch(temp){
                        case 2:printf("board fault");break;
                        case 3:printf("where to add");break;
                        case 4:printf("no board at all");break;
                        case 5:printf("impossible");break;
                        case 6:printf("install first");break;
                    }
                }
                else if(!flag1){
                    printf("unknown command");
                }
            }
        }
        printf("\n");
    }
}