📄 file.cpp
字号:
#include "iostream.h"
#include "stdlib.h"
#include "string.h"
#include "stdio.h"
#include "time.h"
#include "iomanip.h"
typedef struct node
{
char name[80];
int node_number;
int len;
int start;
char type[4];
char time[32]; ///时间
struct node *father,*child,*next;
}inode;
inode *root,*now;
int hao=1;
int FAT[256];
int wst[16][16];
void main()
{
void help();
int change(char c[]);
void init();
void getmulu(inode *p);
void md(char c[]);
void cd(char c[]);
void rd(char c[]);
void mk(char c[]);
void dir(char c[]);
void del(char c[]);
void show();
void weishitu();
help();
init();
weishitu();
int i,j=0;
char c[80],name[80],mingling[10];
do
{
int k=0,t=0,h;
getmulu(now);
cin.getline(c,80);
for(i=0;i<80;i++)
{
if(c[i]==' ')
break;
else
mingling[k++]=c[i];
}
mingling[k]='\0';
for(i=k+1;i<80;i++)
{
if(c[i]=='\0')
break;
else
name[t++]=c[i];
}
name[t]='\0';
h=change(mingling);
switch(h)
{
case 1:
md(name);
break;
case 2:
cd(name);
break;
case 3:
rd(name);
break;
case 4:
mk(name);
break;
case 5:
dir(name);
break;
case 6:
del(name);
break;
case 7:
show();
break;
case 8:
exit(0);
default:
cout<<mingling<<" 不是内部或外部命令,也不是可运行的程序或批处理文件。"<<endl;
break;
}
}while(1);
}
void help()
{
cout<<" -----------------------------help-------------------"<<endl;
cout<<" MD-创建子目录 CD-改变当前目录 "<<endl;
cout<<" RD-删除空子目录 MK-创建一个新文件 "<<endl;
cout<<" DIR-列出当前目录的内容 DEL-删除文件"<<endl;
cout<<" show-显示位示图和FAT信息 exit-退出"<<endl;
cout<<" ----------------------------------------------------"<<endl;
}
void GetCurrentTime(inode *p)
{
char s[32];
time_t tnow = time(0); //得到系统时间
strftime(s , 32 , "%Y-%m-%d %H:%M:%S" , localtime(&tnow)); //根据格式字符串生成字符串
strcpy(p->time,s);
}
void getmulu(inode *p)
{
char s[20][80];
int i=0;
cout<<"C:";
if(p==root)
cout<<"\\";
else
{
while(p!=root)
{
strcpy(s[i],p->name);
i++;
p=p->father;
}
for(int j=i-1;j>=0;j--)
cout<<"\\"<<s[j];
}
cout<<">";
}
int change(char c[])
{
if(strcmp(c,"MD")==0||strcmp(c,"md")==0)
return 1;
if(strcmp(c,"CD")==0||strcmp(c,"cd")==0)
return 2;
if(strcmp(c,"RD")==0||strcmp(c,"rd")==0)
return 3;
if(strcmp(c,"MK")==0||strcmp(c,"mk")==0)
return 4;
if(strcmp(c,"DIR")==0||strcmp(c,"dir")==0)
return 5;
if(strcmp(c,"DEL")==0||strcmp(c,"del")==0)
return 6;
if(strcmp(c,"show")==0||strcmp(c,"SHOW")==0)
return 7;
if(strcmp(c,"EXIT")==0||strcmp(c,"exit")==0)
return 8;
else
return 0;
}
void init()
{
root=(struct node *)malloc(sizeof(inode));
strcpy(root->name,"/");
root->node_number=hao;
strcpy(root->type,"DIR");
root->father=root;
root->next=NULL;
root->child=NULL;
GetCurrentTime(root);
now=root;
}
void md(char c[])
{
inode *p=(struct node *)malloc(sizeof(inode));
inode *q;
int flags=0;
if(now->child!=NULL)
{
inode *s;
q=now->child;
while(q!=NULL)
{
s=q;
if(strcmp(q->name,c)==0&&strcmp(q->type,"DIR")==0)
{
cout<<"子目录或文件 "<<c<<" 已存在。"<<endl;
flags=1;
break;
}
else
q=q->next;
}
if(flags==0)
{
hao++;
s->next=p;
p->next=NULL;
p->father=now;
p->child=NULL;
strcpy(p->name,c);
strcpy(p->type,"DIR");
p->node_number=hao;
GetCurrentTime(p);
}
}
else
{
hao++;
now->child=p;
p->next=NULL;
p->father=now;
p->child=NULL;
strcpy(p->name,c);
strcpy(p->type,"DIR");
p->node_number=hao;
GetCurrentTime(p);
}
}
void cd(char c[])
{
if(strcmp(c,"..")==0)
now=now->father;
else if(strcmp(c,"\\")==0)
now=root;
else
{
inode *p;
p=now;
while(p!=NULL)
{
p=p->child;
if(strcmp(c,p->name)==0&&strcmp(p->type,"DIR")==0)
{
now=p;
break;
}
else
p=p->next;
}
}
}
void dir(char c[])
{
inode *p;
int i,j=0;
cout<<endl;
cout<<" ";
getmulu(now);
cout<<"的目录"<<endl;
if(strcmp(c,"?")==0)
{
i=0;
if(now->child!=NULL)
{
p=now->child;
while(p!=NULL)
{
cout<<p->time<<" <"<<p->type<<"> "<<p->name<<endl;
if(strcmp(p->type,"FIL")==0&&strlen(p->name)==1)
j++;
if(strcmp(p->type,"DIR")==0&&strlen(p->name)==1)
i++;
p=p->next;
}
cout<<" "<<j<<"个名为一个字符的文件"<<endl;
cout<<" "<<i<<"个名为一个字符的目录"<<endl;
}
else
cout<<"空文件夹!"<<endl;
}
else
{
i=2;
cout<<now->time<<" <DIR> ."<<endl;
cout<<now->time<<" <DIR> .."<<endl;
if(now->child!=NULL)
{
p=now->child;
while(p!=NULL)
{
cout<<p->time<<" <"<<p->type<<"> "<<p->name<<endl;
if(strcmp(p->type,"FIL")==0)
j++;
if(strcmp(p->type,"DIR")==0)
i++;
p=p->next;
}
cout<<" "<<j<<"个文件"<<endl;
cout<<" "<<i<<"个目录"<<endl;
}
else
cout<<"空文件夹!"<<endl;
}
}
void rd(char c[])
{
inode *p,*q;
int flag=0;
if(now->child!=NULL)
{
p=now->child;
while(p!=NULL)
{
if(strcmp(p->type,"DIR")==0&&strcmp(p->name,c)==0)
{
if(p->child==NULL)
{
if(p==now->child&&p->next==NULL)
now->child=NULL;
else if(p==now->child&&p->next!=NULL)
{
now->child=p->next;
p->next->next=NULL;
}
else if(p!=now->child&&p->next==NULL)
q->next=NULL;
else
{
q->next=p->next;}
free(p);
flag=1;
break;
}
else
cout<<"目录非空。";
}
else
{q=p;p=p->next;}
}
}
if(flag==0)
{
cout<<"无该文件夹!"<<endl;
}
}
void mk(char c[])
{
void fat(inode *p);
inode *p=(struct node *)malloc(sizeof(inode));
inode *q;
int flags=0;
if(now->child!=NULL)
{
inode *s;
q=now->child;
while(q!=NULL)
{
s=q;
if(strcmp(q->name,c)==0&&strcmp(q->type,"FIL")==0)
{
cout<<"子目录或文件 "<<c<<" 已存在。"<<endl;
flags=1;
break;
}
else
q=q->next;
}
if(flags==0)
{
hao++;
s->next=p;
p->next=NULL;
p->father=now;
p->child=NULL;
strcpy(p->name,c);
strcpy(p->type,"FIL");
srand(time(NULL));
p->len=rand()%10+1;
p->node_number=hao;
GetCurrentTime(p);
fat(p);////////FAT/////////////////
cout<<"文件 "<<c<<"的起始块号:"<<p->start<<" 大小:"<<p->len<<endl;
}
}
else
{
hao++;
now->child=p;
p->next=NULL;
p->father=now;
p->child=NULL;
strcpy(p->name,c);
strcpy(p->type,"FIL");
srand(time(NULL));
p->len=rand()%10+1;
p->node_number=hao;
GetCurrentTime(p);
fat(p);////////FAT///////////
cout<<"文件 "<<c<<"的起始块号:"<<p->start<<" 大小:"<<p->len<<endl;
}
}
void del(char c[])
{
inode *p,*q;
int flag=0;
if(now->child!=NULL)
{
p=now->child;
while(p!=NULL)
{
if(strcmp(p->type,"FIL")==0&&strcmp(p->name,c)==0)
{
if(p==now->child&&p->next==NULL)
now->child=NULL;
else if(p==now->child&&p->next!=NULL)
{
now->child=p->next;
q->next=NULL;
}
else if(p!=now->child&&p->next==NULL)
q->next=NULL;
else
q->next=p->next;
///////////////////////
/* int h=FAT[p->start];
int a=p->start/16;
int b=p->start
weishitu[a]=0;*/
///////////////////////
free(p);
flag=1;
break;
}
else
{q=p;p=p->next;}
}
}
if(flag==0)
{
cout<<"在";
getmulu(now);
cout<<"目录"<<" 中找不到文件"<<c<<endl;
}
}
////////////////////////////////////////
void weishitu()
{
int i,j;
srand(time(NULL));
for(i=0;i<16;i++)
{
for(j=0;j<16;j++)
{
wst[i][j]=rand()%2;
}
}
for(i=0;i<16;i++)
{
for(j=0;j<16;j++)
if(wst[i][j]==1)
FAT[i*16+j]=-2;//被占用
else
FAT[i*16+j]=0;
}
}
void fat(inode *p)
{
int i,j,k,flag1=0,flag2=0,len;
if(p->len==1)
{
for(i=0;i<16;i++)
{
for(j=0;j<16;j++)
if(wst[i][j]==0)
{
p->start=i*16+j;
wst[i][j]=1;
FAT[p->start]=-1;////结束
flag1=1;
break;
}
if(flag1==1)
break;
}
}
else
{
len=p->len;
for(i=0;i<16;i++)
{
for(j=0;j<16;j++)
if(wst[i][j]==0)
{
p->start=i*16+j;
k=p->start;
wst[i][j]=1;
flag1=1;
break;
}
if(flag1==1)
break;
}
for(i=0;i<16;i++)
{
for(j=0;j<16;j++)
if(wst[i][j]==0)
{
if(len!=1)
FAT[k]=i*16+j;
else
{
FAT[k]=-1;
flag2=1;
break;
}
wst[i][j]=1;
k=i*16+j;
len--;
}
if(flag2==1)
break;
}
}
}
void show()
{
int i,j;
cout<<"位示图:"<<endl;
for(i=0;i<16;i++)
{
for(j=0;j<16;j++)
cout<<" "<<wst[i][j]<<" ";
cout<<endl;
}
cout<<"FAT:(0--可用 -1--结束 -2--已被占用):"<<endl;
j=1;
for(i=0;i<256;i++)
{
cout<<resetiosflags(ios::left)<<setw(4)<<FAT[i];
if(j%16==0)
cout<<endl;
j++;
}
}
/////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -