1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| #include <cstdio> #include <cstring> #include <string> #include <iostream> #include <cstdlib> #include <map> #include <set> #include <vector>
using namespace std;
struct Block { int type; char name[256]; Block *root; Block *father; vector<Block *> son;
Block(Block *fa, char name[], int type) { strcpy(this->name, name); root = fa->root; father = fa; this->type = type; }
Block() { strcpy(name, "\\"); type = 0; root = this; father = this;
}
Block *find(char name[], int type) {
if (strcmp(name, "\\") == 0) return root; if (strcmp(name, "..") == 0) return father; for (Block *bl:son) { if (strcmp(bl->name, name) == 0 && bl->type == type) return bl; } return NULL; }
int md(char name[]) { if (find(name, 0) != NULL) { cout << "directory already exist" << endl; return 1; } else { Block *tmp = new Block(this, name, 0); son.push_back(tmp); cout << "success" << endl; return 0; } }
int rd(char name[]) { Block *p = find(name, 0); if (p == NULL || p == father || p == root || p->son.size() > 0) { cout << "can not delete the directory" << endl; return 1; } else { for (vector<Block *>::iterator it = son.begin(); it != son.end(); it++) { if (*it == p) { son.erase(it); delete p; break; } } cout << "success" << endl; } }
int ls() { for (Block *bl:son) { cout << bl->type << " " << bl->name << endl; } }
int cre(char name[]) { if (find(name, 1) != NULL) { cout << "file already exist" << endl; return 1; } else { Block *tmp = new Block(this, name, 1); son.push_back(tmp); cout << "success" << endl; return 0; } }
int del(char name[]) { Block *p = find(name, 1); if (p == NULL) { cout << "no such file" << endl; return 1; } else { for (vector<Block *>::iterator it = son.begin(); it != son.end(); it++) { if (*it == p) { son.erase(it); delete p; break; } } cout << "success" << endl; } }
int pwd() { if (this == root) { cout << "\\"; return 0; } else { father->pwd(); cout << name << "\\"; } return 0; } }; int cd(Block*&cur,char to[]) { Block* p=cur->find(to,0); if (p==NULL) { cout<<"no such directory"<<endl; return 1; }else { cur=p; cout<<"success"<<endl; return 0; } }
int main() { Block * root = new Block; Block * dir=root; char a[100],b[100]; while (cin>>a) { if (strcmp(a,"CD")==0) { cin>>b; cd(dir,b); } else if (strcmp(a,"MD")==0){ cin>>b; dir->md(b); } else if (strcmp(a,"LS")==0) { dir->ls(); } else if (strcmp(a,"RD")==0) { cin>>b; dir->rd(b); } else if (strcmp(a,"CREATE")==0) { cin>>b; dir->cre(b); }else if (strcmp(a,"DELETE")==0) { cin>>b; dir->del(b); } else if (strcmp(a,"PWD")==0){ dir->pwd(); cout<<endl; } } return 0; }
|