HDU 1413(文件系统)

#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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值