共计 4738 个字符,预计需要花费 12 分钟才能阅读完成。
#include<bits/stdc++.h>
using namespace std;
/**
* command:
* cd [dirname]
* to shift to a name_given dir
* mkdir [dirname]
* create a new dir under current dir
* (&md)
* touch [filename]
* create a new file under current file
* (&touhc)
* ls dir
* list the dir under current dir in specific order
* (&ld)
* ls file
* list the file under current dir
* (&lf)
* shut
* shut to have a good day without IT productions
* ret
* directly return to the Desktop
* don't ask me why i develop this command
* just because IIII HATEEEE inputing "Desktop"!!!!!
* */
int dircnt=1; //the number of dir
int now=1; //the dir's ID which you are now
int fa[100010]; //fa for Union-Find
map<string,int>mp; //give it a name turned out an ID
struct directory{
string dirname; //its name
int dep; //the dep in the dir_tree
vector<int>son; //its son_dir
//give it a name turned out an ID of file
map<string,int>mp_file;
//its son_file
vector<string>son_file;
int filecnt=0; //the number of file
};
directory a[100010]; //struct for dirs
struct file{
string filename;
};
file b[100010]; //struct for file
int tfc=0; //overall file count
//for overall file conduction
map<pair<int,string> ,int>tmf;
deque<int>pos; // your current path
int pos_size=1; // the size of path
void mkdir(){//"mkdir"
string new_name;
cin>>new_name;
if(mp[new_name]){//SPJ for [under]
cout<<"already have this directory!"<<endl;
return;
}
dircnt++;
mp[new_name]=dircnt; //ID
fa[ dircnt ] = now; //son and father
a[now].son.push_back(dircnt); //contain ID
a[dircnt].dep=a[now].dep+1; //dep
a[dircnt].dirname=new_name; //dir_name
cout<<"Successfully make a directory named\""<<new_name<<"\""<<endl;
}
int find_dep(int x,int to_dep){
if(a[x].dep==to_dep)return x;
return find_dep(fa[x],to_dep);
}
void change_dir(){//"cd"
string to_name;
cin>>to_name;
if(mp[to_name]==0){//SPJ for [under] invalid case
cout<<"you don't have such a directory ,which named \""<<to_name<<"\""<<endl;
return;
}
int to=mp[to_name],to_dep=a[to].dep;
now=to;
cout<<"Loading..."<<endl;
pos.clear();//as you see
pos_size=0;
for(int i=1;i<=to_dep;i++){//start from Desktop (time_eff not good(no care))
pos.push_back(find_dep(to,i));
pos_size++;
//dont forget pos_size
//cout<<"id:"<<find_dep(to,i)<<" and dir_name:"<<a[find_dep(to,i)].dirname<<endl;
////test [up];
}
cout<<"Successfully changed to \""<<to_name<<"\""<<endl;
}
void ret(){//"ret"
string to_name="Desktop";
now=1;
cout<<"Loading..."<<endl;
pos.clear();
pos_size=1;
pos.push_back(1);
cout<<"Successfully changed to \""<<to_name<<"\""<<endl;
}
int overall_now;
void list_dir(int now,int blk){//"ls dir"
cout<<a[now].dirname;
if(a[now].son.empty() and now == overall_now){
cout<<endl;
return;
}
if(a[now].son.empty())return;
cout<<"--";
// int delta=0/*,cnt=1*/;
// for(auto v:a[now].son)delta=max(delta,int(a[v].dirname.size())+2);
// that's not true[up]
for(int i=0;i<(int)a[now].son.size();i++){
if(i==(int)a[now].son.size()-1)cout<<"|-";
else cout<<"| ";
int v=a[now].son[i];
list_dir(v,blk+(int)a[v].dirname.size()+4);
if(i==(int)a[now].son.size()-1)continue;
else cout<<endl<<string(blk,' ');
}
cout<<endl;
return ;
}
void list_fl(int now){//"ls file"
printf("LISTING...:\n");
if((int)a[now].son_file.size()==0){
cout<<"However , you have no file under this directory... :("<<endl;
return;
}
for(auto s:a[now].son_file){
cout<<string(5,' ')<<s<<endl;
}
cout<<"total:"<<a[now].filecnt<<endl;
return;
}
void touch(int now,string new_name){
if(a[now].mp_file[new_name])
{//SPJ
cout<<"Already have such a file named \""<<new_name<<"\""<<endl;
// printf("Already have such a file named \"")//fail...
return;
}
a[now].mp_file[new_name]=++a[now].filecnt; //for dir
a[now].son_file.push_back(new_name); //for dir's filr's name(for ls fl)
tmf[make_pair(now,new_name)]=++tfc; //for allover
b[tfc].filename=new_name; //contain its name
printf("Successully created a file named \"%s\"\n",new_name.c_str());
return;
}
void init_system(){//as you see
for (int i=0;i<=100009;i++)fa[i]=i; //Union-find
mp["Desktop"]=1; //desk_id
a[1].dirname="Desktop"; //desk_name
a[1].dep=1; //desk_dep
now=1; //cur_pos
pos.push_back(1); //cur_path
cout<<endl;
cout<<"<Desktop>:";
}
void print_path(){
cout<<"<";
for(int i=1;i<=pos_size;i++){
cout<<a[pos.front()].dirname;
pos.push_back(pos.front());
pos.pop_front();
if(i!=pos_size)cout<<"/";
//SPJ
//using a deque to turn it aside to output
//your current path
}
cout<<">:";
}
int main(){
init_system();
while(1){
string tmp;
cin>>tmp;
if(isdigit(tmp[0])){
cout<<tmp<<endl;
print_path();
}
if(!isdigit(tmp[0])){
if(tmp=="ret"){
ret();
print_path();
continue;
}
if(tmp=="mkdir" or tmp=="md"){
mkdir();
print_path();
continue;
}
if(tmp=="cd"){
change_dir();
print_path();
continue;
}
if(tmp=="ls"){
string ty;
cin>>ty;
if(ty=="dir") overall_now=now,list_dir(now,a[now].dirname.size()+2);
if(ty=="file") list_fl(now);
print_path();
continue;
}
if(tmp=="ld"){
overall_now=now,list_dir(now,a[now].dirname.size()+2);
print_path();
continue;
}
if(tmp=="lf"){
list_fl(now);
print_path();
continue;
}
if(tmp=="touch" or tmp=="touhc"){
string ty;
cin>>ty;
touch(now,ty);
print_path();
continue;
}
if(tmp=="shut"){
cout<<endl<<"Shut down the system successfully!"<<endl;
cout<<"Have a good day!"<<endl;
exit(0);
}
cout<<"Such Command is not exist!"<<endl<<endl;
print_path();
}
}
return 0;
}
/**
* g++ a.cxx
* ./a.out
*
*
**/
正文完

