实现Windows资源管理器
##问题描述
Windows资源管理器是用来管理计算机资源的窗口,电脑里所有的文件都可以在资源管理器里找到,可以在资源管理器里查看文件夹的分层结构,可以利用资源管理器快速进行文件和文件夹的操作。例如,磁盘(根)、目录、不同类型的文件。 其中,文件信息包括文件名、类型、创建时间、文件大小等;磁盘信息包括磁盘名称、总大小、可用空间等;目录信息包括目录名称、修改日期、大小、对象数等。

基本要求

(1)构造一个空的资源管理器;

(2)新建/删除磁盘;

(3)在当前选择目录下新建/删除目录;

(4)在当前选择目录下新建/删除文件;

(5)以目录树的形式输出当前目录下的文件以及文件夹信息,并统计目录数和文件数;

(6)回上一级:当前目录为当前目录的上一级目录,并以目录树的形式输出当前目录下的文件以及文件夹信息,并统计目录数和文件数;

(7)模糊查找目录/文件信息,并显示查找结果;

(8)撤销一个资源管理器。

思路

链表烦死了,但是还只能用链表写(貌似其实正解是二叉树),书上东西还没有看完就开工了,最后实现了一个看起来有点像二叉树的树形结构,样式的话其实是模仿了命令行的设计,用一整行命令去实现操作,看起来比较美观,但是有些隐藏Bug可能还没发现,但是目前为止是能用的,写了400多行,腰酸背痛,要gg了,呜呜~

文件文件夹结构体实现

struct file {
int date_hour;
int date_min;
int date_sec;
string m_name;
string m_type="默认";
int m_zone;
file *nex;
file(string name, string type, int zone,file *Nex=NULL):m_name(name),m_type(type),m_zone(zone),nex(Nex){
time_t time_seconds = time(0);
localtime_s(&now_time, &time_seconds);
this->date_hour =now_time.tm_hour;
this->date_min = now_time.tm_min;
this->date_sec = now_time.tm_sec;
}
file *insertAfter(string name, string type, int zone) {
nex = new file(name, type, zone, nex);
return nex;
}
};
struct floder {
file *F_first;
int date_hour;
int date_min;
int date_sec;
int max_Size;
floder *nex;//同级
floder *link;//子文件夹
string m_name;
floder(){
F_first = new file(" ", " ", 100);
time_t time_seconds = time(0);
localtime_s(&now_time, &time_seconds);
this->date_hour = now_time.tm_hour;
this->date_min = now_time.tm_min;
this->date_sec = now_time.tm_sec;
nex = link = NULL;
}
floder(string name,int size,floder *Nex=NULL):m_name(name),max_Size(size),nex(Nex) {
F_first = new file(" "," ",100);
time_t time_seconds = time(0);
localtime_s(&now_time, &time_seconds);
this->date_hour = now_time.tm_hour;
this->date_min = now_time.tm_min;
this->date_sec = now_time.tm_sec;
//nex = NULL;
link = NULL;
}
floder *insrtAfter(string name,int size){
nex = new floder(name,size,nex);
return nex;
}
void insertLink(string name,int size) {
link = new floder(name, size, link);
}
};

命令行操作类实现

void print_Time(floder *p);
void print_Time(file *p);
class Disk {
floder user;
string name;
string path;
floder *cur_Disk;
public:
Disk(){
name = "@Titordong";
path=name+ "\\";
//user.nex = new floder();
}
floder*GetHead() { return &user;}
string GetPath() {
return path;
}
void PrintPath() {
cout << path << ">";
}
floder* Open_Floder(floder *cur, string aim) {
bool flag = false;
floder *temp = cur;
cur = cur->link->nex;
while (cur!= NULL) {
if (cur->m_name == aim) {
if (temp == &user) {
cur_Disk = cur;
}
flag = true;
path += cur->m_name;
path += "\\";
return cur;
}
else cur = cur->nex;
}
if (flag == false) {
cout << "无效路径!" << endl;
return temp;
}
return cur;
}
floder *quit(floder *cur) {
if (cur == &user) {
cout << "已到达根内存" << endl;
return cur;
}
floder *ans = NULL;
query_Dir(&user,cur,ans);
if (ans == NULL) {
cout << "失败!!!" << endl;
return cur;
}
path = path.substr(0, path.length() - 1);
while (path[path.size() - 1] != '\\') {
path = path.substr(0, path.length() - 1);
}
return ans;
}
bool query_Dir(floder *cur,floder *aim,floder*&ans) {
floder *p = cur->link,*temp=cur;
if (p != NULL)
p = p->nex;
while (p!=NULL) {
if (p == aim) {
ans = temp;
return true;
}
else {
if (query_Dir(p, aim, ans))return true;
p = p->nex;
}
}
return false;
}
int query_Zone(floder *p) {
long long ans = 0;
floder* t = p->link;
if(t!=NULL)
t=t->nex;
while (t != NULL) {
ans += query_Zone(t);
t = t->nex;
}
file *q = p->F_first->nex;
while (q != NULL) {
ans += q->m_zone;
q = q->nex;
}
return ans;
}
void find(string Path, floder *src, string aim) {
floder *p = src->link;
if (p != NULL) {
p = p->nex;
}
while (p != NULL) {
if (cmp(p->m_name, aim)) {
cout <<setw(10)<< Path << "> "<<setw(20)<< p->m_name <<setw(20)<<"<DIR>"<< endl;
}
if(Path[Path.size()-1]!='\\')
find(Path + "\\" + p->m_name, p, aim);
else find(Path + p->m_name, p, aim);
p = p->nex;
}
file *q = src->F_first->nex;
while (q != NULL) {
if (cmp(q->m_name, aim)) {
cout <<setw(10)<< Path << "> " <<setw(20)<< q->m_name << endl;
}
q = q->nex;
}
}
bool cmp(string a, string b) {
if (a.size() < b.size())return false;
for (int i(0); i < a.size(); i++) {
bool flag = true;
for (int j(0); j < b.size(); j++) {
if (a[i] != b[j])flag = false;
}
if (flag == true)return true;
}
return false;
}
void Display_Dir(floder*cur) {
if (cur == &user) {
cout << "请先进入一个磁盘" << endl;
return;
}
int num_F = 0, num_D = 0;
long long all_zone = query_Zone(cur);
floder *p = cur->link; if(p!=NULL)
p = p->nex;
while (p != NULL) {
print_Time(p);
cout<<setw(20) << "<DIR>" << setw(29) << p->m_name << endl;
num_D++;
p = p->nex;
}
file *q = cur->F_first->nex;
while (q != NULL) {
print_Time(q);
cout<< setw(20) << q->m_type << setw(10) << q->m_zone << "字节" << setw(15) << q->m_name << endl;
num_F++;
q = q->nex;
}
cout << setw(40) << num_F << "个文件,已用" << all_zone << "个字节" << endl;
cout << setw(40) << num_D << "个目录,剩余可用空间" <<cur_Disk->max_Size-all_zone<<"字节"<< endl;
}
void Add_Disk(string name,int size=0) {
floder *p = GetHead()->link;
if (p == NULL) {
p = new floder("hh",0);
GetHead()->link = p;
}
while (p->nex!= NULL) {
p = p->nex;
}
p=p->insrtAfter(name,size);
}
void Add_floder(string name, floder *src) {
if (src == &user) {
cout << "请先创建磁盘!" << endl;
return;
}
floder *p = src->link,*temp=src;
if (p == NULL) {
p=new floder("hh", 0);
src->link = p;
}
while (p->nex != NULL) {
p = p->nex;
}
p=p->insrtAfter(name,0);
temp->date_hour = p->date_hour;
temp->date_min = p->date_min;
temp->date_sec = p->date_sec;
}
void Add_file(string name, string type, int size, floder *src) {
if (src == &user) {
cout << "请先创建磁盘!" << endl;
return;
}
if (size > cur_Disk->max_Size - query_Zone(src)) {
cout << "内存不足!!创建失败!" << endl;
return;
}
file *p = src->F_first;
floder *temp = src;
while (p->nex != NULL) {
p = p->nex;
}
p=p->insertAfter(name, type, size);
temp->date_hour = p->date_hour;
temp->date_min = p->date_min;
temp->date_sec = p->date_sec;
}
void Delet_Dir(floder *src) {
file*q = src->F_first->nex,*t;
while (q != NULL) {
t = q;
q = q->nex;
delete t;
}
floder *p = src->link,*tt;
if (p != NULL)
p = p->nex;
while (p != NULL) {
tt = p;
Delet_Dir(p);
p = p->nex;
delete tt;
}
}
void Delet_D(floder*src, string aim) {
floder *p = src->link;
bool flag = false;
while (p!=NULL&&p->nex!= NULL) {
if (p->nex->m_name == aim) {
flag = true;
break;
}
p = p->nex;
}
if (flag) {
Delet_Dir(p->nex);
if (p->nex->nex == NULL) {
delete p->nex;
p->nex = NULL;
}
else {
floder*t = p->nex;
p->nex = p->nex->nex;
delete t;
}
}
else {
cout << "没有这个文件夹" << endl;
}
}
void Delet_File(file *src) {
file *p = src->nex,*t=src;
if (p->nex == NULL) {
delete p;
p = NULL;
}
else {
t->nex = p->nex;
delete p;
p = NULL;
}
}
void Delet_F(floder*src, string aim) {
file *p = src->F_first;
bool flag = false;
while (p->nex!=NULL) {
if (p->nex->m_name == aim) {
flag = true;
break;
}
p = p->nex;
}
if (flag) {
Delet_File(p);
}
else {
cout << "没有这个文件" << endl;
}
} };
void print_Time(floder *p) {
cout << p->date_hour<<":";
if (p->date_min < 10)
cout << 0;
cout << p->date_min << ":";
if (p->date_sec < 10)
cout << 0;
cout << p->date_sec;
}
void print_Time(file *p) {
cout << p->date_hour << ":";
if (p->date_min < 10)
cout << 0;
cout << p->date_min << ":";
if (p->date_sec < 10)
cout << 0;
cout << p->date_sec;
}

主函数

#include"head.h"
#include<iostream>
#include<cstring>
#include<strstream>
using namespace std;
int main() {
Disk S;
char cmd[100];
char op[10],dir[10],type[10],name[10];
int size;
floder*cur = S.GetHead();
S.PrintPath();
while (1) {
cin.getline(cmd,100);
istrstream strin(cmd, sizeof(cmd));
strin >> op;
if (strcmp(op, "mkdir")==0) {
strin >> dir >> size;
if(cur==S.GetHead())
S.Add_Disk(dir,size);
else {
S.Add_floder(dir, cur);
}
S.PrintPath();
}
else if (strcmp(op, "dir") == 0) {
S.Display_Dir(cur);
S.PrintPath();
}
else if (strcmp(op, "cd") == 0) {
strin >> dir;
if (strcmp(dir, "..")) {
cur = S.Open_Floder(cur, dir);
S.PrintPath();
}
else {
cur=S.quit(cur);
S.PrintPath();
}
}
else if (strcmp(op, "type") == 0) {
strin >> name >> type >> size;
S.Add_file(name, type, size, cur);
S.PrintPath();
}
else if (op[0] == '\0') {
S.PrintPath();
}
else if (strcmp(op, "del") == 0) {
strin >> name;
S.Delet_D(cur, name);
S.PrintPath();
}
else if (strcmp(op, "rm") == 0) {
strin >> name;
S.Delet_F(cur, name);
S.PrintPath();
}
else if (strcmp(op, "whereis") == 0) {
strin >> name;
S.find(S.GetPath(), cur, name);
S.PrintPath();
}
else if (strcmp(op, "exit") == 0) {
return 0;
}
else {
cout << "无效的参数" << endl;
S.PrintPath();
}
}
return 0;
}

后记

其实指针链表蛮好玩的,噗~

2018/11/29 23:21:51

windows资源管理器(只能看,不能用)的更多相关文章

  1. SVN has atopping svn已停止工作 or windows资源管理器无限重启

    准备在空间时间用用linux,就在自己的win7系统上安装了属性系统,用easyBCD安装的,谁知安装好之后win7系统下的svn客户端不能使用了,点击报错“SVN已停止工作”,随后怀疑是linux引 ...

  2. 使用windows资源管理器的排序规则

    对于windows资源管理器 abc_1_def是要排到abc_10_def前面的 而一般的排序规则, 都会吧_10_排到前面 所以为了使用习惯, 最好用资源管理器的排序规则, windows有个AP ...

  3. 为Windows资源管理器右键菜单添加菜单项

    为Windows资源管理器右键菜单添加菜单项 在Windows下命令行用的比较多,经常在资源管理器里翻到某个目录,若想要在此目录下跑命令,只能是打开cmd.exe,然后一路cd才能到达此目录. 每次都 ...

  4. 出现“Windows资源管理器已停止工作”错误

    出现"Windows资源管理器已停止工作"错误 什么是资源管理器呢,explorer.exe进程的作用就是让我们管理计算机中的资源! 今天开电脑的时候就一直提示windows资源管 ...

  5. 怎样在Windows资源管理器中添加右键菜单以及修改右键菜单顺序

    有时,我们需要在Windows资源管理器的右键菜单中添加一些项,以方便使用某些功能或程序. 比如我的电脑上有一个免安装版的Notepad++,我想在所有文件的右键菜单中添加一项用Notepad++打开 ...

  6. SharePoint 2010 "客户端不支持使用windows资源管理器打开此列表" 解决方法

    SharePoint 2010 在“库”--“库工具”,有一个“使用资源管理器打开”的按钮,点上去报“客户端不支持使用windows资源管理器打开此列表”.如图: 解决方案:在“开始”--“管理工具” ...

  7. sqlserver和Windows资源管理器争用内存

    sqlserver和Windows资源管理器在设置成相同的优先级的情况下(普通),Windows资源管理器优先于sqlserver对内存的征用.开始是

  8. Windows资源管理器文件名排序

    Windows资源管理器文件名排序 Windows资源管理器文件名排序 背景:自然排序 什么是自然排序? 怎样按自然排序的规则进行排序? 基于Python的解决方案 参考材料 这学期担任了本科生教学助 ...

  9. WPF实现Windows资源管理器(附源码)

      今天我来写一篇关于利用WPF来实现Windows的资源管理器功能,当然只是局部实现这个功能,因为在很多时候我们需要来实现对本机资源的管理,当然我们可以使用OpenFileDialog dialog ...

随机推荐

  1. 使用Vlc.DotNet打开摄像头并截图 C#

      参考上一篇  使用vlc打开usb摄像头 理论上输入下面地址 "dshow:// :dshow-size=1600*1200:dshow-vdev=USB CAM2"C#就能打 ...

  2. DMA设计

    目录 DMA设计 DMA框架 手册请看英文手册 芯片特性 请求来源 协议简述 基本时序 模式 协议 数据大小的描述 具体完整的实例时序 代码设计 驱动程序 测试程序 测试 参考链接 title: DM ...

  3. CMDB服务器管理系统【s5day88】:采集资产-文件配置(一)

    django中间件工作原理 整体流程: 在接受一个Http请求之前的准备 启动一个支持WSGI网关协议的服务器监听端口等待外界的Http请求,比如Django自带的开发者服务器或者uWSGI服务器. ...

  4. [物理学与PDEs]第1章习题3 常场强下电势的定解问题

    在一场强为 ${\bf E}_0$ (${\bf E}_0$ 为常向量) 的电场中, 置入一个半径为 $R$ 的导电球体, 试导出球外电势所满足的方程及相应的定解条件. 解答: 设导电球体为 $B_R ...

  5. 给一个Unix域套接字bind一个路径名

    #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <strings.h& ...

  6. LinQ简单增、删、改、查

    一.简单介绍 1.LinQ to Sql类(NET Language Integrated Query (LINQ) ) LINQ定义了大约40个查询操作符,如select.from.in.where ...

  7. MySQL数据库学习2 - 数据库的操作

    一.系统数据库 二.创建数据库 三.数据库相关操作 四.了解内容 一.系统数据库 执行如下命令,查看系统库 show databases; information_schema: 虚拟库,不占用磁盘空 ...

  8. ch03 课下作业——缓冲区溢出漏洞实验

    一.实验简介: 缓冲区溢出是指程序试图向缓冲区写入超出预分配固定长度数据的情况.这一漏洞可以被恶意用户利用来改变程序的流控制,甚至执行代码的任意片段.这一漏洞的出现是由于数据缓冲器和返回地址的暂时关闭 ...

  9. [Linux容器]当我们谈容器的时候,我们在谈什么

    Docker在当下很火,那么,当我们谈Docker,谈容器的时候,我们在谈什么? 或者说,你对Docker,对容器了解吗?容器,到底是怎么一回事儿? 这篇文章着重来讲一下Linux容器,为什么强调Li ...

  10. ubuntu 16.04扩充root 分区

    ubuntu使用过程中,提示root分区剩余空间不足,剩余200多M时还可以进行一些操作,剩余几M时拷贝等命令都不能够执行. 扩充root分区步骤如下: 1.查看root分区所在位置: 命令: sud ...