标签:style blog color os io for 2014 ar
这题是道大模拟= =
代码比较长,并且需要注意的细节很多,各种不合法的命令判定简直巨坑= =
还有一些容易忽视的地方, 比如 new 操作, 竟然会有 new 1 或 new 0 这种丧心病狂的命令, 结果我刚开始的判定方法就崩了= =
最后不得不改成读入一行命令再切割。
我觉得最麻烦的操作就是 back 和 fore 了,刚开始想不明白是什么样子的,建议先做一下 POJ 1028 ,一道模拟浏览器的题。
代码如下:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> #include <vector> #include <stack> using namespace std; int n, historyTop = 0, historyNum = 0; char Cmd[50 + 5], cmd2[50 + 5], cmd3[50 + 5], Prefix[4048 + 10]; struct File { int Type, Size, Depth; char Name[50 + 5]; File *Fa; vector<File *> subFile; } F[2014 + 5], *P = F, *root, *now; stack<File *> foreStack, backStack; void solveNew() { int fileType, spaceLoc; getchar(); gets(cmd2); spaceLoc = 0; while (spaceLoc <= strlen(cmd2) - 1 && cmd2[spaceLoc] != ‘ ‘) spaceLoc++; if (spaceLoc < strlen(cmd2) - 1) { if (cmd2[0] == ‘0‘ && spaceLoc == 1) fileType = 0; else if (cmd2[0] == ‘1‘ && spaceLoc == 1) fileType = 1; else { printf("WRONG COMMAND.\n"); return; } for (int i = 2; i <= strlen(cmd2) - 1; i++) cmd3[i - 2] = cmd2[i]; cmd3[strlen(cmd2) - 1 - 2 + 1] = ‘\0‘; } else { fileType = 0; strcpy(cmd3, cmd2); } for (int i = 0; i <= now -> Size - 1; i++) { if (!strcmp(cmd3, now -> subFile[i] -> Name) && now -> subFile[i] -> Type == fileType) { printf("WRONG COMMAND.\n"); return; } } File *newFile = ++P; newFile -> Type = fileType; newFile -> Size = 0; newFile -> Depth = now -> Depth + 1; strcpy(newFile -> Name, cmd3); newFile -> Fa = now; now -> Size++; now -> subFile.push_back(newFile); if (fileType == 1) { now = newFile; backStack.push(now); while (!foreStack.empty()) foreStack.pop(); } } void solveUp() { if (now -> Fa == NULL) { printf("WRONG COMMAND.\n"); return; } now = now -> Fa; backStack.push(now); while (!foreStack.empty()) foreStack.pop(); } void solveEnter() { char fileName[50 + 5], tempName[50 + 5]; File *p = now; scanf("%s", cmd2); int loc, num; bool flag = false; while (strlen(cmd2)) { if (p -> Size == 0) { printf("WRONG COMMAND.\n"); return; } loc = 0; while (loc <= strlen(cmd2) - 1 && cmd2[loc] != ‘\\‘) loc++; if (loc == 0 || loc == strlen(cmd2)) { printf("WRONG COMMAND.\n"); return; } strncpy(fileName, cmd2, loc); fileName[loc] = ‘\0‘; flag = false; for (int i = 0; i <= p -> Size - 1; i++) { if (!strcmp(fileName, p -> subFile[i] -> Name) && p -> subFile[i] -> Type == 1) { flag = true; p = p -> subFile[i]; break; } } if (!flag) { printf("WRONG COMMAND.\n"); return; } if (loc == strlen(cmd2) - 1) break; for (int i = loc + 1; i <= strlen(cmd2) - 1; i++) tempName[i - loc - 1] = cmd2[i]; num = strlen(cmd2) - loc - 1; strncpy(cmd2, tempName, num); cmd2[num] = ‘\0‘; } now = p; backStack.push(now); while (!foreStack.empty()) foreStack.pop(); } void solveBack() { if (backStack.size() == 1) { printf("WRONG COMMAND.\n"); return; } foreStack.push(backStack.top()); backStack.pop(); now = backStack.top(); } void solveFore() { if (foreStack.size() == 0) { printf("WRONG COMMAND.\n"); return; } now = foreStack.top(); foreStack.pop(); backStack.push(now); } void solvePrint(File *p) { Prefix[p -> Depth * 2] = ‘\0‘; printf("%s", Prefix); Prefix[p -> Depth * 2] = ‘.‘; if (p -> Type == 1) printf("\\"); printf("%s\n", p -> Name); for (int i = 0; i <= p -> Size - 1; i++) solvePrint(p -> subFile[i]); } int main() { freopen("explorer.in", "r", stdin); freopen("explorer.out", "w", stdout); scanf("%d", &n); root = ++P; root -> Type = 1; root -> Size = 0; root -> Depth = 0; strcpy(root -> Name, "exp"); root -> Fa = NULL; now = root; backStack.push(now); for (int i = 1; i <= 4048 + 5; i++) Prefix[i] = ‘.‘; for (int i = 1; i <= n; i++) { scanf("%s", Cmd); if (!strcmp(Cmd, "new")) solveNew(); if (!strcmp(Cmd, "up")) solveUp(); if (!strcmp(Cmd, "enter")) solveEnter(); if (!strcmp(Cmd, "back")) solveBack(); if (!strcmp(Cmd, "fore")) solveFore(); if (!strcmp(Cmd, "print")) solvePrint(root); } fclose(stdin); fclose(stdout); return 0; }
【Lytning Test1】 资源管理器,布布扣,bubuko.com
标签:style blog color os io for 2014 ar
原文地址:http://www.cnblogs.com/F-Magician/p/3895310.html