链表

 

文件系统

 HDU - 1413 

题意:中文题,简单模拟一下文件系统。

orz链表都写不溜啊。。。

http://blog.csdn.net/liang5630/article/details/12419861?locationNum=7&fps=1

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 using namespace std;
  5 
  6 struct Dir{
  7     char name[25];
  8     char filename[1005][25];
  9     Dir *nex[1005];
 10     Dir *fa;
 11     int sum;
 12     int filesum;
 13 }*p;
 14 char note[6][50]={"success","no such directory","directory already exist",
 15                     "can not delete the directory","file already exist","no such file"};
 16 
 17 //CD  Md RD CREAT DELETE
 18 
 19 int main(){
 20     char op[15],name[25];
 21     int flag;
 22     p=new Dir;
 23     Dir *rt=p;
 24     strcpy(rt->name,"\\");
 25     rt->sum=rt->filesum=0;
 26     rt->fa=p;
 27     while(scanf("%s%s",op,name)!=EOF){
 28         if(strcmp(op,"CD")==0){
 29             flag=1;
 30             if(p->sum>0){
 31                 for(int i=0;i<p->sum;i++){
 32                     if(strcmp(p->nex[i]->name,name)==0){  //找到了,进入下一级
 33                         flag=0;
 34                         p=p->nex[i];
 35                         break;
 36                     }
 37                 }
 38             }
 39             if(strcmp(name,"..")==0){
 40                 p=p->fa;
 41                 flag=0;
 42             }
 43             if(strcmp(name,"\\")==0){
 44                 p=rt;
 45                 flag=0;
 46             }
 47             printf("%s\n",note[flag]);
 48         }
 49         else if(strcmp(op,"MD")==0){
 50             flag=0;
 51             if(p->sum>0){
 52                 for(int i=0;i<p->sum;i++){
 53                     if(strcmp(p->nex[i]->name,name)==0){
 54                         flag=2;
 55                         break;
 56                     }
 57                 }
 58             }
 59             if(strcmp(name,"..")==0||strcmp(name,"\\")==0){  //不允许创建..和\子目录
 60                 flag=2;
 61             }
 62             printf("%s\n",note[flag]);
 63             if(flag==0){  //创建子目录成功 ,更新当前目录所包含的文件夹
 64                 Dir *temp=new Dir;
 65                 strcpy(temp->name,name);
 66                 temp->sum=temp->filesum=0;
 67                 temp->fa=p;
 68                 p->nex[p->sum]=temp;
 69                 (p->sum)++;
 70             }
 71         }else if(strcmp(op,"RD")==0){
 72             flag=0;
 73             int i;
 74             if(p->sum>0){
 75                 for(i=0;i<p->sum;i++){
 76                     if(strcmp(p->nex[i]->name,name)==0){
 77                         if(p->nex[i]->sum||p->nex[i]->filesum){  //不是空的
 78                             flag=3;
 79                             break;
 80                         }
 81                         else break;
 82                     }
 83                 }
 84                 if(i==p->sum) flag=3; //没有找到这个文件夹
 85             }else flag=3;  //没有文件夹
 86             printf("%s\n",note[flag]);
 87             if(flag==0){
 88                 for(;i<(p->sum)-1;i++) p->nex[i]=p->nex[i+1];
 89                 (p->sum)--;
 90             }
 91         }else if(strcmp(op,"CREATE")==0){
 92             flag=0;
 93             int i;
 94             if(p->filesum>0){
 95                 for(i=0;i<p->filesum;i++){
 96                     if(strcmp(p->filename[i],name)==0){
 97                         flag=4;break;
 98                     }
 99                 }
100             }
101             printf("%s\n",note[flag]);
102             if(flag==0){
103                 strcpy(p->filename[p->filesum],name);
104                 (p->filesum)++;
105             }
106         }else {
107             flag=5;
108             int i;
109             if(p->filesum>0){
110                 for(i=0;i<p->filesum;i++){
111                     if(strcmp(p->filename[i],name)==0){
112                         flag=0;
113                         break;
114                     }
115                 }
116             }
117             printf("%s\n",note[flag]);
118             if(flag==0){
119                 for(;i<(p->filesum-1);i++) strcpy(p->filename[i],p->filename[i+1]);
120                 (p->filesum)--;
121             }
122         }
123     }
124     return 0;
125 }
View Code

 

posted @ 2017-08-08 13:23  yijiull  阅读(124)  评论(0编辑  收藏  举报