栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1635 Directory Listing

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1635 Directory Listing

#include<iostream>#include<list>#include<queue>#include<sstream>using namespace std;class treeNode{public:    list<treeNode*> file;    string name;    int size;    int totSize;    bool read();    void calcsize();    void print();    static int recurcalc(treeNode* subtree);    static void recurprint(treeNode* subtree,vector<bool>& vb,bool isLast);};bool treeNode::read(){    string line;    if(getline(cin,line))    {        istringstream iss;        iss.clear();        iss.str(line);        iss >> name >> size; queue<treeNode*> q;        if(name[0] == '*') q.push(this);        while(!q.empty())        { getline(cin,line); bool inP = false; string contain; for(int i = 0; i < line.size(); ++i) {     if(inP == true)     {         if(line[i] == ')')         {  iss.clear();  iss.str(contain);  contain.clear();  string nm;  int sz;  treeNode* father = q.front();  q.pop();  while(iss >> nm >> sz)  {      treeNode* node = new treeNode;      node->name = nm;      node->size = sz;      if(node->name[0] == '*')          q.push(node);      father->file.push_back(node);  }  inP = false;         }         else  contain += line[i];     }     else if(inP == false && line[i] == '(')         inP = true; }        }    }    else        return false;}void treeNode::calcsize(){    recurcalc(this);}int treeNode::recurcalc(treeNode* subtree){    subtree->totSize = subtree->size;    for(list<treeNode*>::iterator p = subtree->file.begin(); p != subtree->file.end(); ++p)        subtree->totSize += recurcalc(*p);    return subtree->totSize;}void treeNode::print(){    vector<bool> vb;    recurprint(this,vb,false);}void treeNode::recurprint(treeNode* subtree,vector<bool>& vb,bool notLast){    for(int i = 0; i < vb.size(); ++i)    {        if(vb[i] == false) cout << " ";        else cout << "|";        cout << "       ";    }    vb.push_back(notLast);    cout << "|_" << subtree->name << "[" << subtree->totSize << "]" << endl;    for(list<treeNode*>::iterator p = subtree->file.begin(); p != subtree->file.end(); ++p)    {        bool tmp;        ++p;        tmp = !(p == subtree->file.end());        --p;        recurprint(*p,vb,tmp);        vb.pop_back();    }}int main(){    while(1)    {        treeNode root;        if(!root.read()) break;        root.calcsize();        root.print();    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372952.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号