在ZIP归档文件中,保留着所有压缩文件和目录的相对路径和名称。当使用WinZIP等GUI软件打开ZIP归档文件时,可以从这些信息中重建目录的树状结构。请编写程序实现目录的树状结构的重建工作。
输入格式:
输入首先给出正整数N(≤104),表示ZIP归档文件中的文件和目录的数量。随后N行,每行有如下格式的文件或目录的相对路径和名称(每行不超过260个字符):
- 路径和名称中的字符仅包括英文字母(区分大小写);
- 符号“\”仅作为路径分隔符出现;
- 目录以符号“\”结束;
- 不存在重复的输入项目;
- 整个输入大小不超过2MB。
输出格式:
假设所有的路径都相对于root目录。从root目录开始,在输出时每个目录首先输出自己的名字,然后以字典序输出所有子目录,然后以字典序输出所有文件。注意,在输出时,应根据目录的相对关系使用空格进行缩进,每级目录或文件比上一级多缩进2个空格。
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct TNode{
bool type;
string name;
struct TNode *Child_file[500];
struct TNode *Child_mulu[500];
int file;
int mulu;
}TNode,*Root;
int n;
Root root;
TNode *Search(Root Node,string s){
for(int i = 0;i < Node->mulu;i++)
if(Node->Child_mulu[i]->name == s)return Node->Child_mulu[i];
return NULL;
}
TNode *Creat_Mulu(Root &head,string s){
TNode *p = new TNode;
p->name = s;
p->type = true;
p->mulu = 0;
p->file = 0;
head->Child_mulu[head->mulu++] = p;
return p;
}
void Creat_File(Root &head,string s){
TNode *p = new TNode;
p->name = s;
p->type = false;
head->Child_file[head->file++] = p;
}
void Deal(string s){
string str = "";
TNode *Pre = root;
int Time = 0;
Root head = root;
for(int i = 0;i < s.size();i++){
if(s[i] != '\\')str += s[i];
else {
Pre = Search(head,str);
if(!Pre)head = Creat_Mulu(head,str);
else head = Pre;
str = "";
}
}
if(s[s.size() - 1] != '\\'){
Pre = Search(head,str);
if(!Pre)Creat_File(head,str);
}
}
void Initial(){
root = new TNode;
root->name = "root";
root->file = 0;
root->mulu = 0;
root->type = true;
cin>>n;
for(int i = 0;i < n;i++){
string s;
cin>>s;
Deal(s);
}
}
bool cmp(TNode *a,TNode *b){
return a->name < b->name;
}
void Show(Root head,int n){
for(int j = 0;j < n;j++)cout<<" ";
cout<<head->name<<endl;
if(head->type == false)return;
sort(head->Child_file,(head->Child_file) + head->file,cmp);
sort(head->Child_mulu,(head->Child_mulu) + head->mulu,cmp);
for(int i = 0;i < head->mulu;i++)Show(head->Child_mulu[i],n + 1 );
for(int i = 0;i < head->file;i++)Show(head->Child_file[i],n + 1);
}
int main(){
Initial();
Show(root,0);
}
该程序旨在根据ZIP归档文件中的文件和目录数量及其相对路径,重建目录的树状结构。输入包括正整数N及N行路径信息,路径仅包含字母,以''分隔。输出需按字典序展示目录和文件,每级目录或文件比上一级多缩进2个空格。

1828

被折叠的 条评论
为什么被折叠?



