#include <iostream>
#include <cstring>
using namespace std;
struct file //文件
{
char name[100]; //文件名
file* next; //同级下一个文件
};
struct dir //文件夹(目录)
{
char name[100];
dir* next; //同级下一个文件夹
dir* up; //上级文件夹
dir* down; //下级文件夹
file* downFile; //下级文件
};
dir* root, * now;
//查找下级文件夹s
dir* searchDir(char* s)
{
dir* p = now->down->next;
while (p)
{
if (!strcmp(p->name, s))
return p;
p = p->next;
}
return NULL;
}
//查找下级文件s
file* searchFile(char* s)
{
file* p = now->downFile->next;
while (p)
{
if (!strcmp(p->name, s))
return p;
p = p->next;
}
return NULL;
}
//进入下级文件夹
void doCD()
{
char s[100];
cin >> s;
if (!strcmp(s, "\\"))
{
now = root;
cout << "success" << endl;
return;
}
if (!strcmp(s, ".."))
{
now = now->up;
cout << "success" << endl;
return;
}
dir* p = searchDir(s);
if (!p)
{
cout << "no such directory" << endl;
return;
}
now = p;
cout << "success" << endl;
}
//建立新文件夹
void doMD()
{
char s[100];
cin >> s;
if (!strcmp(s, "..") || !strcmp(s, "\\"))
{
cout << "directory already exist" << endl;
return;
}
dir* p, * q;
q = searchDir(s);
if (q)
{
cout << "directory already exist" << endl;
return;
}
p = new dir;
strcpy(p->name, s);
p->down = new dir;
p->down->next = NULL;
p->downFile = new file;
p->downFile->next = NULL;
p->up = now;
p->next = now->down->next;
now->down->next = p;
cout << "success" << endl;
}
//删除文件夹
void doRD()
{
char s[100];
cin >> s;
dir* p, * q;
p = searchDir(s);
if (!p || p->down->next)
{
cout << "can not delete the directory" << endl;
return;
}
q = now->down;
while (q)
{
if (q->next == p)
{
q->next = p->next;
delete p;
cout << "success" << endl;
return;
}
q = q->next;
}
}
//建立新文件
void doCREATE()
{
char s[100];
cin >> s;
file* p = searchFile(s);
if (p)
{
cout << "file already exist" << endl;
return;
}
p = new file;
strcpy(p->name, s);
p->next = now->downFile->next;
now->downFile->next = p;
cout << "success" << endl;
return;
}
//删除文件
void doDELETE()
{
char s[100];
cin >> s;
file* p = searchFile(s), * q;
if (!p)
{
cout << "no such file" << endl;
return;
}
q = now->downFile;
while (q)
{
if (q->next == p)
{
q->next = p->next;
delete p;
cout << "success" << endl;
return;
}
q = q->next;
}
}
void init()
{
root = new dir;
root->down = new dir;
root->downFile = new file;
root->next = NULL;
root->up = root;
now = root;
root->down->next = NULL;
root->downFile->next = NULL;
}
int main()
{
init();
char s[10];
while (cin >> s)
{
if (!strcmp(s, "CD"))
doCD();
else if (!strcmp(s, "MD"))
doMD();
else if (!strcmp(s, "RD"))
doRD();
else if (!strcmp(s, "CREATE"))
doCREATE();
else if (!strcmp(s, "DELETE"))
doDELETE();
}
return 0;
}