Document structure chart

E: File structure "diagram"
View submitted statistical questions
Total time limit: 1000ms memory limit: 65536kB
describe
It is often useful to see the structure of the file system on your computer. The "explorer" program on Microsoft Windows is such an example. However, before there is a graphical interface, there is no graphical representation. At that time, the best way is to display the structure of the directory and file as a "diagram", and use the form of indentation to represent the structure of the directory. For example:

ROOT
| dir1
| file1
| file2
| file3
| dir2
| dir3
| file1
file1
file2
This figure shows that the ROOT directory consists of three subdirectories and two files. The first subdirectory contains three files, the second subdirectory is empty, and the third subdirectory contains one file.

input
Your task is to write a program to read some test data. Each set of test data represents the file structure of a computer. Each group of test data ends with '*', and all reasonable input data ends with '×'. A set of test data includes the names of some files and directories (although we didn't give them in the input, we always assume that the ROOT directory is the outermost directory). In the input, indicate the end of the contents of a directory with ']'. The first letter of the directory name is' d ', and the first letter of the file name is' f'. File names may or may not have extensions (such as fmyfile.dat and fmyfile). The names of files and directories do not include spaces and are no longer than 30. The total number of subdirectories and files in a directory shall not exceed 30.
output
When displaying the contents of a directory, first display its subdirectories (if any), and then display the files (if any). The file is required to be displayed in the alphabetical order of the name (the directory does not need to be displayed in the alphabetical order of the name, but only in the order in which the directory appears). For each set of test data, we need to output "DATA SET x:", where x is the number of test data (starting from 1). Output a blank line between two sets of test data to separate them.

You need to note that we use a '|' and five spaces to represent the indentation level.
sample input
file1
file2
dir3
dir2
file1
file2
]
]
file4
dir1
]
file3
*
file2
file1
*

sample output
DATA SET 1:
ROOT
| dir3
| | dir2
| | file1
| | file2
| dir1
file1
file2
file3
file4

DATA SET 2:
ROOT
file1
file2
Tips
A directory and its subdirectories are at different levels
A directory is at the same level as the files in it
Half failure code
I want to input all the data before * and then output them, but it's more tedious to write, and I finally give up. It's estimated that it will take more than 130 lines to finish the adjustment.

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int k=1;
void root(int i,int t)
{
    if(p[i][0]=='d')
    {
        t++;
        pout(i,t);
        root(i+1,t);
    }
    if(p[i][0]=='f')
    {
        ppout(i,t);
        root(i+1,t);
    }
}
void ppout(int i,int t)
{
    if(p[i][0]=='f')
    {
        ccp[k++]=p[i];
        ppout(i+1,t);
    }
    else
    {
       if(k>1)
        {
            sort(ccp+1,ccp+k);
       for(int ii=1;ii<=k-1;ii++)
       {
       for(int j=1;j<=t;j++)
       {
           cout<<"|     ";
       }
       cout<<ccp[ii]<<endl;
       }
       return ;
        }
        else
        {
            for(int j=1;j<=t;j++)
            {
                cout<<"|     ";
            }
            cout<<ccp[1]<<endl;
        }
    }
}
void pout(int i,int t)
{
    for(int j=1;j<=t;j++)
    {
        cout<<"|     ";
    }
    cout<<p[i]<<endl;
}
int main()
{
    string p[35];
    string cp[35];
    while(1)
    {
        int i=1;
        int l=1;
        int j=1;
        while(1)
        {
            cin>>p[i];
            if(p[i][0]=='f') cp[l++]=p[i];
            if(p[i]=="*"||p[i]=="#") break;
            i++;
        }
        if(p[i]=="#") break;
        if(l>1) sort(cp+1,cp+l);
        cout<<"DATA SET "<<j<<":"<<endl;
        cout<<"ROOT"<<endl;
        int ii=1;
        while(1)
        {
            if(p[ii][0]=='d')
            {
                root(ii,0);
            }
            ii++;
            if(p[ii][0]=='*') break;
        }

        for(int jj=1;jj<=i;jj++)
        {
            cout<<cp[jj]<<endl;
        }

    }
    return 0;
}

You want to use set when writing for the second time, but it's hard to complete if you input all the data before * and then output it,
Pay attention to the requirements in the title. The last time you output a file, you do not need to output all the files that appear in the last time.
Failure code

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
string p[35];
void dout(int i,int t)
{
    for(int j=1;j<=t;j++)
    {
        cout<<"|     ";
    }
    cout<<p[i]<<endl;
}
void fout(int t,set<string>fp)
{
    for(set<string>::iterator it=fp.begin();it!=fp.end();it++)
    {
    for(int j=1;j<=t;j++)
    {
        cout<<"|     ";
    }
    cout<<*it<<endl;
    }
}
void zout(set<string>fp)
{
    for(set<string>::iterator it=fp.begin();it!=fp.end();it++)
    {
        cout<<*it<<endl;
    }
}
void root(int i,int t)
{
    set<string>fp;
    while(1)
    {
        if(p[i][0]=='f')
        {
            fp.insert(p[i]);
        }
        else if(p[i][0]=='d')
        {
            t++;
            dout(i,t);
            root(i+1,t);
        }
        else if(p[i]=="]")
        {
            fout(t,fp);
            t--;
            return ;
        }
        if(p[i]=="*")
        {
            zout(fp);
            return ;
        }
        i++;
    }
}
int main()
{

    int op=0;
    while(1)
    {
        int i=1;
    while(1)
    {
        cin>>p[i];
        if(p[i]=="*"||p[i]=="#"){op++; break;}
        i++;
    }
    if(p[i]=="#") break;
    cout<<"DATA SET "<<op<<":"<<endl;
    cout<<"ROOT"<<endl;
    root(1,0);
    }
    return 0;
}

If you input and output, it's easier
And the condition in the title is that the input file must not be repeated, so
The set in the code can be changed to a priority queue.

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int i=0;
int num=1;
int jud=1;
int op=1;
void pout()
{
    if(op) {cout<<"DATE SET "<<num<<":"<<endl<<"ROOT"<<endl;op=0;}
}
void root()
{
    string p;
    set<string>fp;
    while(cin>>p)
    {
        if(p[0]=='f')
        {
            fp.insert(p);
            pout();
        }
        else if(p[0]=='d')
        {
            pout();
            i++;
            for(int j=1;j<=i;j++)
            {
                cout<<"|     ";
            }
            cout<<p<<endl;
            root();

        }
        else if(p=="]")
        {
            for( set<string>::iterator it=fp.begin();it!=fp.end();it++)
            {
                for(int j=1;j<=i;j++)
            {
                cout<<"|     ";
            }
            cout<<*it<<endl;
            }
            i--;
            return ;
        }
        else if(p=="*")
        {
            for(set<string>::iterator it=fp.begin();it!=fp.end();it++)
            {
                cout<<*it<<endl;
            }
            i=0;
            num++;
            op=1;
            return ;
        }
        else if(p=="#")
        {
            jud=0;
            return ;
        }

    }
}
int main()
{
    while(jud)
    {
        if(jud==0) break;
       // cout<<"DATE SET "<<num<<":"<<endl;
       // cout<<"ROOT"<<endl;
        root();
    }
    return 0;
}

Published 46 original articles, won praise 0, visited 2585
Private letter follow

Tags: Windows

Posted on Tue, 10 Mar 2020 00:22:34 -0700 by ulrikqk