1. 程式人生 > >windows資源管理器(只能看,不能用)

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

getpath 對象 可用 pen std char cin 不同 進行

實現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資源管理器(只能看,不能用)