当前位置: 首页 > 工具软件 > pacific > 使用案例 >

Pacific Northwest 1998 FILE MAPPING

刘骏祥
2023-12-01

我突然发现一道有趣的题目
建立文件系统并tree出来
话说说实在的,做这道题真的没什么意义= =

描述:
It is often helpful for computer users to see a visual representation of the file structure on their computers. The “explorer” in Microsoft Windows is an example of such a system. Before the days of graphical user interfaces, however, such visual representations were not possible. The best that could be done was to show a static “map”of directories and files, using indentation as a guide to directory contents. For example:


ROOT
|        DIR1
|        File1
|        File2
|        File3
|        DIR2
|        DIR3
|        File1
File1
File2

This shows that the root directory contains two files and three subdirectories. The first subdirectory contains 3 files, the second is empty and the third contains one file.

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

本题主要工作在于递归建树,相信对大家难度不大

装逼的面向对象代码。。。。

#include<iostream>
#include<string>
#include<queue>
using namespace std;
#define file 1
#define dir  0
int num;
struct strcmp {
    bool operator ()(string &a, string &b) {
        return a > b;
    }
};
class filesystem {

    struct DIR {
        string name;
        priority_queue<string,vector<string>,struct strcmp>subfiles;
        DIR *subdirs[30]; int cntdir;

        DIR(string name) {
            cntdir = 0;
            this->name = name;
        }
        void getSub() {
            string temp;
            cin >> temp;
            while (temp[0] != ']') {
                if (temp[0] == '*') {
                    output(this);
                    return;
                }
                if (temp[0] == '#')exit(0);
                if (temp[0] == 'f')
                    subfiles.push(temp);
                else subdirs[cntdir] = new DIR(temp),
                    subdirs[cntdir++]->getSub();
                cin >> temp;
            }
        }
    };
public:
    DIR *ROOT;
    filesystem() {
        ROOT = new DIR("ROOT");
        ROOT->getSub();
    }
    friend void output(DIR *target) {
        cout << "DATA SET " << num << ":" << endl;
        output(target, 0);
        cout<<endl;
    }
    friend void output(DIR *test, int level) {
        for (int i = 0; i < level; i++)
            cout << "|     ";
        cout << test->name << endl;
        for (int i = 0; i < test->cntdir; i++) {
            output(test->subdirs[i], level + 1);
        }
        while (!test->subfiles.empty()) {
            for (int i = 0; i < level; i++)
                cout << "|     ";
            cout << test->subfiles.top() << endl;
            test->subfiles.pop();
        }
    }
};
int main() {
    int i = 0;
    while (++i) {
        num = i;
        filesystem test;
    }
}
 类似资料:

相关阅读

相关文章

相关问答