| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3440 人关注过本帖, 1 人收藏
标题:[原创][讨论]关于文件管理系统的数据结构模拟
只看楼主 加入收藏
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏(1)
 问题点数:0 回复次数:7 
[原创][讨论]关于文件管理系统的数据结构模拟
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编辑过]

搜索更多相关主题的帖子: 数据结构 文件管理 二叉树 windows 操作系统 
2007-06-21 22:25
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
大致浏览了下。

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

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


Fight  to win  or  die...
2007-06-22 09:39
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 

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


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-06-22 23:22
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 

头文件
//-------------_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编辑过]


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-06-23 00:54
璇璇贺贺
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-7-6
收藏
得分:0 

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


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






2007-07-09 18:35
野比
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:1627
专家分:516
注 册:2007-5-24
收藏
得分:0 
应该会很受欢迎...

女侠,约吗?
2007-07-09 21:18
luckyday
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-6-20
收藏
得分:0 
赞一个

2007-07-10 10:06
lj2260
Rank: 2
等 级:论坛游民
帖 子:32
专家分:62
注 册:2010-9-12
收藏
得分:0 
不错,可以学习学习。
2011-11-23 15:15
快速回复:[原创][讨论]关于文件管理系统的数据结构模拟
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016221 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved