首页 > 题库 > 职业考证 > 软考 > 程序员 > 案例题

阅读下列说明和C++代码,填写代码中的空缺,将解答写入答题纸的对应栏内。
【说明】
某系统需根据类别和内容对数据以文件夹的方式进行组织,数据自身以文件的形式存储,文件夹可以包含文件夹和文件。为该系统设计的相关类及其关系如图6-1所示。

【C++代码】
#include <list>
#include <iostream>
#include <string>
using namespace std;
class Node {
protected :
       string name ;         //文件或文件夹名称public :
       void printName ( ) {cout <<name << endl; }  //打印文件或文件夹名称
       virtual void add (Node *file)=0;         //为一个文件夹增加子文件夹或文件
       virtual void remove (Node *file)=0;   //删除一个文件夹的子文件夹或文件
       virtual list<Node*> *getChildren ( )=0;//获得一个文件夹的子文件夹或文件
};
class File(1)Node {
public :
        File(string name) {(2)= name; }
        void add (Node *file) { return ; }
        void remove (Node *file){ return ; }
        list<Node*> *getChildren ( ) { return (3); }
};
class Folder(4)Node {
private :
        list <Node*> children;      //存储子文件夹或文件
public :
        Folder (string name) {
            this->name = name;
        }

        void add (Node *file) { children.push_back (file);  }
        void remove (Node *file) { children.remove (file) ; }
        list<Node*> *getchildren (){ return(5); }
};

class Client  {   //  构造一个树形文件夹和文件结构

public:
       void creatTree ( ) {
            Node *root = new Folder ( "D:/ " ) ;
            Node *folder = new Folder ( "parent" ) ;
            Node *child = new Folder ( "cxy" );
            Node * file = new File ( "TestFile.cpp" ) ;
            root->add ( folder) ;
            folder->add ( child) ;
            child->add (file) ;
            traverse ( root) ;
        }

void traverse (Node *node)  {   //遍历树形结构
        node->printName ( );
        list<Node*>* children = (6);
        if(children == NULL) return;
        list<Node*> : :iterator it;
        for (it=children->begin ( ) ; it !=children->end ( ) ; it++) {
              traverse ( *it);
        }
    }
};
int main ( ) {
    Client *client = new Client ( ) ;
    client->creatTree ( ) ;


参考答案: 查看答案 查看解析 下载APP畅快刷题

相关知识点试题

相关试卷