//wrongfile.h
#ifndef WRONGFILE_H
#define WRONGFILE_H
/*工程中可能會出現的一些異常
*SameFile:處理當向某用戶添加一個已經存在的文件是拋出
*WrongUser:查找的用戶不存在拋出這個異常
*WrongFile:查找的文件不存在
*UserExist:添加的用戶已經存在
*/
#include <exception>
using namespace std;
class SameFile: public exception
{
public:
virtual const char* what() const
{
return "文件已經存在!";
}
};
class WrongUser: public exception
{
public:
virtual const char* what() const
{
return "用戶不存在!";
}
};
class WrongFile: public exception
{
public:
virtual const char* what() const
{
return "文件不存在!";
}
};
class UserExist: public exception
{
public:
virtual const char* what() const
{
return "用戶已經存在!";
}
};
#endif
//file.h
#ifndef FILE_H
#define FILE_H
/*文件的二級存儲方式,用戶級(User),用戶目錄(MFD)
*MFD中保存用戶信息,要查詢某個文件要先查詢該用戶
*是否存在在根據MFD中給定的用戶信息區查找
*/
#include <iostream>
#include <vector>
#include <string>
#include "wrongfile.h"
using namespace std;
class MFD;
class User
{
private:
User(const string &name):m_name(name)
{}
//添加新的文件到用戶的文件目錄
void addFile(const string &fname)throw(SameFile);
//查找fname是否存在在用戶的文件目錄中
bool findFile(const string &fname);
//輸出用戶的信息
friend ostream& operator <<(ostream &os, const User &uname)
{
os<<"用戶名 "<<"文件"<<endl;
os<<uname.m_name<<" ";
string::size_type sl;
sl = uname.m_name.size();
vector<string>::size_type lengh(0);
vector<string>::const_iterator ite;
ite = uname.m_fileName.begin();
for (; lengh != uname.m_fileName.size(); ++lengh)
{
os<<*ite<<endl;
for(string::size_type i = 0; i != sl + 4; ++i)
cout<<" ";
++ite;
}
return os;
}
private:
//用戶名
string m_name;
//用戶的文件目錄
vector<string> m_fileName;
friend class MFD;
};
class MFD
{
public:
//從文件中讀入用戶和文件
MFD();
//添加新的用戶
void addUser(const string &uname)throw(UserExist);
//添加新用戶的同時添加屬於該用戶的文件
void addUFile(const string &uname, const string &fname)throw(SameFile);
//查找文件所屬的用戶,存在返回,不存在拋出異常
string& findUFile(const string &fname)throw(WrongFile);
//如果給定的用戶存在打印該用戶的信息
//如果不存在拋出異常
void printUser(const string &uname) throw(WrongUser);
//在析構的時候保存到文件
~MFD();
private:
//用戶目錄
vector<User> m_user;
//判斷某個用戶是否存在用戶 目錄中,存在返回它的位置
//不存在返回m_user.end()
vector<User>::iterator existUser(const string &uname);
};
#endif
//filefun.cpp
#include <iostream>
#include <fstream>
#include <algorithm>
#include "file.h"
using namespace std;
MFD::MFD()
{
ifstream user, file;
string username, filename;
string endOfFile(".txt");
vector<User>::iterator ite;
user.open("user.txt");
while (user>>username)
{
User u(username);
filename = username + endOfFile;
file.open(filename.c_str());
while (file>>filename)
u.m_fileName.push_back(filename);
m_user.push_back(u);
file.close();
}
}
void User::addFile(const string &fname)throw(SameFile)
{
vector<string>::iterator ite;
ite = find(m_fileName.begin(), m_fileName.end(), fname);
if (ite == m_fileName.end())
m_fileName.push_back(fname);
else
throw SameFile();
}
//這個函數不能聲明爲常函數和find有關
bool User::findFile(const string &fname)
{
vector<string>::iterator ite;
ite = find(m_fileName.begin(), m_fileName.end(), fname);
if (ite != m_fileName.end())
return true;
else
return false;
}
vector<User>::iterator MFD::existUser(const string &uname)
{
vector<User>::iterator ite;
ite = m_user.begin();
for(; ite != m_user.end(); ++ite)
{
if (ite->m_name == uname)
break;
}
return ite;
}
void MFD::addUser(const string &uname)throw(UserExist)
{
vector<User>::iterator ite;
ite = existUser(uname);
if (ite == m_user.end())
{
User u(uname);
m_user.push_back(u);
}
else
throw UserExist();
}
void MFD::addUFile(const string &uname, const string &fname)throw(SameFile)
{
vector<User>::iterator ite;
ite = existUser(uname);
if (ite == m_user.end())
{
User u(uname);
u.m_fileName.push_back(fname);
m_user.push_back(u);
}
else
ite->addFile(fname);
}
string& MFD::findUFile(const string &fname) throw(WrongFile)
{
vector<User>::iterator ite;
ite = m_user.begin();
for (; ite != m_user.end(); ++ite)
{
if (ite->findFile(fname))
break;
}
if (ite != m_user.end())
return ite->m_name;
else
throw WrongFile();
}
void MFD::printUser(const string &uname) throw(WrongUser)
{
vector<User>::iterator ite;
ite = existUser(uname);
if (ite != m_user.end())
cout<<*ite<<endl;
else
throw WrongUser();
}
MFD::~MFD()
{
ofstream user, file;
user.open("user.txt");
string endOfFile(".txt");
string filename;
vector<string>::iterator site;
vector<User>::iterator ite;
ite = m_user.begin();
for (; ite != m_user.end(); ++ite)
{
user<<ite->m_name<<" ";
filename = ite->m_name + endOfFile;
file.open(filename.c_str());
site = ite->m_fileName.begin();
for (; site != ite->m_fileName.end(); ++site)
file<<*site<<" ";
file.close();
}
}
//manager.h
#ifndef MANAGER_H
#define MANAGER_H
/*操作文件的兩種身份:普通用戶(CommonUser),管理員(Manager)
*不同的身份擁有不同的訪問文件的權限.因爲普通用戶擁有的權限
*管理員也擁有所以將CommonUser類設置爲基類,在它的基礎上派生出
*管理員類(Manager).
*/
#include <iostream>
#include <string>
#include "file.h"
using namespace std;
class CommonUser
{
public:
//構造對象的時候設置密碼
CommonUser(const string &password = "19851010"):
m_password(password)
{}
//對文件的操作
virtual void operate();
virtual ~CommonUser(){}
protected:
MFD m_mfd;
//密碼
string m_password;
//判斷密碼是否正確
bool rightPassword(const string &password)const;
//查找文件
void searchFile(const string &fname);
//查找用戶
void searchUser(const string &uname);
private:
//用戶功能菜單
virtual void showMenu()const;
};
class Manager: public CommonUser
{
public:
//構造基類
Manager():CommonUser("19851121")
{}
//對文件的操作
virtual void operate();
virtual ~Manager(){}
protected:
//添加用戶
void appendUser(const string &uname);
//添加用戶的同時添加屬於該用戶的文件
void appendUFile(const string &uname, const string &fname);
private:
//用戶功能菜單
virtual void showMenu()const;
};
inline void CommonUser::showMenu()const
{
cout<<"你可以進行的操作: "<<endl;
cout<<"1 查找文件 "<<endl;
cout<<"2 查找用戶 "<<endl;
}
inline bool CommonUser::rightPassword(const string &password)const
{
return m_password == password;
}
inline void Manager::showMenu()const
{
cout<<"你可以進行的操作: "<<endl;
cout<<"1 查找文件 "<<endl;
cout<<"2 查找用戶 "<<endl;
cout<<"3 添加用戶 "<<endl;
cout<<"4 添加用戶和文件"<<endl;
}
#endif
//manfun.cpp
#include <iostream>
#include "manager.h"
using namespace std;
void CommonUser::operate()
{
string password;
cout<<"請輸入密碼: ";
cin>>password;
cout<<endl;
if (!rightPassword(password))
{
cout<<"密碼錯誤!"<<endl;
return;
}
else
{
showMenu();
cout<<"請選擇: ";
int choice;
cin>>choice;
cout<<endl;
string name;
switch (choice)
{
case 1: cout<<"輸入要查找的文件名: ";
cin>>name;
cout<<endl;
searchFile(name);
break;
case 2: cout<<"輸入要查找的用戶: ";
cin>>name;
cout<<endl;
searchUser(name);
break;
default:cout<<"選擇錯誤!"<<endl;
break;
}
}
}
void CommonUser::searchFile(const string &fname)
{
string user;
try
{
user = m_mfd.findUFile(fname);
}
catch (WrongFile wf)
{
cerr<<wf.what()<<endl;
return;
}
cout<<"文件: "<<fname<<"屬於用戶: "<<user<<endl;
}
void CommonUser::searchUser(const string &uname)
{
try
{
m_mfd.printUser(uname);
}
catch (WrongUser wu)
{
cerr<<wu.what()<<endl;
}
}
void Manager::operate()
{
string password;
cout<<"請輸入密碼: ";
cin>>password;
cout<<endl;
if (!rightPassword(password))
{
cout<<"密碼錯誤!"<<endl;
return;
}
else
{
showMenu();
cout<<"請選擇: ";
int choice;
cin>>choice;
cout<<endl;
string uname, fname;
switch (choice)
{
case 1: cout<<"輸入要查找的文件名: ";
cin>>fname;
cout<<endl;
searchFile(fname);
break;
case 2: cout<<"輸入要查找的用戶: ";
cin>>uname;
cout<<endl;
searchUser(uname);
break;
case 3: cout<<"輸入要添加的用戶名: ";
cin>>uname;
cout<<endl;
appendUser(uname);
break;
case 4: cout<<"輸入要添加的用戶名: ";
cin>>uname;
cout<<endl;
cout<<"輸入文件名: ";
cin>>fname;
cout<<endl;
appendUFile(uname, fname);
break;
default:cout<<"選擇錯誤!"<<endl;
break;
}
}
}
void Manager::appendUser(const string &uname)
{
try
{
m_mfd.addUser(uname);
}
catch(UserExist u)
{
cerr<<u.what()<<endl;
}
}
void Manager::appendUFile(const string &uname, const string &fname)
{
try
{
m_mfd.addUFile(uname, fname);
}
catch (SameFile sf)
{
cout<<"輸入的用戶已經存在!"<<endl;
cout<<endl;
cerr<<sf.what()<<endl;
}
}
//main.cpp
#include <iostream>
#include "manager.h"
using namespace std;
int main()
{
CommonUser *pu;
int choice;
cout<<"登錄身份:"<<endl;
cout<<"1 用戶 "<<endl;
cout<<"2 管理員"<<endl;
cout<<"選擇: ";
cin>>choice;
cout<<endl;
if (1 == choice)
pu = new CommonUser();
else
{
if (2 == choice)
pu = new Manager();
else
{
cout<<"選擇錯誤!"<<endl;
return -1;
}
}
pu->operate();
delete pu;
return 0;
}