📄 实验4.cpp
字号:
#include "iostream.h"
#include "stdlib.h"
#include "string.h"
#include <iomanip.h>
typedef struct node
{
int suoyin;
char name[20];
int len;
char type[10];
int father;
struct node *next;
int child[20];
int top;
}tree;
tree *root,*rr;
tree *thist=NULL;
int jilu=1;
void init()
{
root=new node;
root->father=0;
root->suoyin=jilu;
strcpy(root->type,"<dir>");
strcpy(root->name,"C:\\");
root->top=-1;
rr=root;
rr->next=NULL;
jilu++;
thist=root;
}
void md()
{
char name[20];
cin>>name;
int flag=0;
tree *yy;
yy=thist->next;
while(yy!=NULL)
{
if(strcmp(yy->name,name)==0&&strcmp(yy->type,"<dir>")==0)
{
flag=1;
break;
}
else
yy=yy->next;
}
if(flag==1)
{
cout<<"子目录或文件 "<<name<<" 已经存在。"<<endl;
}
else
{
tree *t1;
t1=new node;
strcpy(t1->name,name);
strcpy(t1->type,"<dir>");
t1->suoyin=jilu;
t1->father=thist->suoyin;
t1->top=-1;
rr->next=t1;
rr=t1;
rr->next=NULL;
thist->top++;
thist->child[thist->top]=jilu;
jilu++;
}
}
void mk()
{
char name[20];
cin>>name;
int flag=0;
tree *yy;
yy=thist->next;
while(yy!=NULL)
{
if(strcmp(yy->name,name)==0&&strcmp(yy->type,"file")==0)
{
flag=1;
break;
}
else
yy=yy->next;
}
if(flag==1)
{
cout<<"子目录或文件 "<<name<<" 已经存在。"<<endl;
}
else
{
tree *t1;
t1=new node;
strcpy(t1->name,name);
t1->len=rand()%10;
t1->suoyin=jilu;
t1->father=thist->suoyin;
t1->top=-1;
strcpy(t1->type,"file");
rr->next=t1;
rr=t1;
rr->next=NULL;
thist->top++;
thist->child[thist->top]=jilu;
jilu++;
}
}
void cd()
{
char name[20];
cin>>name;
tree *t1;
int flag=0;
if(strcmp(name,"\\")==0)
{
thist=root;
}
else
if(strcmp(name,"..")==0)
{
tree *yy;
yy=root;
while(yy!=NULL)
{
if(yy->suoyin==thist->father)
{
thist=yy;
break;
}
else
yy=yy->next;
}
}
else
{
for(int i=0;i<=thist->top;i++)
{
t1=thist->next;
while(t1!=NULL)
{
if(strcmp(t1->name,name)==0&&strcmp(t1->type,"<dir>")==0)
{
thist=t1;
flag=1;
break;
}
else
t1=t1->next ;
}
}
if(flag==0)
cout<<"系统找不到指定路径。"<<endl;
}
}
void dir()
{
tree *t1;
int m=2,w=0;
cout<<" "<<thist->type<<setw(10)<<"."<<endl;
cout<<" "<<thist->type<<setw(10)<<".."<<endl;
for(int i=0;i<=thist->top;i++)
{
t1=thist->next;
while(t1!=NULL)
{
if(t1->suoyin==thist->child[i])
{
if(strcmp(t1->type,"<dir>")==0)
{
cout<<" "<<setw(10)<<t1->type<<setw(10)<<t1->name<<endl;
m++;
}
else
{
cout<<" "<<setw(10)<<t1->len<<setw(10)<<t1->name<<endl;
w++;
}
break;
}
else
t1=t1->next;
}
}
cout<<setw(20)<<w<<" 个文件"<<endl;
cout<<setw(20)<<m<<" 个目录"<<endl;
}
void rd()
{
char name[20];
cin>>name;
tree *yy,*tmp,*t1;
int flag=0;
yy=thist->next;
while(yy!=NULL)
{
if(strcmp(yy->name,name)==0&&strcmp(yy->type,"<dir>")==0)
{
tmp=yy;
flag=1;
break;
}
else yy=yy->next ;
}
if(flag==0)
cout<<"系统找不到指定文件。"<<endl;
else
{
if(tmp->top!=-1)
cout<<"目录不是空的。"<<endl;
else
{
yy=root;
for(int i=0;i<=jilu;i++)
{
if(yy->suoyin==tmp->suoyin-1)
{
t1=yy;
break;
}
else yy=yy->next ;
}
int j1,j;
for(j=0;j<=thist->top;j++)
{
if(thist->child[j]==tmp->suoyin)
{
j1=j;
break;
}
}
for(j=j1;j<=thist->top;j++)
{
thist->child[j]=thist->child[j+1];
}
if(tmp==rr)
{
rr=t1;
rr->next=NULL;
}
else
t1->next=tmp->next ;
}
jilu--;
}
}
void del()
{
char name[20];
cin>>name;
tree *yy,*tmp,*t1;
int flag=0;
yy=thist;
while(yy!=NULL)
{
if(strcmp(yy->name,name)==0&&strcmp(yy->type,"file")==0)
{
tmp=yy;
flag=1;
break;
}
else yy=yy->next ;
}
if(flag==0)
cout<<"系统找不到指定文件。"<<endl;
else
{
yy=root;
for(int i=0;i<=jilu;i++)
{
if(yy->suoyin==tmp->suoyin-1)
{
t1=yy;
break;
}
else yy=yy->next ;
}
int j1,j;
for(j=0;j<=thist->top;j++)
{
if(thist->child[j]==tmp->suoyin)
{
j1=j;
break;
}
}
for(j=j1;j<=thist->top;j++)
{
thist->child[j]=thist->child[j+1];
}
if(tmp==rr)
{
rr=t1;
rr->next=NULL;
}
else
t1->next=tmp->next ;
jilu--;
}
}
void main()
{
init();
cout<<"C:\\>";
char a[4];
do
{
cin>>a;
if(strcmp(a,"md")==0)
md();
else if(strcmp(a,"cd")==0)
cd();
else if(strcmp(a,"rd")==0)
rd();
else if(strcmp(a,"mk")==0)
mk();
else if(strcmp(a,"dir")==0)
dir();
else if(strcmp(a,"del")==0)
del();
if(thist==root)
cout<<thist->name<<">";
else
cout<<"C:\\"<<thist->name<<">";
}
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -