操作系统作业存储

来源:互联网 发布:免费抢票软件 编辑:程序博客网 时间:2024/06/05 20:13

//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;
}

原创粉丝点击