• 设计一个树型目录结构的文件系统,其根目录为 root,各分支可以是目录,也可以是文件,最后的叶子都是文件。


    设计一个树型目录结构的文件系统,其根目录为 root,各分支可以是目录,也可以是文件,最后的叶子都是文件。

    我实现的功能是提供父目录(兄弟目录),输入文件名,创建树型目录结构,文本文件不可以再有子目录。最终打印出每个创建的文件夹或文本文件的路径。

    代码:

    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    
    
    using namespace std;
    
    typedef struct node                //节点
    {
        char *data;                     //文件名
        int dir_file;                   //文件夹或者文本文件
        struct node *first_child,*next_sibling;
    } tree;                             //多叉树
    
    typedef struct                       //存放树节点的队列
    {
        tree * data;
        int pre;
    } Box;
    
    typedef struct
    {
        Box data[100];
        int front,rear;
    } Queue;
    
    Queue que;
    
    void height(tree *t)                     // 层序遍历
    {
        tree *p = t,*q;
        que.data[0].data = p;
        que.data[0].pre = -1;
        que.front = -1;
        que.rear = 0;
        if(p->first_child == NULL)
            return ;
        while(que.front != que.rear)
        {
            que.front++;
            q = que.data[que.front].data->first_child;
            while(q != NULL)
            {
                que.rear++;
                que.data[que.rear].data = q;
                que.data[que.rear].pre = que.front;
                q = q->next_sibling;
            }
        }
    }
    
    tree *insertTree(char *ch, tree *parent, tree *pre_sibling,int judge)     //插入节点
    {
        tree *child = (tree *)malloc(sizeof(tree));
        child->data = ch;
        child->dir_file = judge;
    
        if (parent != NULL)
        {
            parent->first_child = child;
        }
        if (pre_sibling != NULL)
        {
            pre_sibling->next_sibling = child;
        }
    
        child->first_child = NULL;
        child->next_sibling = NULL;
    
        return child;
    }
    
    tree *create()                 //创建头节点
    {
        tree *root = (tree *)malloc(sizeof(tree));
        root->data = "root";
        root->dir_file = 1;
        root->first_child = NULL;
        root->next_sibling = NULL;
        return root;
    }
    
    void insert(Queue q,int index,int tj)           //插入函数
    {
        char *name;
        name = new char [10];
        bool judge;
        if(q.data[index].data->dir_file == 0)   //文本文件
        {
            cout<<"the current file is "<<q.data[index].data->data<<endl;
            cout<<"input the name :";
            cin>>name;
            cout<<"input "0":";
            cin>>judge;
            tree *node = insertTree(name,NULL,q.data[index].data,judge);
        }
        else if(q.data[index].data->dir_file == 1 && tj == 1)    //要在同一目录下创建另一文件
        {
            cout<<"the current file is "<<q.data[index].data->data<<endl;
            cout<<"input the name :";
            cin>>name;
            cout<<"if it is folder, input "1",else input "0":";
            cin>>judge;
            tree *node = insertTree(name,NULL,q.data[index].data,judge);
        }
        else                                                   //在某一目录下创建文件
        {
            cout<<"the current folder is "<<q.data[index].data->data<<endl;
            cout<<"input the name :";
            cin>>name;
            cout<<"if it is folder, input "1",else input "0":";
            cin>>judge;
            tree *node = insertTree(name,q.data[index].data,NULL,judge);
        }
    
    }
    
    int main()
    {
        int mark = 0;
        int num = 1;
        int tj = 0;
        int temp[100];
        tree *root = create();
        height(root);
        while(mark != -1)
        {
            cout<<"now is No."<<num<<",input the number of file(must be existed):";
            cin>>num;
            cout<<"the same level(the first can't choose Yes) ? 1.Yes 2.No"<<endl;
            cin>>tj;
            if(tj == 1)
                tj = 1;
            else
                tj = 0;
            insert(que,num-1,tj);
            height(root);
            cout<<"Quit input -1 ,continue input 1 :";
            cin>>mark;
        }
        for(int i = 1; i<=que.rear; i++)      //打印路径
        {
            int k = que.data[i].pre;
            int j = 0;
            while(k != -1)
            {
                temp[j] = k;
                j++;
                k = que.data[k].pre;
            }
            while(j > 0)
            {
                j--;
                cout<<"/"<<que.data[temp[j]].data->data;
            }
            cout<<"/"<<que.data[i].data->data<<endl;
        }
    }

    运行实例:

     
  • 相关阅读:
    推荐一个不错的在线Linux学习平台(免安装系统)
    C#基本语法知识
    GDI+ 使用LockBits和指针加快处理速度
    C#对图像像素处理的三种方式
    [转]video4linux(v4l)使用摄像头的实例基础教程与体会
    Eclipse Qt开发环境的建立
    c#图像处理基础
    [转]超酷的图像效果
    Qt开发环境的建立
    C++模版全掌握(实例)
  • 原文地址:https://www.cnblogs.com/NikkiNikita/p/9450747.html
Copyright © 2020-2023  润新知