模拟文件目录

#include "iostream"
#include "string"
#include "ctime"
using namespace std;
typedef struct node
{
 int onepoint;    //一个点
 int twopoint;    //两个点
 char attribute;    //属性
 string name;    //名字
 struct node *link;   //链接的目录
 int begin;
 struct node *next;   //下一个节点

}FCB;


int check(char *s)
{
 int i=0;

 if(( s[i]=='c' || s[i]=='C' )&& ( s[i+1]=='D' || s[i+1]=='d'))  //进入目录CD
  return 1;
 else if(( s[i]=='m' || s[i]=='M' )&& ( s[i+1]=='k' || s[i+1]=='K'))  //创建文件MK
  return 2;
 else if(( s[i]=='m' || s[i]=='M' )&& ( s[i+1]=='D' || s[i+1]=='d'))  //创建目录MD
  return 3;
 else if(( s[i]=='r' || s[i]=='R' )&& ( s[i+1]=='m' || s[i+1]=='M'))  //删除目录RM
  return 4;
 else if(( s[i]=='d' || s[i]=='D' )&& ( s[i+1]=='E' || s[i+1]=='e') && ( s[i+2]=='l' || s[i+2]=='L'))
  return 5;  //删除文件del
 else if(( s[i]=='d' || s[i]=='D' )&& ( s[i+1]=='i' || s[i+1]=='I') && ( s[i+2]=='r' || s[i+2]=='R'))
  return 6;  //dir
 else if(s[i]=='e' && s[i+1]=='x' && s[i+2]=='i' && s[i+3]=='t')
  return 0;
 else
  return -1;
}

void display(FCB *ing,int map[][20],int FAT[])
{
 FCB *temp=ing;
 cout<<temp->name<<"的目录:"<<endl;
 cout<<"."<<'/t'<<"<DIR>"<<endl;
 cout<<".."<<'/t'<<"<DIR>"<<endl;
 if(!temp->link)
 {
  while(temp->next)
  {
   temp=temp->next;
   cout<<temp->name<<'/t';

   if(temp->attribute=='D')
    cout<<"<DIR>"<<endl;
   else
   {
    int g=temp->begin;
    cout<<"<FILE>"<<endl;
    cout<<g<<" ";
    while(FAT[g]!=-1)
    {
     cout<<FAT[g]<<" ";
     g=FAT[g];
    }
    cout<<endl;
   }
   
  }
 }
 else
 {
  temp=temp->link;
  while(temp->next)
  {
   temp=temp->next;
   cout<<temp->name<<'/t';

   if(temp->attribute=='D')
    cout<<"<DIR>"<<endl;
   else
   {
    int g=temp->begin;
    cout<<"<FILE>"<<endl;
    cout<<g<<" ";
    while(FAT[g]!=-1)
    {
     cout<<FAT[g]<<" ";
     g=FAT[g];
    }
    cout<<endl;
   }
   
  }
 }

 for(int x=0;x<20;x++)
 {
  for(int y=0;y<20;y++)
  {
   cout<<map[x][y]<<" ";
  }
  cout<<endl;
 }
 cout<<endl;

}

void init(int map[][20],time_t t,int f[])
{
 for(int i=0;i<20;i++)
 {
  for(int j=0;j<20;j++)
  {
   map[i][j]=rand()%2;
  }
 }

 for(i=0;i<400;i++)
  f[i]=-1;

 cout<<"外存空间位示图:"<<endl;

 for(i=0;i<20;i++)
 {
  for(int j=0;j<20;j++)
  {
   cout<<map[i][j]<<" ";
  }
  cout<<endl;
 }

}

FCB *outside;
void slove(FCB *k,int p)
{
 if(k->link->next)
 {
  FCB *st=k->link->next;
  slove(st,p);
 }
 
 while(k)
 {
  if(k->onepoint==p)
   outside=k;
  k=k->next;
 }
}

int main(void)
{
 string now="/";
 time_t lo;
 time(&lo);
 srand(lo);
 int FAT[400];
 int map[20][20];
 init(map,lo,FAT);

 int max_i=0;
 int now_i=0;
 int flag=0;
 FCB beg;
 beg.link=NULL;
 beg.name="/";
 beg.next=NULL;
 beg.onepoint=0;
 beg.twopoint=0;
 beg.attribute='D';
 FCB *doing=&beg;
 char cmd[20];
 cout<<"   目录模拟: Made by LG"<<endl;

 while(cmd!="exit")
 {
  cout<<now<<":";
  cin.getline(cmd,20,'/n');
  flag=check(cmd);
  if(-1==flag)
  {
   cout<<"指令错误,重新输入。"<<endl;
   continue;
  }
  else if(0==flag)
  {
   cout<<"Thanks for using."<<endl;
   exit(0);
  }

  else if(1==flag)    //CD
  {
   string temp(cmd,3,20);
   FCB *t=doing;

   if(temp=="/")
   {
    doing=&beg;
    now_i=doing->onepoint;
    continue;
   }

   if(temp=="..")
   {
    if(doing->name=="/")
     continue;

    int p=doing->twopoint;
    FCB *k=beg.next;

    if(!k)
     continue;
    else
    {
     slove(k,p);
     doing=outside;
     continue;
    }
   }

   while(t)
   {
    if(t->name==temp)
    {
     doing=t->link;
     now_i=t->onepoint;
     break;
    }
    t=t->next;
   }

   if(!t)
   {
    cout<<"输入参数错误。"<<endl;
   }

 

  }

  else if(2==flag)    //MK
  {
   max_i++;
   int num;
   string temp(cmd,3,20);


   FCB *f=new FCB;
   f->attribute='F';
   f->link=NULL;
   f->name=temp;
   f->onepoint=max_i;
   f->twopoint=now_i;
   FCB *last;

   cout<<"输入要创建文件的块数:";
   cin>>num;
   getchar();
   int x,y;
   int ss=num;
   int tai;

   for(x=0;x<20;x++)
   {
    for(y=0;y<20;y++)
    {
     if(map[x][y]==1)
     {
      num--;
      map[x][y]=0;

      if(num==ss-1)
      {
       f->begin=x*20+y;
      }
 
      else
      {
       FAT[tai]=x*20+y;
      }

      tai=x*20+y;
     }

     if(num==0)
     {
      goto cctv;
     }
    }
   }
cctv:  
   FAT[tai]=-1;

   FCB *uu=doing;
   if(uu->next)
   {
    while(uu)
    {
     if(uu->name==temp && uu->attribute=='F')
     {
      cout<<"存在该文件名的文件。"<<endl;
      goto end;
     }
     last=uu;
     uu=uu->next;
    }
    
    if(uu)
    {
     uu->next=f;
     f->next=NULL; 
    }
    else
    {
     last->next=f;
     f->next=NULL;
    }
    

   }

   else
   {
    doing->next=f;
    f->next=NULL;
   }

end:  ;
  }

  else if(3==flag)    //MD
  {
   max_i++;
   string temp(cmd,3,20);
   FCB *f=new FCB;
   f->name=temp;
   f->next=NULL;
   f->onepoint=max_i;
   f->twopoint=now_i;
   f->attribute='D';
   f->link=new FCB;    //为新建立的目录分配空间
   f->link->attribute='D';
   f->link->link=NULL;
   f->link->name=temp;
   f->link->next=NULL;
   f->link->onepoint=max_i;
   f->link->twopoint=now_i;

   if(!doing->next)
   {
    doing->next=f;
    f->next=NULL;
   }
   else
   {
    FCB *u=doing;

    while(u->next)
    {
     u=u->next;
    }

    u->next=f;
    f->next=NULL;
   }

  }

  else if(4==flag)    //RM
  {
   string temp(cmd,3,20);
   FCB *u=doing;
   FCB *w,*last=doing;
   if(u->next)
   {
    while(u)
    {
     if(u->name==temp && u->attribute=='F')
     {
      cout<<"指令错误。(rm删除目录)."<<endl;
      break;
     }

     if(u->name==temp && u->attribute=='D')
     {
      w=u->link;
      if(w->next)
      {
       cout<<"该目录不为空,不能删除."<<endl;
       break;
      }

      else
      {
       last->next=u->next;
       delete u;
       break;
      }
     }
     
     last=u;
     u=u->next;
    }
   }

   else
   {
    cout<<"该目录为空。"<<endl;
   }


  }

  else if(5==flag)    //DEL
  {
   string temp(cmd,4,20);

   FCB *u=doing->next;
   FCB *last=doing;

   if(u)
   {
    while(u)
    {
     if(u->name==temp && u->attribute=='D')
     {
      cout<<"指令错误。(del删除文件)."<<endl;
      break;
     }

     if(u->name==temp && u->attribute=='F')
     {
      int x,y;
      while(u->begin!=-1)
      {
       y=u->begin%20;
       x=u->begin/20;
       map[x][y]=1;
       u->begin=FAT[u->begin];
      }
      last->next=u->next;
      delete u;
      break;
     }
     
     last=u;
     u=u->next;
    }

   }

   else
   {
    cout<<"该目录下不存在文件."<<endl;
   }
   
  }

  else if(6==flag)    //DIR
  {
   display(doing,map,FAT);
   cout<<endl;
  }

 


 }


 return 0;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值