代码

 运行程序

账号:root

密码:123456

输入命令:help help help

#include <bits/stdc++.h>
#define getb(type) (type*)malloc(sizeof(type))
using namespace std;
const int manx=10010;
const int addrsize=110;
string fileaddr[addrsize];//索引表,存放文件目录和二级文件的地址
string fileinfo[manx];//文件名
char usernowname[10];//全局变量,当前用户名
char usernowpath[200] = "\\\0";
int usernowpride;//全局变量,当前用户类型
int nowlevel;
struct user //用户类型定义
{
    char name[10];//用户名
    char pass[10];//用户密码
    int pride;//用户权限,1为管理员,0为普通用户
};
void chomdUser();
void console();
void chomd(string str1,string str2,string str3);
void linux_mkdir(string str1,string str2,string str3);
int mkdir_first(string str2);//创建根目录函数
int mkdir_second(string str2,string str3);//创建二级目录函数
void linux_ls(string str1,string str2,string str3);
void linux_remove(string str1,string str2,string str3);
void linux_cd2(string str1,string str2,string str3);
bool createuser();
bool login();
void logout();
void linux_cd(string str1,string str2,string str3);
void linux_cd(string str1,string str2,string str3){
    system("cls");
    string cmd[3];
    while(1){
       cout<<"["<<usernowname<<"#"<<str2<<"]>>>";
       cin>>cmd[0]>>cmd[1]>>cmd[2];
       chomd(cmd[0],cmd[1],cmd[2]);
    }
}
int mkdir_second(string str2,string str3){
    int flag=0;
    int index;//获取一级目录的下标 
    for(int i=0;i<addrsize;i++){//首先验证一级目录是否存在 
         if(fileaddr[i]==str3){
             flag=1;
             index=i; 
             break;
         }
   }
   if(flag==1){
       //一级目录存在可以创建二级目录否则不可以创建
          int a,b;
          a=index*100;
          b=a+100-1;
          for(int i=a;i<=b;i++){
             if(fileinfo[i]==""){
             fileinfo[i]=str2;
             break;//创建完成跳出循环 
          }         
       }
      return 1;
   }else{
         return -1; 
   } 
} 
int mkdir_first(string str2){
    int flag=0;
    for(int i=0;i<addrsize;i++){
         if(fileaddr[i]==str2){
             flag=1;
             break;
         }
   }
   if(flag==1){
           return -1;
    }else{
         for(int i=0;i<addrsize;i++){
             if(fileaddr[i]==""){
               fileaddr[i]=str2;
               break;
            }
         }
         return 1;
     }
} 
void linux_mkdir(string str1,string str2,string str3){
    if(str3=="/"){//创建根目录中的文件列表 
        int index=mkdir_first(str2);
        if(index==-1){
           cout<<"文件夹已存在不可重复创建"<<endl;
        }else if(index==1){
            cout<<str2<<"创建成功"<<endl; 
        }
    }else{//创建二级目录 
        int index=mkdir_second(str2,str3);
        if(index==-1){
            cout<<"不可以创建"<<endl; 
        }else if(index==1){
            cout<<"创建完成"<<endl; 
        }
    }  
}
void linux_remove(string str1,string str2,string str3){
   if(str3=="/"){//删除根目录一级目录
       int i;
       for(i=0;i<addrsize;i++){
            if(fileaddr[i]==str2){
                 fileaddr[i]="";break;    
         }
       }
       if(i>=addrsize) cout<<"文件夹不存在"<<endl;
       else cout<<str2<<"删除成功"<<endl; 
   }else{//删除二级目录 
      int i=0,index=-1;
      for(int i=0;i<addrsize;i++){
          if(fileaddr[i]==str2){
             index=i;break;     
        }
      }
      if(index==-1) cout<<"根目录路径错误"<<endl;
      else{
          int a,b,i;
          a=index*100;
          b=a+100-1;
          for(i=a;i<=b;i++){
             if(fileinfo[i]==str3){
                 fileinfo[i]="";break;      
           }    
        }
        if(i>b) cout<<"文件夹不存在"<<endl;
        else cout<<str3<<"删除成功"<<endl; 
      }
   } 
}
int main(){
    console();
    return 0;
}
void console(){
    while(1){
      system("cls");
      while(1) if(login())//判断用户是否登录
      break;
      //默认的管理账号密码是root   123456.用户账号可以进行相应的操作
      if(strcmp(usernowname,"root")==0){
         system("cls");
         string cmd[3];
         while(1){
            cout<<"["<<usernowname<<"#]>>>";
            cin>>cmd[0]>>cmd[1]>>cmd[2];
            chomd(cmd[0],cmd[1],cmd[2]);
        }
      }else{
        system("cls");
         string cmd[3];
         while(1){
            cout<<"["<<usernowname<<"#]>>>";
            cin>>cmd[0]>>cmd[1]>>cmd[2];
            chomd(cmd[0],cmd[1],cmd[2]);
        }
      }
  }
}
bool login()//用户登陆
{
    char name[10];
    char pass[10];
    cout<<"Login:";
    cin>>name;
    cout<<endl;
    cout<<"password:";
    cin>>pass;
    if((strcmp("root",name)==0)&&(strcmp("123456",pass)==0))//管理员
    {
        usernowpride = 1;
        strcpy(usernowname,"root");
        return true;
    }
    FILE *fp=NULL;
    struct user actuser;
    if(!(fp=fopen("user","ab+"))) //ab+ 以二进制方式打开,并在原内容后追加
    {
        cout<<"Error:用户表错误\n";
        return false;
    }
    rewind(fp);  //将文件指针重新指向文件的开头
    while(!feof(fp)) //判断指针是否已经到达文件的尾部
    {
        fread(&actuser,sizeof(struct user),1,fp); //把user里的数据读入到actuser中
        if((strcmp(actuser.name,name)==0)&&(strcmp(actuser.pass,pass)==0))
        {
            usernowpride = actuser.pride;//记录当前用户的权限
            strcpy(usernowname,actuser.name);//记录当前用户的主路径
            nowlevel=-1;//记录当前目录层次
            fclose(fp);
            //设置路径
            if(strcmp(usernowpath,"\\")!=0)         //不是根目录就添加斜杠
                strcat(usernowpath,"\\");
            strcat(usernowpath,usernowname);
            return true;
        }
    }
    cout<<"Error:用户名或密码无效,请核对后再输入\n";
    fclose(fp);
    return false;
}
void linux_ls(string str1,string str2,string str3){
    if(str2=="/"&&str3=="/"){//输出根目录文件
        int count=0; 
        for(int i=0;i<addrsize;i++){
            if(fileaddr[i]!=""){
                cout<<fileaddr[i]<<"  ";
                count++;
            }
            if(count%5==0&&count!=0){
                cout<<endl;//
            }
        } 
        cout<<endl;
    }else{//输出二级目录的文件 
        int index;
        for(int i=0;i<addrsize;i++){
            if(fileaddr[i]==str2){
                index=i;
                break;
            }
        }
        if(index>=addrsize){
            cout<<"文件夹不存在"<<endl;
        }else{
            int a,b,count=0;
            a=index*100;
            b=a+100-1;
            for(int i=a;i<=b;i++){
               if(fileinfo[i]!=""){
                    cout<<fileinfo[i]<<"  ";
                    count++;
               }
               if(count%5==0&&count!=0){
                    cout<<endl;
               }
            }cout<<endl;
        }
    } 
}
void chomd(string str1,string str2,string str3){
    if(str1=="mkdir"&&str2!=""&&str3!=""){
          linux_mkdir(str1,str2,str3);//创建文件夹的命令
    }else if(str1=="ls"&&str2!=""&&str3!=""){
        linux_ls(str1,str2,str3);
    }else if(str1=="remove"&&str2!=""&&str3!=""){
       linux_remove(str1,str2,str3);
    }else if(str1=="useradd"&&str2=="new"&&str3=="username"){
        createuser();
    }else if(str1=="cd"&&str2!=""&&str3!=""){
        linux_cd(str1,str2,str3);
    }else if(str1=="help"&&str2=="help"&&str3=="help"){
        chomdUser();
    }else if(str1=="ccurrent"&&str2=="username"&&str3!=""){
        //查看当前的用户
        cout<<"当前的用户:"<<usernowname<<endl; 
    }else if(str1=="change"&&str2=="username"&&str3=="login"){
        //更换登录的用户 
        logout();
    }else if(str1=="system"&&str2=="-version"&&str3=="/"){
        cout<<"文件管理系统1.1版本"<<endl;
    }else if(str1=="cd/"&&str2!=""&&str3!=""){
        linux_cd2(str1,str2,str3);
    }else{
        cout<<"输入命令不存在请查阅(help help help)"<<endl; 
    }
}
void chomdUser(){
  cout<<"=========系统命令操作============"<<endl; 
  cout<<"mkdir filename / 创建文件夹的命令"<<endl;
  cout<<"mkdir filename test在文件夹test下面创建文件filaname"<<endl; 
  cout<<"remove filename 删除文件夹filename"<<endl;
  cout<<"remove filename test 删除文件夹下面的test文件"<<endl;
  cout<<"ls / / 查看根目录的文件夹"<<endl;
  cout<<"ls test / 查看test文件夹下面的目录"<<endl; 
  cout<<"useradd new username 创建新的用户"<<endl;
  cout<<"change username login 切换登录的用户"<<endl; 
  cout<<"ccurrnt username / 查看当前的用户"<<endl;
  cout<<"system -version / 查看系统版本"<<endl;
  cout<<"===login===系统登录入口"<<endl;
  cout<<"cd/ / /返回到根目录"<<endl;
  cout<<"username:root;password:123456初始密码"<<endl;
}
void linux_cd2(string str1,string str2,string str3){
   if(str1=="cd/"&&str2=="/"&&str3=="/"){
        string cmd[3];
        while(1){
          cout<<"["<<usernowname<<"#]>>>";
          cin>>cmd[0]>>cmd[1]>>cmd[2];
          chomd(cmd[0],cmd[1],cmd[2]);
       }
    }
}
//注销
void logout()
{
    strcpy(usernowname,"  ");
    nowlevel=-1;
    strcpy(usernowpath,"\\");
    console();
}
//新建用户
bool createuser()
{
    struct user newuser;
    char name[10];
    char pass[10];
    int pride;
    if(usernowpride!=1)
    {
        cout<<"当前用户没有新建用户的权限\n";
        return false;
    }
    FILE *fp;
    if((fp = fopen("user","ab+"))==NULL)
    {
        cout<<"用户表打开失败\n";
        return false;
    }
    else
    {
        cout<<"请输入用户名:";
        cin>>name;
        if(strcmp(name,"root")==0)
        {
            printf("Error:此为超级管理员\n");
            return false;
        }
        rewind(fp);
        while(!feof(fp))
        {
            fread(&newuser,sizeof(struct user),1,fp);
            if(strcmp(newuser.name,name)==0)
            {
                cout<<"该用户名已经存在\n";
                fclose(fp);
                return false;
            }
        }
        cout<<"请输入用户密码:";
        cin>>pass;
        cout<<"请输入用户权限(0普通用户,1管理员):";
        cin>>pride;
        strcpy(newuser.name,name);
        strcpy(newuser.pass,pass);
        newuser.pride=pride;
 
//为新建用户建立用户目录文件
        if(!fopen(newuser.name,"ab+"))
        {
            cout<<"用户创建失败\n";
            //如创建失败则把已经建立的用户目录删除
            char cmd[20] = "DEL ";
            strcat(cmd,newuser.name);
            system(cmd);
            fclose(fp);
            return false;
        }
        if(!fwrite(&newuser,sizeof(struct user),1,fp))
        {
            cout<<"创建失败\n";
            fclose(fp);
            return false;
        }
        else
        {
 
            cout<<"用户创建成功\n";
            fclose(fp);
            return true;
        }
    }
}

 

posted @ 2020-05-11 23:39  薄眠抛却陈年事。  阅读(638)  评论(0编辑  收藏  举报