hdu 1413 文件系统

  此题是一个模拟题,做模拟题,分清条件就可以了。本题我是用c++做的,这样我们可以用string要简便一些。加上vector、map,这样本题就简化多了。具体实现看代码:
#include<iostream>
#include<vector>
#include<string.h>
#include<string>
#include<map>
#include<algorithm>

using namespace std;

struct command{
    vector<string> dir;//当前子目录
vector<string> file;//当前目录下的文件
map<string,int>   num;//对应当前子目录的编号
vector<command *> next;//指向子目录
command *pre;//指向当前目录的上一级
};

command *root;

int Judge(string k,vector<string> p){//执行查找
vector<string>::iterator it;
it=find(p.begin(),p.end(),k);
return it!=p.end();
}

int DElete(string p,string q,command *t){//执行删除和判断
vector<string>::iterator it;
int x,i;
if(q=="file"){
it=find(t->file.begin(),t->file.end(),p);
t->file.erase(it);
}
else{
x=t->num[p];
for(i=0;i<t->next.size() && x>0;i++)x--;
if(t->next[i]->dir.size() || t->next[i]->file.size())
return 0;
it=find(t->dir.begin(),t->dir.end(),p);
t->dir.erase(it);
}
return 1;
}

void make(){
    string p,q;
root=new command();
command *h=root,*t;
int x;
while(cin>>p>>q){
if(p=="CD"){//进入一个子目录
            t=h;
if(q==".." || q=="\\"){
cout<<"success"<<endl;
if(q=="..")
h=t->pre;
else
h=root;
}
else if(Judge(q,t->dir)){
cout<<"success"<<endl;
x=t->num[q];
                t=t->next[x];
h=t;
}
else{
cout<<"no such directory"<<endl;
}
}
else if(p=="MD"){//创建一个子目录
t=h;
if(q==".." || Judge(q,t->dir)){
cout<<"directory already exist"<<endl;
}
else{
                cout<<"success"<<endl;
t->dir.push_back(q);
t->num[q]=t->next.size();
command *newafile=new command();
t->next.push_back(newafile);
t=newafile;
t->pre=h;
}
}
else if(p=="RD"){//删除一个子目录
t=h;
if(Judge(q,t->dir)){
if(DElete(q,"dir",t)){
cout<<"success"<<endl;
}
else
cout<<"can not delete the directory"<<endl;
}
else{
cout<<"can not delete the directory"<<endl;
}
}
else if(p=="CREATE"){//创建一个文件
t=h;
if(Judge(q,t->file)){
                  cout<<"file already exist"<<endl;
}
else
{
t->file.push_back(q);
cout<<"success"<<endl;
}
}
else if(p=="DELETE"){//删除一个文件
t=h;
if(Judge(q,t->file)){
DElete(q,"file",t);
cout<<"success"<<endl;
}
else
                cout<<"no such file"<<endl;
}
}
}

int main(){
    make();
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值