模拟二级文件系统(C++面向对象实现)

 

#include <iostream>
#include <vector>
#include <sstream>

#define pb push_back

using namespace std;

#include "HELP.h"
#include "_FILE.h"
#include "UFD.h"
#include "USER.h"
#include "MFD.h"

#include "LOGIN.h"
#include "CD.h"
#include "DIR.h"
#include "CREATE.h"
#include "DELETE.h"
#include "OPEN.h"
#include "CLOSE.h"
#include "READ.h"
#include "WRITE.h"
#include "CD.h"
#include "MKDIR.h"
#include "RMDIR.h"



int main()
{
    //freopen("input.txt","r",stdin);

    int user=-1;//当前用户指针
    int cata=-1;//当前目录指针

    MFD M;//创建一个主文件目录类

    //界面命令输入前缀格式
    cout<<"~";
    if(user!=-1) cout<<"\\"<<M.Get_user_name(user);
    if(cata!=-1) cout<<"\\"<<M.Get_cata_name(user,cata);
    cout<<":>";

    M.Add_user("root","123");//添加一个超级用户

    help();//显示命令格式

    while(1){

        cout<<"~";
        if(user!=-1) cout<<"\\"<<M.Get_user_name(user);
        if(cata!=-1) cout<<"\\"<<M.Get_cata_name(user,cata);
        cout<<":>";

        //------处理命令,判断合法性和对应的操作
        string s;
        getline(cin,s);
        stringstream ss(s);

        string op[20];
        op[0]=op[1]=op[2]="";

        int cnt=0;
        while(ss>>op[cnt]){
            cnt++;
        }

        if(cnt>3) {
            cout<<"Command format error!\n";
            continue;
        }
        //------

        //选择对应的操作,cnt==3则包含路径,cnt==2则不包含路劲
        if(op[0] == "login")
        {
            if(cnt!=3) cout<<"Command format error!\n";
            else
                user = LOGIN(M,op[1],op[2]);
        }
        if(op[0]=="help"){
            help();
        }
        if(user!=-1) {
            if(op[0] == "add"){
                if(cnt==3&&user==0)
                    M.Add_user(op[1],op[2]);
                else cout<<"Command format error!\n";
            }
            else if(op[0] == "dir"){
                    if(cnt!=1) cout<<"Command format error!\n";
                    else
                        DIR(M,user,cata);
            }
            else if(op[0] == "create"){
                if(cnt==2)
                    CREATE(M,user,cata,"",op[1]);
                else if(cnt==3) CREATE(M,user,cata,op[1],op[2]);
                else {
                    cout<<"Command format error!\n";
                }
            }
            else if(op[0] == "delete"){
                if(cnt==2)
                    DELETE(M,user,cata,"",op[1]);
                else if(cnt==3) DELETE(M,user,cata,op[1],op[2]);
                else {
                    cout<<"Command format error!\n";
                }
            }
            else if(op[0] == "open"){
                if(cnt==2)
                    OPEN(M,user,cata,"",op[1]);
                else if(cnt==3) OPEN(M,user,cata,op[1],op[2]);
                else {
                    cout<<"Command format error!\n";
                }
            }
            else if(op[0] == "close"){
                if(cnt==2)
                    CLOSE(M,user,cata,"",op[1]);
                else if(cnt==3) CLOSE(M,user,cata,op[1],op[2]);
                else {
                    cout<<"Command format error!\n";
                }
            }
            else if(op[0] == "read"){
                if(cnt==2)
                    READ(M,user,cata,"",op[1]);
                else if(cnt==3) READ(M,user,cata,op[1],op[2]);
                else {
                    cout<<"Command format error!\n";
                }
            }
            else if(op[0] == "write"){
                if(cnt==2)
                    WRITE(M,user,cata,"",op[1]);
                else if(cnt==3) WRITE(M,user,cata,op[1],op[2]);
                else {
                    cout<<"Command format error!\n";
                }
            }
            else if(op[0] == "cd"){
                if(cnt>2) {
                    cout<<"Command format error!\n";
                    continue;
                }
                CD(M,user,cata,op[1]);
            }
            else if
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值