题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
#include <stdio.h> #include <string.h> int main() { char command[6][2][22] = { 0 }; char result[7][22] = { 0 }; strcpy(command[0][0], "reset"); strcpy(command[1][0], "reset"); strcpy(command[2][0], "board"); strcpy(command[3][0], "board"); strcpy(command[4][0], "reboot"); strcpy(command[5][0], "backplane"); strcpy(command[1][1], "board"); strcpy(command[2][1], "add"); strcpy(command[3][1], "delete"); strcpy(command[4][1], "backplane"); strcpy(command[5][1], "abort"); strcpy(result[0], "reset what"); strcpy(result[1], "board fault"); strcpy(result[2], "where to add"); strcpy(result[3], "no board at all"); strcpy(result[4], "impossible"); strcpy(result[5], "install first"); char word = getchar(); while (word!= EOF) { char input[21] = { 0 }; char std[2][21] = {0}; int c = 0; int k = 0; int i = 0; for (i = 0; word != '\n'; i++) { input[i] = word; word = getchar(); } for (i = 0;input[i]!=' '&&input[i]!=0; i++) { std[0][c++] = input[i]; } strcpy(std[1], input + i+1); memset(input, 0, sizeof(input)); int flag = strcmp(input, std[1]); if (flag == 0) { char* p = strstr(command[0][0], std[0]); if (p==&command[0][0][0]) printf("%s\n", result[0]); else printf("unknown command\n"); } else { int flag2 = 0; int p1 = 0; for (int i = 1; i <= 5; i++) { char* q = strstr(command[i][0], std[0]); if (q == &command[i][0][0]) { char* q2 = strstr(command[i][1], std[1]); if (q2 == &command[i][1][0]) { p1 = i; flag2++; } } } if (flag2 == 1) { printf("%s\n", result[p1]); } else { printf("unknown command\n"); } } word = getchar(); } return 0; }