码迷,mamicode.com
首页 > Windows程序 > 详细

windows资源管理器(只能看,不能用)

时间:2018-11-30 00:40:55      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:cout   链表   删除目录   second   als   cal   use   不同   查找   

实现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资源管理器(只能看,不能用)

标签:cout   链表   删除目录   second   als   cal   use   不同   查找   

原文地址:https://www.cnblogs.com/Titordong/p/10041708.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!