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

基本要求

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

(2)新建/删除磁盘;

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

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

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

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

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

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

思路

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

文件文件夹结构体实现

  1. struct file {
  2. int date_hour;
  3. int date_min;
  4. int date_sec;
  5. string m_name;
  6. string m_type="默认";
  7. int m_zone;
  8. file *nex;
  9. file(string name, string type, int zone,file *Nex=NULL):m_name(name),m_type(type),m_zone(zone),nex(Nex){
  10. time_t time_seconds = time(0);
  11. localtime_s(&now_time, &time_seconds);
  12. this->date_hour =now_time.tm_hour;
  13. this->date_min = now_time.tm_min;
  14. this->date_sec = now_time.tm_sec;
  15. }
  16. file *insertAfter(string name, string type, int zone) {
  17. nex = new file(name, type, zone, nex);
  18. return nex;
  19. }
  20. };
  21. struct floder {
  22. file *F_first;
  23. int date_hour;
  24. int date_min;
  25. int date_sec;
  26. int max_Size;
  27. floder *nex;//同级
  28. floder *link;//子文件夹
  29. string m_name;
  30. floder(){
  31. F_first = new file(" ", " ", 100);
  32. time_t time_seconds = time(0);
  33. localtime_s(&now_time, &time_seconds);
  34. this->date_hour = now_time.tm_hour;
  35. this->date_min = now_time.tm_min;
  36. this->date_sec = now_time.tm_sec;
  37. nex = link = NULL;
  38. }
  39. floder(string name,int size,floder *Nex=NULL):m_name(name),max_Size(size),nex(Nex) {
  40. F_first = new file(" "," ",100);
  41. time_t time_seconds = time(0);
  42. localtime_s(&now_time, &time_seconds);
  43. this->date_hour = now_time.tm_hour;
  44. this->date_min = now_time.tm_min;
  45. this->date_sec = now_time.tm_sec;
  46. //nex = NULL;
  47. link = NULL;
  48. }
  49. floder *insrtAfter(string name,int size){
  50. nex = new floder(name,size,nex);
  51. return nex;
  52. }
  53. void insertLink(string name,int size) {
  54. link = new floder(name, size, link);
  55. }
  56. };

命令行操作类实现

  1. void print_Time(floder *p);
  2. void print_Time(file *p);
  3. class Disk {
  4. floder user;
  5. string name;
  6. string path;
  7. floder *cur_Disk;
  8. public:
  9. Disk(){
  10. name = "@Titordong";
  11. path=name+ "\\";
  12. //user.nex = new floder();
  13. }
  14. floder*GetHead() { return &user;}
  15. string GetPath() {
  16. return path;
  17. }
  18. void PrintPath() {
  19. cout << path << ">";
  20. }
  21. floder* Open_Floder(floder *cur, string aim) {
  22. bool flag = false;
  23. floder *temp = cur;
  24. cur = cur->link->nex;
  25. while (cur!= NULL) {
  26. if (cur->m_name == aim) {
  27. if (temp == &user) {
  28. cur_Disk = cur;
  29. }
  30. flag = true;
  31. path += cur->m_name;
  32. path += "\\";
  33. return cur;
  34. }
  35. else cur = cur->nex;
  36. }
  37. if (flag == false) {
  38. cout << "无效路径!" << endl;
  39. return temp;
  40. }
  41. return cur;
  42. }
  43. floder *quit(floder *cur) {
  44. if (cur == &user) {
  45. cout << "已到达根内存" << endl;
  46. return cur;
  47. }
  48. floder *ans = NULL;
  49. query_Dir(&user,cur,ans);
  50. if (ans == NULL) {
  51. cout << "失败!!!" << endl;
  52. return cur;
  53. }
  54. path = path.substr(0, path.length() - 1);
  55. while (path[path.size() - 1] != '\\') {
  56. path = path.substr(0, path.length() - 1);
  57. }
  58. return ans;
  59. }
  60. bool query_Dir(floder *cur,floder *aim,floder*&ans) {
  61. floder *p = cur->link,*temp=cur;
  62. if (p != NULL)
  63. p = p->nex;
  64. while (p!=NULL) {
  65. if (p == aim) {
  66. ans = temp;
  67. return true;
  68. }
  69. else {
  70. if (query_Dir(p, aim, ans))return true;
  71. p = p->nex;
  72. }
  73. }
  74. return false;
  75. }
  76. int query_Zone(floder *p) {
  77. long long ans = 0;
  78. floder* t = p->link;
  79. if(t!=NULL)
  80. t=t->nex;
  81. while (t != NULL) {
  82. ans += query_Zone(t);
  83. t = t->nex;
  84. }
  85. file *q = p->F_first->nex;
  86. while (q != NULL) {
  87. ans += q->m_zone;
  88. q = q->nex;
  89. }
  90. return ans;
  91. }
  92. void find(string Path, floder *src, string aim) {
  93. floder *p = src->link;
  94. if (p != NULL) {
  95. p = p->nex;
  96. }
  97. while (p != NULL) {
  98. if (cmp(p->m_name, aim)) {
  99. cout <<setw(10)<< Path << "> "<<setw(20)<< p->m_name <<setw(20)<<"<DIR>"<< endl;
  100. }
  101. if(Path[Path.size()-1]!='\\')
  102. find(Path + "\\" + p->m_name, p, aim);
  103. else find(Path + p->m_name, p, aim);
  104. p = p->nex;
  105. }
  106. file *q = src->F_first->nex;
  107. while (q != NULL) {
  108. if (cmp(q->m_name, aim)) {
  109. cout <<setw(10)<< Path << "> " <<setw(20)<< q->m_name << endl;
  110. }
  111. q = q->nex;
  112. }
  113. }
  114. bool cmp(string a, string b) {
  115. if (a.size() < b.size())return false;
  116. for (int i(0); i < a.size(); i++) {
  117. bool flag = true;
  118. for (int j(0); j < b.size(); j++) {
  119. if (a[i] != b[j])flag = false;
  120. }
  121. if (flag == true)return true;
  122. }
  123. return false;
  124. }
  125. void Display_Dir(floder*cur) {
  126. if (cur == &user) {
  127. cout << "请先进入一个磁盘" << endl;
  128. return;
  129. }
  130. int num_F = 0, num_D = 0;
  131. long long all_zone = query_Zone(cur);
  132. floder *p = cur->link;
  133. if(p!=NULL)
  134. p = p->nex;
  135. while (p != NULL) {
  136. print_Time(p);
  137. cout<<setw(20) << "<DIR>" << setw(29) << p->m_name << endl;
  138. num_D++;
  139. p = p->nex;
  140. }
  141. file *q = cur->F_first->nex;
  142. while (q != NULL) {
  143. print_Time(q);
  144. cout<< setw(20) << q->m_type << setw(10) << q->m_zone << "字节" << setw(15) << q->m_name << endl;
  145. num_F++;
  146. q = q->nex;
  147. }
  148. cout << setw(40) << num_F << "个文件,已用" << all_zone << "个字节" << endl;
  149. cout << setw(40) << num_D << "个目录,剩余可用空间" <<cur_Disk->max_Size-all_zone<<"字节"<< endl;
  150. }
  151. void Add_Disk(string name,int size=0) {
  152. floder *p = GetHead()->link;
  153. if (p == NULL) {
  154. p = new floder("hh",0);
  155. GetHead()->link = p;
  156. }
  157. while (p->nex!= NULL) {
  158. p = p->nex;
  159. }
  160. p=p->insrtAfter(name,size);
  161. }
  162. void Add_floder(string name, floder *src) {
  163. if (src == &user) {
  164. cout << "请先创建磁盘!" << endl;
  165. return;
  166. }
  167. floder *p = src->link,*temp=src;
  168. if (p == NULL) {
  169. p=new floder("hh", 0);
  170. src->link = p;
  171. }
  172. while (p->nex != NULL) {
  173. p = p->nex;
  174. }
  175. p=p->insrtAfter(name,0);
  176. temp->date_hour = p->date_hour;
  177. temp->date_min = p->date_min;
  178. temp->date_sec = p->date_sec;
  179. }
  180. void Add_file(string name, string type, int size, floder *src) {
  181. if (src == &user) {
  182. cout << "请先创建磁盘!" << endl;
  183. return;
  184. }
  185. if (size > cur_Disk->max_Size - query_Zone(src)) {
  186. cout << "内存不足!!创建失败!" << endl;
  187. return;
  188. }
  189. file *p = src->F_first;
  190. floder *temp = src;
  191. while (p->nex != NULL) {
  192. p = p->nex;
  193. }
  194. p=p->insertAfter(name, type, size);
  195. temp->date_hour = p->date_hour;
  196. temp->date_min = p->date_min;
  197. temp->date_sec = p->date_sec;
  198. }
  199. void Delet_Dir(floder *src) {
  200. file*q = src->F_first->nex,*t;
  201. while (q != NULL) {
  202. t = q;
  203. q = q->nex;
  204. delete t;
  205. }
  206. floder *p = src->link,*tt;
  207. if (p != NULL)
  208. p = p->nex;
  209. while (p != NULL) {
  210. tt = p;
  211. Delet_Dir(p);
  212. p = p->nex;
  213. delete tt;
  214. }
  215. }
  216. void Delet_D(floder*src, string aim) {
  217. floder *p = src->link;
  218. bool flag = false;
  219. while (p!=NULL&&p->nex!= NULL) {
  220. if (p->nex->m_name == aim) {
  221. flag = true;
  222. break;
  223. }
  224. p = p->nex;
  225. }
  226. if (flag) {
  227. Delet_Dir(p->nex);
  228. if (p->nex->nex == NULL) {
  229. delete p->nex;
  230. p->nex = NULL;
  231. }
  232. else {
  233. floder*t = p->nex;
  234. p->nex = p->nex->nex;
  235. delete t;
  236. }
  237. }
  238. else {
  239. cout << "没有这个文件夹" << endl;
  240. }
  241. }
  242. void Delet_File(file *src) {
  243. file *p = src->nex,*t=src;
  244. if (p->nex == NULL) {
  245. delete p;
  246. p = NULL;
  247. }
  248. else {
  249. t->nex = p->nex;
  250. delete p;
  251. p = NULL;
  252. }
  253. }
  254. void Delet_F(floder*src, string aim) {
  255. file *p = src->F_first;
  256. bool flag = false;
  257. while (p->nex!=NULL) {
  258. if (p->nex->m_name == aim) {
  259. flag = true;
  260. break;
  261. }
  262. p = p->nex;
  263. }
  264. if (flag) {
  265. Delet_File(p);
  266. }
  267. else {
  268. cout << "没有这个文件" << endl;
  269. }
  270. }
  271. };
  272. void print_Time(floder *p) {
  273. cout << p->date_hour<<":";
  274. if (p->date_min < 10)
  275. cout << 0;
  276. cout << p->date_min << ":";
  277. if (p->date_sec < 10)
  278. cout << 0;
  279. cout << p->date_sec;
  280. }
  281. void print_Time(file *p) {
  282. cout << p->date_hour << ":";
  283. if (p->date_min < 10)
  284. cout << 0;
  285. cout << p->date_min << ":";
  286. if (p->date_sec < 10)
  287. cout << 0;
  288. cout << p->date_sec;
  289. }

主函数

  1. #include"head.h"
  2. #include<iostream>
  3. #include<cstring>
  4. #include<strstream>
  5. using namespace std;
  6. int main() {
  7. Disk S;
  8. char cmd[100];
  9. char op[10],dir[10],type[10],name[10];
  10. int size;
  11. floder*cur = S.GetHead();
  12. S.PrintPath();
  13. while (1) {
  14. cin.getline(cmd,100);
  15. istrstream strin(cmd, sizeof(cmd));
  16. strin >> op;
  17. if (strcmp(op, "mkdir")==0) {
  18. strin >> dir >> size;
  19. if(cur==S.GetHead())
  20. S.Add_Disk(dir,size);
  21. else {
  22. S.Add_floder(dir, cur);
  23. }
  24. S.PrintPath();
  25. }
  26. else if (strcmp(op, "dir") == 0) {
  27. S.Display_Dir(cur);
  28. S.PrintPath();
  29. }
  30. else if (strcmp(op, "cd") == 0) {
  31. strin >> dir;
  32. if (strcmp(dir, "..")) {
  33. cur = S.Open_Floder(cur, dir);
  34. S.PrintPath();
  35. }
  36. else {
  37. cur=S.quit(cur);
  38. S.PrintPath();
  39. }
  40. }
  41. else if (strcmp(op, "type") == 0) {
  42. strin >> name >> type >> size;
  43. S.Add_file(name, type, size, cur);
  44. S.PrintPath();
  45. }
  46. else if (op[0] == '\0') {
  47. S.PrintPath();
  48. }
  49. else if (strcmp(op, "del") == 0) {
  50. strin >> name;
  51. S.Delet_D(cur, name);
  52. S.PrintPath();
  53. }
  54. else if (strcmp(op, "rm") == 0) {
  55. strin >> name;
  56. S.Delet_F(cur, name);
  57. S.PrintPath();
  58. }
  59. else if (strcmp(op, "whereis") == 0) {
  60. strin >> name;
  61. S.find(S.GetPath(), cur, name);
  62. S.PrintPath();
  63. }
  64. else if (strcmp(op, "exit") == 0) {
  65. return 0;
  66. }
  67. else {
  68. cout << "无效的参数" << endl;
  69. S.PrintPath();
  70. }
  71. }
  72. return 0;
  73. }

后记

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

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. mysql中replace替换字符串更改方法

    MySQL中update替换部分字符串replace的简单用法 近日,遇到了需要将部分字符串替换为另外的字符,平时用的最多的是直接update整个字段值,在这种情况下效率比较低,而且容易出错.其实my ...

  2. jQuery使用(十四):extend()方法

    浅层克隆 深层克隆 扩展方法 一.extend的基本使用 语法: $.extend( target [, object1 ] [, objectN ] ) $.extend( [deep ], tar ...

  3. Web常见漏洞修复建议

    一.SQL注入修复建议 1.过滤危险字符,例如:采用正则表达式匹配union.sleep.and.select.load_file等关键字,如果匹配到则终止运行. 2.使用预编译语句,使用PDO需要注 ...

  4. SQLserver 数据库高版本无法还原到低版本的数据解决方法

    sql server 数据库的版本只支持从上往下兼容.即高版本可以兼容低版本 .低版本不能兼容低版本.通常我们在开发时会用比较高的版本.但是部署到客户那边可能他们的数据库版本会比较低. 我们可以通过导 ...

  5. java.util.zip.ZipException: invalid entry size

    启动maven项目时报java.util.zip.ZipException: invalid entry size (expected 7612 but got 5955 bytes) 可能是mave ...

  6. Vue生命周期中mounted和created的区别

    参考链接:https://blog.csdn.net/xdnloveme/article/details/78035065

  7. shiro-core包引用的版本问题

    在做shiro学习时,遇到这样的问题: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/ ...

  8. Java消息队列--ActiveMq 初体验

    1.下载安装ActiveMQ ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Un ...

  9. 【原创】大叔经验分享(8)创建hive表时用内部表还是外部表

    内部表和外部表最主要的一个差别就是删除表或者删除分区时,底层的文件是否自动删除,内部表会自动删除,外部表不会自动删除,所以基础数据表一定要用外部表,即使误删表或分区之后,还可以很容易的恢复回来. 虽然 ...

  10. RFB Net笔记

    ECCV2018 论文:Receptive Field Block Net for Accurate and Fast Object Detection 论文链接:https://arxiv.org/ ...