c++ 链表

来源:互联网 发布:nat网络层 编辑:程序博客网 时间:2024/06/05 18:50

//引入相应的c++库文件
#include<string>
#include<iostream>
#include<iomanip>
#include<conio.h>
// 宏定义变量
#define FILENAME_LENGTH 10 
#define COMMAND_LENGTH 10  
#define PARA_LENGTH 30    
using namespace std; //命名空间std内定义的所有标识符都有效


//账号结构
typedef struct users 
{ //声明一个结构体
char     name[8]; //用户名
char     pwd[10]; //密码
}
 users; //users=>struct实例
struct fnode//文件结构链表
{
char filename[FILENAME_LENGTH];
int  isdir; 
    int isopen; 
    char content[255]; //内容   
    fnode *parent; //父节点
    fnode *child;//子节点
    fnode *prev; //上一个节点
    fnode *next; //下一个节点
};


users usrarray[4] = /*用户名:user 密码:pwd*/
{
 "user","pwd",
 "root","root",
};


 fnode *initfile(char filename[],int isdir);//初始化文件或目录
 void createroot();//建立系统根目录
 int run(int r);//系统运行
 int findpara(char *topara);//对参数进行处理
 bool chklogin(char *users, char *pwd);//检查账号与口令 
 void help(int r);//命令列表
 int mkdir();//建立目录
 int create();//建立文件
 int read();//读取文件
 int write();//写入文件
 int del();//删除文件
 int cd();//切换目录
 int dir();//文件与目录列表 
 fnode *root,*recent,*temp,*ttemp,*zz;
 char para[PARA_LENGTH],command[COMMAND_LENGTH],temppara[PARA_LENGTH],recentpara[PARA_LENGTH];


//创建文件与目录结点
 fnode* initfile(char filename[],int isdir)
{
 fnode *node=new fnode;
 strcpy(node->filename,filename);
 node->isdir=isdir;
 node->isopen=0;
 node->parent=NULL;
 node->child=NULL;
 node->prev=NULL;
 node->next=NULL;
 return node;
}


//创建文件存储结点
void createroot ()
{
   recent=root=initfile("/",1);
   root->parent=NULL;
   root->child=NULL;
   root->prev=root->next=NULL;
   strcpy(para,"/");  
}






int create()   //创建文件
{  

temp=initfile(" ",0);
    cin>>temp->filename;//文件名
    cin>>temp->content;//文件信息
if(recent->child==NULL){  //条件满足

/*建立文件*/
temp->parent=recent;
        temp->child=NULL;
temp->isdir=0;
        zz=temp;
recent->child=temp;
        temp->prev=temp->next=NULL;
        cout<<"文件建立成功!"<<endl;

}else{


ttemp=recent->child;
if(strcmp(zz->filename,temp->filename)==0&&zz->isdir==0){
printf("对不起,文件已存在!\n");
return 1;
}else{

while(ttemp->next) //遍历路径 查找 
{

ttemp=ttemp->next;
if(strcmp(ttemp->filename,temp->filename)==0&&ttemp->isdir==0)//如果文件存在
{
printf("对不起,文件已存在!\n");
return 1;
}
}
}
ttemp->next=temp;
        temp->parent=NULL;
        temp->child=NULL;
        temp->prev=ttemp;
        temp->next=NULL;
        cout<<"文件建立成功!"<<endl;

}
return 1;
}
 


int read()//文件读取
{
char filename[FILENAME_LENGTH];
    cin>>filename;
    if(recent->child==NULL)
{
cout<<"文件不存在!"<<endl;
return 1;
}
if(strcmp(recent->child->filename,filename)==0)
{
cout<<recent->child->content<<endl;//输出读取信息
return 1;
}
else
{
temp=recent->child;
while(temp->next)//遍历路径
{
if(strcmp(temp->next->filename,filename)==0)
{
cout<<temp->next->content<<endl;//输出读取信息
return 1;}
}
cout<<"文件不存在!"<<endl;
}
}


int write()//文件写入
{
char filename[FILENAME_LENGTH];
cin>>filename;
if(recent->child==NULL)//判断文件是否存在
{
cout<<"文件不存在!"<<endl;
return 1;
}
if(strcmp(recent->child->filename,filename)==0)//文件存在
{
char con[200];
        recent->child->isopen=1;
   cin>>con; //写入内容
   strcat(recent->child->content,con);//文件写入
        recent->child->isopen=0;
        cout<<"文件写入成功!"<<endl;
        return 1;
}
else
   {
temp=recent->child;
while(temp->next)//循环便利路径
{
if(strcmp(temp->next->filename,filename)==0)//文件存在写入
{
char con[200];
recent->child->isopen=1;
strcat(recent->child->content,con);//文件写入
recent->child->isopen=0;
cout<<"文件写入成功!"<<endl;
return 1;}
}
cout<<"文件不存在!"<<endl;
}
}




int rename()//文件重命名
{
char filename[FILENAME_LENGTH];
cin>>filename;
    if(recent->child==NULL)//判断文件是否存在
{
cout<<"文件不存在!"<<endl;
return 1;
}
if(strcmp(recent->child->filename,filename)==0)//存在
{
cin>>recent->child->filename;//重命名
cout<<"重命名成功!"<<endl;
return 1;
}
   else
   {
  temp=recent->child;
  while(temp->next)//遍历路径
  {
  if(strcmp(temp->next->filename,filename)==0)//存在
  {
  cin>>temp->next->filename;//重命名
  cout<<"重命名成功!"<<endl;
  return 1;}
  }
  cout<<"文件不存在!"<<endl;
   }
}






int del()//文件删除
{
char filename[FILENAME_LENGTH]; 
cin>>filename;//输入文件名
temp=new fnode; //实例化一个文件链表
if(recent->child)//判断他的子节点
{
temp=recent->child;
while(temp->next && (strcmp(temp->filename,filename)!=0 || temp->isdir!=0))//循环判断
temp=temp->next;
if(strcmp(temp->filename,filename)!=0) //如何节点下没有
{
cout<<"不存在该文件!"<<endl;
return 0;
}
}
else
{
cout<<"不存在该文件!"<<endl;
return 0;
}
if(temp->parent==NULL)
{
temp->prev->next=temp->next;
if(temp->next)
temp->next->prev=temp->prev;
temp->prev=temp->next=NULL; //清空
}
else
  {
   if(temp->next)//如果不为空
    temp->next->parent=temp->parent;//给赋值 
   temp->parent->child=temp->next;
 }
  delete temp;
  cout<<"文件已删除!"<<endl;
}
 
bool chklogin(char *users, char *pwd)//退出函数
{
 int i;
 for(i=0; i<4; i++)
 {
  if( (strcmp(users,usrarray[i].name)==0) && (strcmp(pwd,usrarray[i].pwd)==0))//匹配用户信息
   return true;
 }
 return false;
}


int mkdir()//建立目录
{  
 temp=initfile(" ",1);
 cin>>temp->filename;//获得输入的文件路径
 if(recent->child==NULL)  //判断是否为空
   {
     temp->parent=recent;
     temp->child=NULL;
     recent->child=temp;
     temp->prev=temp->next=NULL;   
 }
   else//不为空
   {
    ttemp=recent->child;
 while(ttemp->next)//循环路径 查找
 {
    ttemp=ttemp->next;
    if(strcmp(ttemp->filename,temp->filename)==0&&ttemp->isdir==1)
    {
     printf("对不起,目录已存在!");
     return 1;
     }
 }
   ttemp->next=temp;
   temp->parent=NULL;
   temp->child=NULL;
   temp->prev=ttemp;
   temp->next=NULL;
   
  }
return 1;
}


int redir() //重命名文件夹
{
char filename[FILENAME_LENGTH];
char newfilename[FILENAME_LENGTH];
cin>>filename;
cin>>newfilename;
    if(recent->child==NULL)
{
cout<<"文件夹不存在!"<<endl;
return 1;
}
if(strcmp(recent->child->filename,filename)==0&&recent->child->isdir==1)
{
  ttemp=recent->child;
  while (ttemp->next)
  {
  ttemp=ttemp->next;
  if(strcmp(ttemp->filename,newfilename)==0&&ttemp->isdir==1)
  {
  printf("文件夹已存在-1");
  return 1;
  }
  }
  strcpy(recent->child->filename,newfilename);
  cout<<"重命名成功!"<<endl;
   }else{
  ttemp=recent->child;
  while(ttemp->next)
  {
  ttemp=ttemp->next;
  if((strcmp(ttemp->filename,newfilename)==0)&&(ttemp->isdir==1))
  {
  printf("文件夹已存在-2");
  return 1;
  }
  }
  ttemp=recent->child;
  while (ttemp->next)
  {
  ttemp=ttemp->next;
  if((strcmp(ttemp->filename,filename)==0)&&(ttemp->isdir==1))
  {  
  strcpy(ttemp->filename,newfilename);
    cout<<"重命名成功!"<<endl;
return 1;
  }
  }
  cout<<"文件夹不存在-3"<<endl;
   }
}




int cd() //切换目录
{  char topara[PARA_LENGTH];
   cin>>topara;
   if(strcmp(topara,"..")==0)
   {
      int i;
 while(recent->prev)
 recent=recent->prev;
 if(recent->parent)
 {
 recent=recent->parent;
 }
 i=strlen(para);
 while(para[i]!='/' && i>0) i--;
 if(i!=0)
 para[i]='\0';
 else 
 para[i+1]='\0';
   }
   else
   {
  findpara(topara); 
   }
   return 1;
}
int findpara(char *topara)
{
   int i=0;
   if(strcmp(topara,"/")==0)
   {
    recent=root;
    strcpy(para,"/");
    return 1;
   }
   temp=recent;
   strcpy(temppara,para);
   if(topara[0]=='/')
   {
  recent=root->child;
  i++;
  strcpy(para,"/");
   }
   else
   {
  if(recent!=NULL && recent!=root)
  strcat(para,"/");
        if(recent && recent->child)
  {
   if(recent->isdir)
           recent=recent->child;
         else
   {
      printf("路径错误!\n");
   return 1;
  }
  }
}
   while(i<=strlen(topara) && recent)
   {
    int j=0;
 
   while(topara[i]!='/' && i<=strlen(topara))
   {
      recentpara[j]=topara[i];
 i++;j++;
   }
   recentpara[j]='\0';
   while((strcmp(recent->filename,recentpara)!=0 || (recent->isdir!=1)) && recent->next!=NULL)
   {
  recent=recent->next;
   }
   if(strcmp(recent->filename,recentpara)==0)
   {
  if(recent->isdir==0)
  {
  strcpy(para,temppara);
  recent=temp;
  printf("是文件不是目录。\n");
  return 0;
  }
  strcat(para,recent->filename);
   }
   if(strcmp(recent->filename,recentpara)!=0 || recent==NULL)
   {
  strcpy(para,temppara);
  recent=temp;
  printf("输入路径错误\n");
  return 0;
   }
   }
   return 1;
}




/*BOOL Delete Directory(char *DirName) //删除目录

CFileFindtempFind;
char tempFileFind[200]; 
    sprintf (tempFileFind,"%s\\*.*",DirName); 
    BOOL Is Finded=(BOOL)tempFind.FindFile(tempFileFind); 
while(IsFinded)    

    Is Finded=(BOOL)tempFind.FindNextFile(); 
if(!tempFind.IsDots())        

      char foundFileName[200]; 
      strcpy (foundFileName,tempFind.GetFileName().GetBuffer(200));
 if(tempFind.IsDirectory())              
 { 
 char tempDir[200]; 
          sprintf(tempDir,"%s\\%s",DirName,foundFileName); 
 Delete Directory(tempDir);               
 } 
 else 
             { 
char temp FileName[200]; 
sprintf (tempFileName,"%s\\%s",DirName,foundFileName); 
DeleteFile(tempFileName);               }          }    } 
tempFind.Close(); 
if
(!RemovwDirctory(DirName))   

Message Box(0,"删除目录失败!","警告信息",MK_OK); return FALSE;    

return TRUE; 

*/


int ls()  //显示当前目录和文件的个数
{
int i=0,j=0;
temp=new fnode;
temp=recent;
if(temp!=root)
{
cout<<"      <DIR>                         "<<".."<<endl;
}
if(temp->child==NULL)
{
cout<<"Total: "<<" directors                  " <<i<<"          files                "<< j <<endl;
return 1;
}
temp=temp->child;
while(temp)
{
if(temp->isdir)
{cout<<"      <DIR>                        "<<temp->filename<<endl;i++;}
else
{
cout<<"      <FILE>                       "<<temp->filename<<endl;j++;}
temp=temp->next;
}
cout<<"Total: "<<" directors                  " <<i<<"          files                "<< j <<endl;
}


void help(int r) //信息输出
{
if(r==1){
cout<<endl;
cout<<"create:             建立文件                 "<<endl;
cout<<"read:               读取文件                 "<<endl; 
cout<<"write:              写入文件                 "<<endl;
cout<<"rename:             文件重命名               "<<endl;
cout<<"del:                删除文件                 "<<endl;
cout<<"mkdir:              建立目录                 "<<endl;
cout<<"redir:              目录重命名               "<<endl;
cout<<"cd:                 切换目录                 "<<endl;
cout<<"logout:             退出登录                 "<<endl;
cout<<"ls:                 显示当前目录和文件的个数 "<<endl;
}else{
cout<<endl;
cout<<"create:             建立文件                 "<<endl;
cout<<"read:               读取文件                 "<<endl; 
cout<<"rename:             文件重命名               "<<endl;
cout<<"cd:                 切换目录                 "<<endl;
cout<<"logout:             退出登录                 "<<endl;
cout<<"ls:                 显示当前目录和文件的个数 "<<endl;
}

}


int run(int r)
{
if(r==1){
cout<<"linux:"<<para<<">";
 cin>>command;
  if(strcmp(command,"mkdir")==0) //如何输入条件满足 执行相应的方法函数
  mkdir();//建立目录
else if(strcmp(command,"ls")==0)
  ls(); //显示文件个数
else if(strcmp(command,"cd")==0)
  cd();//切换目录
 else if(strcmp(command,"create")==0)
  create();//创建文件
else if(strcmp(command,"read")==0)
  read();//读取文件
else if(strcmp(command,"write")==0)
  write();//写入文件
 else if(strcmp(command,"rename")==0)
  rename();//文件重命名
 else if(strcmp(command,"redir")==0)
  redir();//文件重命名
else if(strcmp(command,"del")==0)
  del();//删除文件
else if(strcmp(command,"help")==0)
  help(r);//帮助信息
 else if(strcmp(command,"logout")==0)
return 0;//退出系统
else
 cout<<"请参考help提供的命令列表1!"<<endl;
  
}else{

    cout<<"linux:"<<para<<">";
cin>>command;
if(strcmp(command,"ls")==0)
  ls(); //显示文件个数
else if(strcmp(command,"cd")==0)
  cd();//切换目录
else if(strcmp(command,"create")==0)
  create();//创建文件
else if(strcmp(command,"read")==0)
  read();//读取文件
else if(strcmp(command,"rename")==0)
  rename();//文件重命名
else if(strcmp(command,"redir")==0)
  redir();//文件重命名
else if(strcmp(command,"help")==0)
  help(r);//帮助信息
else if(strcmp(command,"logout")==0)
  return 0;//退出系统
else
 cout<<"请参考help提供的命令列表2!"<<endl;
 ;


}
}




int main() //程序执行开始
{
int r=0;
int i=0;
char pwds[12],*p; //初始化密码数组 和一个指针 并指向 这个数组
p=pwds;


bool in=false;//登陆标志 true 为登陆 false 未登陆
char users[8];//用户名数组
    cout<<"***************************************************************"<<endl;
    cout<<"*                     二级Linux文件系统                       *"<<endl;
    cout<<"*                    账号:user  密码:pwd                    *"<<endl; 
    cout<<"*        你只有三次机会来试验账号,键入help可以获取帮助        *"<<endl;                                        
    cout<<"***************************************************************"<<endl;
    cout<<endl;
while(i<3)
{
cout<<"请输入用户名(Name):";
cin>>users;
cout<<"请输入密  码(Pass):";
//cin>>pwd;
while((*p=getch())&&*p!=13) //输入密码显示星号
{
putchar('*');
p++; //指针长度+1
}
*(p++)=0;
if(chklogin(users,pwds)) //判断等登陆信息 是否正确
{
in=true;//登陆成功修改登陆标示
if(strcmp(users,"root")==0){
r=1;
}
cout<<"用户验证成功!";
cout<<endl;
createroot(); //创建文件存储节点
printf("创建根目录成功!\n");
help(r);//显示帮助信息
while(in) //如何登陆了
{
if(!run(r))//判断程序执行方法
break;
}
break;
}
else
{
cout<<"输入错误!"<<endl;
}
i++;
if(i>=3){
cout<<"密码错误3次以上! "<<endl;
}
}


}

0 0