Pacific Northwest 1998 FILE MAPPING

我突然发现一道有趣的题目
建立文件系统并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;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值