本程序实现Linux系统中最常用的新建文件夹、文件,删除文件夹、文件,复制文件夹、文件,剪切文件夹和文件,以及查看当前目录或者是某一个目录中的文件和文件夹,进入一个文件夹,以及退出一个目录,查看当前的文件路径等等。
类图的简单介绍:
框架图:
函数框架图:
实验效果图:
最终说明一下:这里面新建文件夹和文件的时候,考虑了文件的重复性,但是复制和剪切的时候没有考虑(偷懒了一下,实际上实现也不是很难,有机会补上!)
代码实现:
struct.h
class file
{
public:
string name;
file* next;
file()
{
name="";
next=NULL;
}
file(string _name)
{
name=_name;
next=NULL;
}
};
class Dir
{
public:
string name;
Dir* next;
Dir* linkDir;
file* curFile;
Dir(string _name)
{
name=_name;
next=NULL;
linkDir=NULL;
curFile=NULL;
}
Dir()
{
name="";
next=NULL;
linkDir=NULL;
curFile=NULL;
}
}root,*curDir;
filesys.h
#include<iostream>
#include<string>
#include<vector>
using namespace std;
#include"struct.h"
void menu();
void init_root();
void select_menu();
void mkdir(string s,Dir* d);
void mkdir(string s,Dir* d,Dir* curdir);
void touch(string s,file* f);
void ls();
void ls(string s);
void cd(string s);
void cd2point();
void pwd();
void rm(string s);
void rmr(string s);
void rmrf(string s);
void mv(string sOri,string sAim);
void mvr(string sOri,string sAim);
void cpr(string sOri,string sAim);
void cp(string sOri,string sAim);
void copyDir(Dir*curdir,Dir* copydir);
void showline();
Dir* findpath(vector<string> vs);
file* readFile(string s,Dir* start);
Dir* readDir(string s,Dir* start);
Dir* cdpath(string& s);
vector<Dir*> path;
filesys.cpp
#include"filesys.h"
void menu()
{
cout<<"*********************************************"<<endl;
cout<<"*mkdir 新建文件夹*"<<endl;
cout<<"*touch 新建文件*"<<endl;
cout<<"*ls 查看当前目录的文件和文件夹*"<<endl;
cout<<"*cd 进入一个目录*"<<endl;
cout<<"*pwd 查看当前路径*"<<endl;
cout<<"*rm 删除文件*"<<endl;
cout<<"*rm -r 删除文件夹*"<<endl;
cout<<"*rm -rf 强行删除文件夹*"<<endl;
cout<<"*cp 复制文件*"<<endl;
cout<<"*cp -r 复制文件夹*"<<endl;
cout<<"*mv 剪切文件*"<<endl;
cout<<"*mv -r 剪切文件夹*"<<endl;
cout<<"*********************************************"<<endl;
}
void init_root()
{
root.name="root";
root.curFile=NULL;
root.next=NULL;
curDir=&root;
}
void select_menu()
{
init_root();
menu();
string instruct;
vector<string> v_instr;
vector<string>::iterator iter;
showline();
while(getline(cin,instruct))
{
//split instruct
while(1)
{
int pos=instruct.find(" ");
if(pos==0)
{
instruct=instruct.substr(1);
continue;
}
if(pos<0)
{
v_instr.push_back(instruct);
break;
}
v_instr.push_back(instruct.substr(0,pos));
instruct=instruct.substr(pos+1);
}
if(v_instr[0]=="mkdir")
{
for (int i = 1; i < v_instr.size(); i++)
{
Dir* d=new Dir();
mkdir(v_instr[i],d);
}
}
else if(v_instr[0]=="touch")
{
for (int i = 1; i < v_instr.size(); i++)
{
file* f=new file();
touch(v_instr[i],f);
}
}
else if(v_instr[0]=="ls")
{
if(v_instr.size()==1)
ls();
else
ls(v_instr[1]);
}
else if(v_instr[0]=="cd"&&v_instr[1]!="..")
{
cd(v_instr[1]);
}
else if(v_instr[0]=="cd.."||(v_instr[0]=="cd"&&v_instr[1]==".."))
{
cd2point();
}
else if(v_instr[0]=="pwd")
{
pwd();
}
else if(v_instr[0]=="rm"&&v_instr[1]!="-r"&&v_instr[1]!="-rf")
{
for (int i = 1; i < v_instr.size(); i++)
{
rm(v_instr[i]);
}
}
else if(v_instr[0]=="rm"&&v_instr[1]=="-r")
{
for (int i = 2; i < v_instr.size(); i++)
{
rmr(v_instr[i]);
}
}
else if(v_instr[0]=="rm"&&v_instr[1]=="-rf")
{
for (int i = 2; i < v_instr.size(); i++)
{
rmrf(v_instr[i]);
}
}
else if(v_instr[0]=="cp")
{
if(v_instr.size()==1)
{
cout<<"commend is not right!"<<endl;
}
else
{
if(v_instr[1]=="-r")
{
if(v_instr.size()!=4)
{
cout<<"commend is not right!"<<endl;
}
else
{
cpr(v_instr[2],v_instr[3]);
}
}
else
{
if(v_instr.size()!=3)
{
cout<<"commend is not right!"<<endl;
}
else
{
cp(v_instr[1],v_instr[2]);
}
}
}
}
else if(v_instr[0]=="mv")
{
if(v_instr.size()==1)
{
cout<<"commend is not right!"<<endl;
}
else
{
if(v_instr[1]=="-r")
{
if(v_instr.size()!=4)
{
cout<<"commend is not right!"<<endl;
}
else
{
mvr(v_instr[2],v_instr[3]);
}
}
else
{
if(v_instr.size()!=3)
{
cout<<"commend is not right!"<<endl;
}
else
{
mv(v_instr[1],v_instr[2]);
}
}
}
}
else if(v_instr[0]=="test")
{
Dir* _dir=new Dir();
cout<<(_dir->next==NULL)<<endl;
copyDir(curDir->next,_dir);
cout<<endl;
}
else if(v_instr[0]=="exit")
{
break;
}
else
{
cout<<"commend is not found!"<<endl;
}
v_instr.clear();
showline();
}
}
void mkdir(string s,Dir* tmp)
{
Dir* foundDir=NULL;
vector<string> vstmp;
if(s[0]!='/')
{
foundDir=curDir;
}
else
{
while(1)
{
int pos=s.find('/');
if(pos==0)
{
s=s.substr(1);
continue;
}
if(pos<0)
{
break;
}
vstmp.push_back(s.substr(0,pos));
s=s.substr(pos+1);
}
foundDir=findpath(vstmp);
}
if(foundDir==NULL)
{
cout<<"directory has not exsited!"<<endl;
return;
}
Dir* tmp_curDir=foundDir;
if(tmp_curDir->next==NULL)
{
tmp_curDir->next=tmp;
tmp_curDir->next->name=s;
}
else
{
tmp_curDir=tmp_curDir->next;
if(s==tmp_curDir->name)
{
cout<<"This directory has existed!"<<endl;
return;
}
while (tmp_curDir->linkDir!=NULL)
{
tmp_curDir=tmp_curDir->linkDir;
if(s==tmp_curDir->name)
{
cout<<"This directory has existed!"<<endl;
return;
}
}
tmp_curDir->linkDir=tmp;
tmp_curDir->linkDir->name=s;
}
}
void mkdir(string s,Dir* tmp,Dir* curdir)
{
Dir* tmp_curDir=curdir;
if(tmp_curDir->next==NULL)
{
tmp_curDir->next=tmp;
tmp_curDir->next->name=s;
}
else
{
tmp_curDir=tmp_curDir->next;
if(s==tmp_curDir->name)
{
cout<<"This directory has existed!"<<endl;
return;
}
while (tmp_curDir->linkDir!=NULL)
{
tmp_curDir=tmp_curDir->linkDir;
if(s==tmp_curDir->name)
{
cout<<"This directory has existed!"<<endl;
return;
}
}
tmp_curDir->linkDir=tmp;
tmp_curDir->linkDir->name=s;
}
}
void touch(string s,file* f)
{
Dir* foundDir=NULL;
vector<string> vstmp;
if(s[0]!='/')
{
foundDir=curDir;
}
else
{
while(1)
{
int pos=s.find('/');
if(pos==0)
{
s=s.substr(1);
continue;
}
if(pos<0)
{
break;
}
vstmp.push_back(s.substr(0,pos));
s=s.substr(pos+1);
}
foundDir=findpath(vstmp);
}
if(foundDir==NULL)
{
cout<<"directory has not exsited!"<<endl;
return;
}
Dir* tmp_curDir=foundDir;
if(tmp_curDir->curFile==NULL)
{
tmp_curDir->curFile=f;
tmp_curDir->curFile->name=s;
}
else
{
file* tmpf=tmp_curDir->curFile;
if(s==tmpf->name)
{
cout<<"This file has existed!"<<endl;
return;
}
while (tmpf->next!=NULL)
{
tmpf=tmpf->next;
if(s==tmpf->name)
{
cout<<"This file has existed!"<<endl;
return;
}
}
tmpf->next=f;
tmpf->next->name=s;
}
}
void ls()
{
Dir* tmp_curDir=curDir;
if(tmp_curDir->curFile!=NULL)
{
file* f = tmp_curDir->curFile;
cout<<"file:"<<endl;
cout<<f->name<<" ";
while (f->next!=NULL)
{
f=f->next;
cout<<f->name<<" ";
}
cout<<endl;
}
if(tmp_curDir->next!=NULL)
{
tmp_curDir=tmp_curDir->next;
cout<<"directory:"<<endl;
cout<<tmp_curDir->name<<" ";
while (tmp_curDir->linkDir!=NULL)
{
tmp_curDir=tmp_curDir->linkDir;
cout<<tmp_curDir->name<<" ";
}
cout<<endl;
}
}
void ls(string s)
{
Dir* foundDir=NULL;
vector<string> vstmp;
if(s[0]!='/')
{
foundDir=curDir;
}
else
{
while(1)
{
int pos=s.find('/');
if(pos==0)
{
s=s.substr(1);
continue;
}
if(pos<0)
{
vstmp.push_back(s);
break;
}
vstmp.push_back(s.substr(0,pos));
s=s.substr(pos+1);
}
foundDir=findpath(vstmp);
}
if(foundDir==NULL)
{
cout<<"directory has not exsited!"<<endl;
return;
}
Dir* tmp_curDir=foundDir;
if(tmp_curDir->curFile!=NULL)
{
file* f = tmp_curDir->curFile;
cout<<"file:"<<endl;
cout<<f->name<<" ";
while (f->next!=NULL)
{
f=f->next;
cout<<f->name<<" ";
}
cout<<endl;
}
if(tmp_curDir->next!=NULL)
{
tmp_curDir=tmp_curDir->next;
cout<<"directory:"<<endl;
cout<<tmp_curDir->name<<" ";
while (tmp_curDir->linkDir!=NULL)
{
tmp_curDir=tmp_curDir->linkDir;
cout<<tmp_curDir->name<<" ";
}
cout<<endl;
}
}
void cd(string s)
{
Dir* tmp_curDir=curDir;
bool flag=false;
if(tmp_curDir->next!=NULL)
{
tmp_curDir=tmp_curDir->next;
if(tmp_curDir->name==s)
{
path.push_back(tmp_curDir);
flag=true;
}
while(flag==false&&tmp_curDir->linkDir!=NULL)
{
tmp_curDir=tmp_curDir->linkDir;
if(tmp_curDir->name==s)
{
path.push_back(tmp_curDir);
flag=true;
}
}
}
if(flag==true)
{
curDir=path[path.size()-1];
}
else
{
cout<<"This directory has not existed!"<<endl;
}
}
void cd2point()
{
if(path.size()>1)
{
path.pop_back();
curDir=path[path.size()-1];
}
else if(path.size()==1)
{
path.pop_back();
curDir=&root;
}
else
{
curDir=&root;
}
}
file* readFile(string s,Dir* start)
{
Dir* tmpDir=start;
file* tmpfile=NULL;
file* foundfile=NULL;
bool flag=false;
if(tmpDir->curFile!=NULL)
{
tmpfile=tmpDir->curFile;
if(tmpfile->name==s)
{
foundfile=tmpfile;
flag=true;
}
while (flag==false&&tmpfile->next!=NULL)
{
tmpfile=tmpfile->next;
if(tmpfile->name==s)
{
foundfile=tmpfile;
flag=true;
}
}
}
return foundfile;
}
Dir* readDir(string s,Dir* start)
{
Dir* tmpDir=start;
Dir* founddir=NULL;
bool flag=false;
if(tmpDir->next!=NULL)
{
tmpDir=tmpDir->next;
if(tmpDir->name==s)
{
founddir=tmpDir;
flag=true;
}
while(flag==false&&tmpDir->linkDir!=NULL)
{
tmpDir=tmpDir->linkDir;
if(tmpDir->name==s)
{
founddir=tmpDir;
flag=true;
}
}
}
return founddir;
}
void pwd()
{
cout<<"/root";
for (int i = 0; i < path.size(); i++)
{
cout<<"/"<<path[i]->name;
}
cout<<endl;
}
void rm(string s)
{
Dir* foundDir=NULL;
vector<string> vstmp;
if(s[0]!='/')
{
foundDir=curDir;
}
else
{
while(1)
{
int pos=s.find('/');
if(pos==0)
{
s=s.substr(1);
continue;
}
if(pos<0)
{
break;
}
vstmp.push_back(s.substr(0,pos));
s=s.substr(pos+1);
}
foundDir=findpath(vstmp);
}
file* tmpf=readFile(s,foundDir);
Dir* tmpDir=foundDir;
if(tmpf==NULL)
{
cout<<s<<" has not exsited!"<<endl;
}
else
{
if(tmpDir->curFile==tmpf)
{
tmpDir->curFile=tmpf->next;
}
else
{
file* found=tmpDir->curFile;
while (found->next!=NULL)
{
if(found->next==tmpf)
{
found->next=tmpf->next;
break;
}
found=found->next;
}
}
}
}
Dir* findpath(vector<string> vs)
{
Dir* tmpDir=NULL;
for (int i = 0; i < vs.size(); i++)
{
if(i==0)
{
if(vs[0]!="root")
break;
else
tmpDir=curDir;
}
else
{
tmpDir=readDir(vs[i],tmpDir);
if(curDir==NULL)
break;
}
}
return tmpDir;
}
void rmr(string s)
{
Dir* foundDir=NULL;
vector<string> vstmp;
if(s[0]!='/')
{
foundDir=curDir;
}
else
{
while(1)
{
int pos=s.find('/');
if(pos==0)
{
s=s.substr(1);
continue;
}
if(pos<0)
{
break;
}
vstmp.push_back(s.substr(0,pos));
s=s.substr(pos+1);
}
foundDir=findpath(vstmp);
}
Dir* tmpDir=foundDir;
Dir* aimDir=readDir(s,foundDir);
if(aimDir->curFile!=NULL||aimDir->next!=NULL)
{
cout<<"the directory is not empty!"<<endl;
return;
}
if(tmpDir->next==NULL||aimDir==NULL)
{
cout<<s<<" has not exsited!"<<endl;
}
else
{
if(tmpDir->next==aimDir)
{
tmpDir->next=aimDir->linkDir;
}
else
{
Dir* found=tmpDir->next;
while (found->linkDir!=NULL)
{
if(found->linkDir==aimDir)
{
found->linkDir=aimDir->linkDir;
break;
}
found=found->linkDir;
}
}
}
}
void rmrf(string s)
{
Dir* foundDir=NULL;
vector<string> vstmp;
if(s[0]!='/')
{
foundDir=curDir;
}
else
{
while(1)
{
int pos=s.find('/');
if(pos==0)
{
s=s.substr(1);
continue;
}
if(pos<0)
{
break;
}
vstmp.push_back(s.substr(0,pos));
s=s.substr(pos+1);
}
foundDir=findpath(vstmp);
}
Dir* tmpDir=foundDir;
Dir* aimDir=readDir(s,foundDir);
if(tmpDir->next==NULL||aimDir==NULL)
{
cout<<s<<" has not exsited!"<<endl;
}
else
{
if(tmpDir->next==aimDir)
{
tmpDir->next=aimDir->linkDir;
}
else
{
Dir* found=tmpDir->next;
while (found->linkDir!=NULL)
{
if(found->linkDir==aimDir)
{
found->linkDir=aimDir->linkDir;
break;
}
found=found->linkDir;
}
}
}
}
Dir* cdpath(string& s)
{
Dir* foundDir=NULL;
vector<string> vstmp;
if(s[0]!='/')
{
foundDir=curDir;
}
else
{
while(1)
{
int pos=s.find('/');
if(pos==0)
{
s=s.substr(1);
continue;
}
if(pos<0)
{
break;
}
vstmp.push_back(s.substr(0,pos));
s=s.substr(pos+1);
}
foundDir=findpath(vstmp);
}
return foundDir;
}
void cp(string sOri,string sAim)
{
Dir* oriDir=cdpath(sOri);
Dir* aimDir=cdpath(sAim);
file* oriFile=readFile(sOri,oriDir);
file* copyoriFile=new file();
copyoriFile->name=oriFile->name;
aimDir=readDir(sAim,aimDir);
if(aimDir->curFile==NULL)
{
aimDir->curFile=copyoriFile;
}
else
{
file* aimFile=aimDir->curFile;
while (aimFile->next!=NULL)
{
aimFile=aimFile->next;
}
aimFile->next=copyoriFile;
}
}
void cpr(string sOri,string sAim)
{
Dir* oriDir=cdpath(sOri);
Dir* aimDir=cdpath(sAim);
oriDir=(oriDir==NULL)?&root:readDir(sOri,oriDir);
aimDir=(aimDir==NULL)?&root:readDir(sAim,aimDir);
Dir* copyDri=new Dir();
copyDri->name=oriDir->name;
copyDir(oriDir,copyDri);
if(aimDir->next==NULL)
{
aimDir->next=copyDri;
}
else
{
aimDir=aimDir->next;
while (aimDir->linkDir!=NULL)
{
aimDir=aimDir->linkDir;
}
aimDir->linkDir=copyDri;
}
}
void copyDir(Dir*curdir,Dir* copydir)
{
Dir* tmpcopydir=copydir;
if(curdir->next!=NULL)
{
curdir=curdir->next;
Dir* tmp=new Dir();
mkdir(curdir->name,tmp,copydir);
copyDir(curdir,copydir->next);
Dir* tmpcopydir=copydir->next;
while (curdir->linkDir!=NULL)
{
curdir=curdir->linkDir;
Dir* tmp=new Dir();
mkdir(curdir->name,tmp,copydir);
copyDir(curdir,tmpcopydir->linkDir);
tmpcopydir=tmpcopydir->linkDir;
}
}
}
void mv(string sOri,string sAim)
{
Dir* oriDir=cdpath(sOri);
Dir* aimDir=cdpath(sAim);
aimDir=(aimDir==NULL)?&root:readDir(sAim,aimDir);
file* oriFile=readFile(sOri,oriDir);
file* aimFile=NULL;
if(aimDir->curFile==NULL)
{
aimDir->curFile=oriFile;
}
else
{
aimFile=aimDir->curFile;
while (aimFile->next!=NULL)
{
aimFile=aimFile->next;
}
aimFile->next=oriFile;
}
//delete file
if(oriDir->curFile==oriFile)
{
oriDir->curFile=oriFile->next;
oriFile->next=NULL;
}
else
{
file* tmpf=oriDir->curFile;
while (tmpf->next!=NULL)
{
if(tmpf->next==oriFile)
{
tmpf->next=oriFile->next;
oriFile->next=NULL;
break;
}
tmpf=tmpf->next;
}
}
}
void mvr(string sOri,string sAim)
{
string tmpsOri=sOri;
string tmpsAim=sAim;
Dir* oriDir=cdpath(sOri);
cpr(tmpsOri,tmpsAim);
oriDir=(oriDir==NULL)?&root:oriDir;
Dir* found=readDir(sOri,oriDir);
if(oriDir->next==found)
{
oriDir->next=found->linkDir;
}
else
{
Dir* tmporiDir=oriDir->next;
while (tmporiDir->linkDir!=NULL)
{
if(tmporiDir->linkDir==found)
{
tmporiDir->linkDir=found->linkDir;
break;
}
tmporiDir=tmporiDir->linkDir;
}
}
/*
Dir* oriDir=cdpath(sOri);
Dir* aimDir=cdpath(sAim);
Dir* tmporiDir=oriDir;
oriDir=(oriDir==NULL)?&root:readDir(sOri,oriDir);
aimDir=(aimDir==NULL)?&root:readDir(sAim,aimDir);
tmporiDir=(tmporiDir==NULL)?&root:tmporiDir;
if(aimDir->next==NULL)
{
aimDir->next=oriDir;
}
else
{
aimDir=aimDir->next;
while (aimDir->linkDir!=NULL)
{
aimDir=aimDir->linkDir;
}
aimDir->linkDir=oriDir;
}
if(tmporiDir->next==oriDir)
{
tmporiDir->next=oriDir->linkDir;
//oriDir->linkDir==NULL;
if(aimDir->next==oriDir)
{
aimDir=aimDir->next;
aimDir->linkDir=NULL;
}
else if(aimDir->linkDir==oriDir)
{
aimDir=aimDir->linkDir;
aimDir->linkDir=NULL;
}
}
else
{
tmporiDir=tmporiDir->next;
while (tmporiDir->linkDir!=NULL)
{
if(tmporiDir->linkDir==oriDir)
{
tmporiDir->linkDir=oriDir->linkDir;
oriDir->linkDir=NULL;
break;
}
tmporiDir=tmporiDir->linkDir;
}
}
*/
}
void showline()
{
cout<<"/root";
for (int i = 0; i < path.size(); i++)
{
cout<<"/"<<path[i]->name;
}
cout<<":>";
}
int main()
{
select_menu();
}