模拟文件目录
来源:互联网 发布:天书世界机甲进阶数据 编辑:程序博客网 时间:2024/06/05 16:32
#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;
}
- 模拟文件目录
- 【模拟】【文件目录】【七中联考】
- [原创]内存文件读写类CMemoryFile(内存中模拟目录和文件)
- android 模拟机 无法push文件到system目录报out of memory错误
- 文件目录
- 文件目录
- 文件目录
- 文件目录
- 目录-文件
- 目录文件
- 文件目录
- 访问文件目录,文件
- 文件复制,目录到目录
- Java面试题---用IO流模拟剪切文件功能(把一文件中所有的文件剪切到指定目录中)
- CFileDialog 打开目录文件 保存目录文件
- C#-文件目录-文件目录操作
- CFileDialog 打开目录文件 保存目录文件
- CFileDialog 打开目录文件 保存目录文件
- asp.net实现无刷新DropDownList联动控件
- 使Eclipse更高效的N个使用技巧
- 无刷新仿google波形扭曲彩色Asp.net验证码
- 网站优化的十大奇招妙技(二)
- openSuSE 下面配置 java环境变量
- 模拟文件目录
- 什么都不懂
- 设备管理
- 网站优化的十大奇招妙技(三)
- 客户每次请求Web页面过程
- 第一个100
- 网站优化的十大奇招妙技(四)
- Java专题 - Java内部类 - Java中的内部类和匿名类(1)
- 今天参加了marvell的培训与研讨会