文件管理系统
#include <bits/stdc++.h>
using namespace std;
class MyFile;
class FileUser;
map<string,string> AdministList;
map<string,string> OrderUser;
map<MyFile,bool> AllFiles;
string getCurrentTime();
class MyFile
{
private:
static int AlltheFileNumber;
static int toAllotFileID;
static string WR_Access_Permission[3];
enum prLevel
{
AllUser_WR = 0,
Creater_WR_Other_ReadOnly = 1,
OnlyCreater_WR=2,
};
int FileID;
string FileName;
string FileContent;
string CreateUser;
string CreateTime;
string LastAlertTime;
string LastAlertUserName;
enum prLevel ProtectLevel;
int FileLength;
public:
MyFile(){AlltheFileNumber++;}
~MyFile(){AlltheFileNumber--;}
MyFile(string fileName,string fileContent,string createTime,string createUser,enum prLevel prl);
int getFileLength();
void BuildFile(string creater,int WRflag);
void AlertFile(string UserName);
void showFileMessage();
void showFileContent();
bool operator < (const MyFile& rhs)const
{
return FileID < rhs.FileID;
}
};
class FileUser
{
private:
string Name;
string Password;
public:
vector<MyFile> FileList;
int FileNumber;
bool isAdministrator;
FileUser(){}
FileUser(string name,bool isAd);
string getName();
string getPassWord();
void setName(string name);
void setPassword(string password);
void UserCreateFile();
};
int main()
{
return 0;
}
string getCurrentTime()
{
time_t t = time(0);
char tmp[64];
strftime( tmp, sizeof(tmp), "%Y/%m/%d %X",localtime(&t) );
return string(tmp);
}
int MyFile::AlltheFileNumber = 0;
int MyFile::toAllotFileID = 0;
string MyFile::WR_Access_Permission[] =
{
string("所有用户可读写"),
string("创建者可读写,其它用户只读"),
string("创建者可读写,其它用户不可操作")
};
MyFile::MyFile(string fileName,string fileContent,string createTime,string createUser,enum prLevel prl)
{
AlltheFileNumber++;
FileID = ++toAlloteFileID;
FileName = fileName;
CreateTime = createTime;
FileContent = fileContent;
CreateUser = createUser;
ProtectLevel = prl;
LastAlertUserName = CreateUser;
FileLength = FileContent.length();
LastAlertTime = CreateTime;
}
void MyFile::AlertFile(string UserName)
{
if(ProtectLevel>0 && UserName!=CreateUser)
{
cout<<"此文件只能创建者修改!\n";
return;
}
cout<<"请选择操作方式:\n";
cout<<"1.重写文件\n";
cout<<"2.在文件后面追加内容\n";
int op,t=1;
while(t)
{
cout<<"请输入操作方式:_\b";
cin>>op;
if(op!=1||op!=2)
{
continue;
}
t--;
string Ask;
switch (op)
{
case 1:
Ask = "写入";
break;
case 2:
Ask = "追加";
}
cout<<"请输入要"<<Ask<<"的内容,每输入一行后按回车键,输入<END>结束输入\n";
string Source,Line;
while(Line!="<END>")
{
cout<<">>>";
cin>>Line;
Source += Line;
}
switch (op)
{
case 1:
FileContent = Source;
break;
case 2:
FileContent += Source;
break;
}
LastAlertUserName = UserName;
LastAlertTime = getCurrentTime();
FileLength = FileContent.length();
cout<<"读写文件成功!\n";
t = 1;
string choose;
while(t--)
{
cout<<"是否显示文件信息?Y/N _\b";
cin>>choose;
switch(choose[0])
{
case 'Y':
showFileContent();
break;
case 'y':
showFileContent();
break;
case 'N':
break;
case 'n':
break;
default:
t++;
}
}
}
}
void MyFile::showFileMessage()
{
cout<<"文件信息如下:\n";
cout<<"---------------------------------------"<<endl;
cout<<"|文件名 :"<<FileName<<endl;
cout<<"|文件创建时间 :"<<CreateTime<<endl;
cout<<"|文件创建用户 :"<<CreateUser<<endl;
cout<<"|文件最后修改时间:"<<LastAlertTime<<endl;
cout<<"|文件最后修改用户:"<<LastAlertUserName<<endl;
cout<<"|文件长度 :"<<FileLength<<endl;
cout<<"|文件读写权限 :"<<WR_Access_Permission[(int)ProtectLevel]<<endl;
cout<<"---------------------------------------"<<endl;
}
void MyFile::showFileContent()
{
cout<<"文件"<<FileName<<"的内容如下:\n";
cout<<FileContent<<endl;
}
int MyFile::getFileLength()
{
return FileLength;
}
void MyFile::BuildFile(string creater,int WRflag=0)
{
FileID = ++toAlloteFileID;
CreateUser = LastAlertUserName = creater;
cout<<"请输入文件名:______\b\b\b\b\b\b";
cin>>FileName;
cout<<"是否要输入文件内容?(Y/N)__\b";
string s,op;
int t = 1;
string Source,Line="";
while(t--)
{
cout<<"请输入Y或N:_\b";
cin>>op;
switch (op[0])
{
case 'Y':
case 'y':
cout<<"请输入要写入文件的内容,每输入一行后按回车键,输入<END>结束输入\n";
Line = "";
while(Line!="<END>")
{
cout<<">>>";
cin>>Line;
Source += Line;
}
FileContent = Source;
FileLength = Source.length();
break;
case 'N':
break;
case 'n':
break;
default:
t++;
break;
}
}
LastAlertTime = CreateTime = getCurrentTime();
if(WRflag>2) WRflag = 0;
WRflag = max(0,WRflag);
if(WRflag==0)
{
ProtectLevel = AllUser_WR;
}
else if(WRflag==1)
{
ProtectLevel = Creater_WR_Other_ReadOnly;
}
else
{
ProtectLevel = OnlyCreater_WR;
}
}
FileUser::FileUser(string name,bool isAd)
{
Name = name, isAdministrator = isAd;
FileNumber = 0;
}
string FileUser::getName()
{
return Name;
}
string FileUser::getPassWord()
{
return Password;
}
void FileUser::setName(string name)
{
this->Name = name;
}
void FileUser::setPassword(string password)
{
this->Password = password;
}
void FileUser::UserCreateFile()
{
int t = 1;
string op;
while(t)
{
cout<<"确定要创建文件吗?Y/N _\b";
cin>>op;
switch (op[0])
{
case 'N':
return;
case 'n':
return;
case 'Y':
t--;
break;
case 'y':
t--;
break;
default:
cout<<"输入错误!请输入Y或N\n";
}
}
MyFile CurrentFile = MyFile();
int RWflag=-1;
while(RWflag<0||RWflag>2)
{
cout<<"请输入要创建文件的读写权限(0,1,2):"<<endl;
cout<<" 1.所有用户均可读写"<<endl;
cout<<" 2.所有用户可读,只有创建者(您)才能修改文件内容"<<endl;
cout<<" 3.您的个人私有文件,只有创建者(您)才能对文件读写,其它用户不能读写该文件";
cout<<"请输入0或1或2:_\b";
cin>>op;
RWflag = op[0]-'0';
}
CurrentFile.BuildFile(this->Name,RWflag);
cout<<"文件创建成功!\n";
FileNumber = FileList.size();
}