题解 | #配置文件恢复#

配置文件恢复

https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5

#include<stdio.h>
#include<string.h>
//定义两个字符串数组
char *str1[5]={"reset","board","board","reboot","backplane"};
char *str2[5]={"board","add","delete","backplane","abort"};

int main(void)
{
    char str[100]={0};
    while (gets(str))
    {
       int len=strlen(str);
       int flag=0;
       int pos;
        for(int i=0;i<len;i++)
        {
            if(str[i]==' ')
            {
                flag++;
                pos=i;
            }
        }
        if(flag==0)//一字串
        {
            char reset[20]="reset";
            if(strncmp(reset,str,len)==0)
            {
                printf("reset what\n");
            }
            else
            {
                printf("unknown command\n");
            }
        }
        else if(flag==1)//两字串
        {
            int cnt=0;
            int result=0;
            for(int i=0;i<5;i++)
            {
               if(strncmp(str,str1[i],pos)==0&&strncmp(str+pos+1,str2[i],len-pos-1)==0) 
               {
                   cnt++;
                   result=i;
               }
            }
            if(cnt==1)
            {
                if(result==0)
                {
                    printf("board fault\n");
                }
                else if(result==1)
                {
                    printf("where to add\n");
                }
                else if(result==2)
                {
                    printf("no board at all\n");
                }
                else if(result==3)
                {
                    printf("impossible\n");
                }
                else if(result==4)
                {
                    printf("install first\n");
                }
            }
            else
            {
                printf("unknown command\n");
            }
            
        }
    }
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
10-15 09:13
已编辑
天津大学 soc前端设计
点赞 评论 收藏
分享
11-28 17:48
中山大学 C++
点赞 评论 收藏
分享
评论
点赞
2
分享
牛客网
牛客企业服务