注册 登录
编程论坛 C++教室

[原创][讨论]关于文件管理系统的数据结构模拟

wfpb 发布于 2007-06-21 22:25, 3440 次点击
windows操作系统中的文件夹的数据结构貌似树,不是二叉树,而是普通数.
可以如下设计,不过只是框架,很多都没有考虑到
template <class T>
class DIRSTRUCT
{   
    DIRSTRUCT * lpParentDir;
    vector<DIRSTRUCT*> SubDir;
    int m_nSubDir;
    T file;
private:
    void DisPlay(const DIRSTRUCT<T>* lpDir)
    {        
        cout<<lpDir->file<<endl;
        for(int i=0;i<lpDir->m_nSubDir;i++)
        {
            DisPlay(lpDir->SubDir[i]);
        }
    }
    void Delete(DIRSTRUCT<T> *lpDir)
    {
        if(lpDir->m_nSubDir)
        {
            for(int i=lpDir->m_nSubDir-1;i>=0;i--)
            {
                Delete(lpDir->GetChild(i));
                delete lpDir->GetChild(i);
                lpDir->SubDir.pop_back();
            }
        }
        lpDir->lpParentDir=NULL;
        lpDir->m_nSubDir=0;
    }
    void Clone(DIRSTRUCT<T> *lp,const DIRSTRUCT<T> *lpDir)
    {
        for(int i=0;i<lpDir->m_nSubDir;i++)
        {
            lp->AddChild(lpDir->SubDir[i]->file);
            Clone(lp->SubDir[i],lpDir->SubDir[i]);
        }
    }
public:
    DIRSTRUCT<T>()
    {
        file=0;
        m_nSubDir=0;
        lpParentDir=NULL;
    }
    DIRSTRUCT<T>(T t,DIRSTRUCT* lpParent=NULL)
    {
        m_nSubDir=0;
        lpParentDir=lpParent;
        file=t;
    }
    DIRSTRUCT(const DIRSTRUCT<T>& dir)
    {
        Copy(&dir);
    }
    DIRSTRUCT<T> &operator=(const DIRSTRUCT<T>& dir)
    {
        Copy(&dir);
        return *this;
    }
    ~DIRSTRUCT<T>()
    {
        remove();
    }
    void Copy(const DIRSTRUCT<T>* lpDir)
    {
        remove();
        file=lpDir->file;
        Clone(this,lpDir);
    }
public:
    void AddChild(T t)
    {
        m_nSubDir++;
        DIRSTRUCT* lpDir=new DIRSTRUCT<T>(t,this);
        SubDir.push_back(lpDir);
    }
    int GetCount()
    {
        return SubDir.size();
    }
    DIRSTRUCT<T>* GetChild(int i)
    {
        return SubDir.at(i);
    }
    DIRSTRUCT<T>* operator[](int i)
    {
        return SubDir.at(i);
    }
    DIRSTRUCT<T>* GetParent()
    {
        return lpParentDir;
    }
    bool IsChild()
    {
        return lpParentDir;
    }
    void show()
    {
        DisPlay(this);
    }
    void remove()
    {
        Delete(this);
    }
};


测试代码,主要是针对几个构造函数,'='的测试。

void main()
{
    DIRSTRUCT<int> dir;
    dir.AddChild(1);
    dir.AddChild(2);
    dir[0]->AddChild(11);
    DIRSTRUCT<int> dir2;
    dir.show();
    cout<<endl;
    dir2.show();
    cout<<endl;
    dir2.Copy(&dir);
    dir2.show();
    dir.remove();
    cout<<endl;
    dir2.show();
    cout<<endl;
    dir.show();
    dir.Copy(&dir2);
    cout<<endl;
    dir.show();
    DIRSTRUCT<int> dir3(dir);
    cout<<endl;
    dir3.show();
    DIRSTRUCT<int> dir4;
    dir4=dir3;
    cout<<endl;
    dir4.show();
    cout<<endl;
    dir4.GetChild(0)->show();
    cout<<endl;
    dir4.GetChild(0)->GetParent()->show();
}

[此贴子已经被作者于2007-6-21 22:26:35编辑过]

7 回复
#2
aipb20072007-06-22 09:39
大致浏览了下。

想给你跪下了,呵呵~写的很好啊,很清晰,不过有没有错误就不知道了。
希望继续完善!哈哈~

[此贴子已经被作者于2007-6-22 9:39:48编辑过]

#3
wfpb2007-06-22 23:22

你过奖了,要是有兴趣,大家可以一起找错误,一起完善啊。。。

#4
wfpb2007-06-23 00:54

头文件
//-------------_dir.h//

#ifndef    _DIR_STRUCT_
#define    _DIR_STRUCT_


#include <vector>
#include <iostream>
#include <string>
using namespace std;


const char* sep=\"\t\";
template <class T>
class DIRSTRUCT
{   
    DIRSTRUCT * lpParentDir;
    vector<DIRSTRUCT*> SubDir;
    int m_nSubDir;
    vector<T> data;
    friend ostream& operator<<(ostream &os,DIRSTRUCT<T> & dir);    //输出函数
private:
    static void DisPlay(ostream &os,DIRSTRUCT<T>* lpDir);        //显示辅助函数
    static void Delete(DIRSTRUCT<T> *lpDir);            //删除辅助函数
    static void Clone(DIRSTRUCT<T> *lp,const DIRSTRUCT<T> *lpDir);    //拷贝辅助函数
public:
    DIRSTRUCT<T>(DIRSTRUCT* lpParent=NULL,vector<T> t=vector<T>());    //构造函数
    DIRSTRUCT(const DIRSTRUCT<T>& dir);                //拷贝构造函数
    DIRSTRUCT<T> &operator=(DIRSTRUCT<T>& dir);            //赋值
    void Copy(const DIRSTRUCT<T>* lpDir);                //拷贝
    ~DIRSTRUCT<T>();                        //析构函数
public:
    void AddChild();                        //添加空子目录
    void AddChild(vector<T> t);                    //添加有数据的子目录
    void AddChild(DIRSTRUCT<T> dir);                //添加完整子目录(包含该子目录的子目录)
    void AddValue(T t);                        //添加数据
    int find(T t);                            //查找指定数据,返回索引
    T& GetValue(int i);                        //索引数据集
    int DataCount();                        //数据集数目
    int DirCount();                            //子目录数
    DIRSTRUCT<T>* GetChild(int i);                    //索引子目录
    DIRSTRUCT<T>* operator[](int i);                //索引子目录
    DIRSTRUCT<T>* GetParent();                    //获取父目录
    bool IsChild();                            //是否为子目录
    bool IsChild(DIRSTRUCT<T>* lpDir);                //是否为lpDir的子孙目录
    void ClearValue();                        //清空数据
    void clear();                            //清空整个结构
};


#endif    //_DIR_STRUCT_


实现文件
//-------------dir.h-------------//
程序代码:

#ifndef    _DIR_IMPLENT_
#define    _DIR_IMPLENT_

#include \"_dir.h\"
template <class T>
ostream& operator<<(ostream &os,DIRSTRUCT<T> & dir)
{
    DIRSTRUCT<T>::DisPlay(os,&dir);
    return os;
}


template <class T>
void DIRSTRUCT<T>::DisPlay(ostream &os,DIRSTRUCT<T>* lpDir)
{        
    for(int j=0;j<lpDir->DataCount();j++)
        os<<lpDir->GetValue(j)<<sep;
    os<<endl;
    if(!lpDir->m_nSubDir)
        return;
    for(int i=0;i<lpDir->m_nSubDir;i++)
    {
        DisPlay(os,lpDir->SubDir[i]);
    }
}


template <class T>
void DIRSTRUCT<T>::Delete(DIRSTRUCT<T> *lpDir)
{
    if(lpDir->m_nSubDir)
    {
        for(int i=lpDir->m_nSubDir-1;i>=0;i--)
        {
            lpDir->ClearValue();
            delete lpDir->GetChild(i);
            lpDir->SubDir.pop_back();
        }
    }
    lpDir->lpParentDir=NULL;
    lpDir->m_nSubDir=0;
}


template <class T>
void DIRSTRUCT<T>::Clone(DIRSTRUCT<T> *lp,const DIRSTRUCT<T> *lpDir)
{
    for(int i=0;i<lpDir->m_nSubDir;i++)
    {
        lp->AddChild(lpDir->SubDir[i]->data);
        Clone(lp->SubDir[i],lpDir->SubDir[i]);
    }
}


template <class T>
DIRSTRUCT<T>::DIRSTRUCT(DIRSTRUCT* lpParent,vector<T> t)    //构造函数
{
    m_nSubDir=0;
    lpParentDir=lpParent;
    data=t;
}


template <class T>
DIRSTRUCT<T>::DIRSTRUCT(const DIRSTRUCT<T>& dir)        //拷贝构造函数
{
    Copy(&dir);
}


template <class T>
DIRSTRUCT<T>& DIRSTRUCT<T>::operator=(DIRSTRUCT<T>& dir)    //赋值
{
    if(!IsChild(&dir))
        Copy(&dir);
    return *this;
}


template <class T>
void DIRSTRUCT<T>::Copy(const DIRSTRUCT<T>* lpDir)    //拷贝
{
    clear();
    data=lpDir->data;
    Clone(this,lpDir);
}


template <class T>
DIRSTRUCT<T>::~DIRSTRUCT<T>()            //析构函数
{
    clear();
}


template <class T>
void DIRSTRUCT<T>::AddChild()            //添加空子目录
{
    m_nSubDir++;
    DIRSTRUCT* lpDir=new DIRSTRUCT<T>();
    SubDir.push_back(lpDir);
}


template <class T>
void DIRSTRUCT<T>::AddChild(vector<T> t)    //添加有数据的子目录
{
    m_nSubDir++;
    DIRSTRUCT* lpDir=new DIRSTRUCT<T>(this,t);
    SubDir.push_back(lpDir);
}


template <class T>
void DIRSTRUCT<T>::AddChild(DIRSTRUCT<T> dir)    //添加完整子目录(包含该子目录的子目录)
{
    m_nSubDir++;
    DIRSTRUCT<T>* pSubdir=new DIRSTRUCT<T>();
    *pSubdir=dir;
    SubDir.push_back(pSubdir);
}


template <class T>
void DIRSTRUCT<T>::AddValue(T t)    //添加数据
{
    data.push_back(t);
}


template <class T>
int DIRSTRUCT<T>::find(T t)        //查找指定数据,返回索引
{
    for(int i=0;i<DataCount();i++)
        if(GetValue(i)==t)
            return i;
        return -1;   
}


template <class T>
T& DIRSTRUCT<T>::GetValue(int i)    //索引数据集
{
    return data.at(i);
}


template <class T>
int DIRSTRUCT<T>::DataCount()        //数据集数目
{
    return data.size();
}


template <class T>
int DIRSTRUCT<T>::DirCount()        //子目录数
{
    return SubDir.size();
}


template <class T>
DIRSTRUCT<T>* DIRSTRUCT<T>::GetChild(int i)    //索引子目录
{
    return SubDir.at(i);
}


template <class T>
DIRSTRUCT<T>* DIRSTRUCT<T>::operator[](int i)    //索引子目录
{
    return SubDir.at(i);
}


template <class T>
DIRSTRUCT<T>* DIRSTRUCT<T>::GetParent()    //获取父目录
{
    return lpParentDir;
}


template <class T>
bool DIRSTRUCT<T>::IsChild()        //是否为子目录
{
    return lpParentDir;
}


template <class T>
bool DIRSTRUCT<T>::IsChild(DIRSTRUCT<T>* lpDir)        //是否为lpDir的子孙目录
{
    for(DIRSTRUCT<T>*pDir=this;pDir;pDir=pDir->GetParent())
        if(pDir==lpDir)
            return true;
    return false;
}


template <class T>
void DIRSTRUCT<T>::ClearValue()    //清空数据
{
    data.clear();
}


template <class T>
void DIRSTRUCT<T>::clear()        //清空整个结构
{
    Delete(this);
}
#endif //_DIR_IMPLENT_


使用时#include "dir.h"

[此贴子已经被作者于2007-6-23 11:34:08编辑过]

#5
璇璇贺贺2007-07-09 18:35

晕了,得让我研究几个月估计能看懂一些。


我得慢慢学习…………………………………………






#6
野比2007-07-09 21:18
应该会很受欢迎...
#7
luckyday2007-07-10 10:06
赞一个
#8
lj22602011-11-23 15:15
不错,可以学习学习。
1