• 大小: 11KB
    文件类型: .cpp
    金币: 2
    下载: 0 次
    发布日期: 2024-01-28
  • 语言: C/C++
  • 标签:

资源简介

用C++的容器和文件实现的职工管理系统

资源截图

代码片段和文件信息

#include 
#include 
#include 
#include 
#include  //vector本质是动态数组
#include 
#include  //sort()函数在这个头文件中
using namespace std;

class Person
{
public:
string name;
string tel;
string adr;
string ID; //身份证号
public:
Person();
Person(string string string string);
};
Person::Person()
{}
Person::Person(string m_name string m_tel string m_adr string m_ID)
{
name = m_name;
tel = m_tel;
adr = m_adr;
ID = m_ID;
}

class Worker :public Person
{
public:
double code; //工号
string section; //部门
double salary; //工资
string email; //邮箱
public:
Worker();
Worker(string string string string double string double string);
};
Worker::Worker()
{}
Worker::Worker(string m_name string m_tel string m_adr string m_ID double cd string sec double sala string ema) :Person(m_name m_tel m_adr m_ID)
{
code = cd;
section = sec;
salary = sala;
email = ema;
}

class Control
{
public:
Control();
void menu();   //主菜单界面
void find(std::vector &ver);            //查询信息
void edit(std::vector &ver);            //修改信息
void del(std::vector &ver);             //删除信息
void insert(std::vector &ver);         //添加信息
void list(std::vector&ver);              //浏览信息
void sortlist(std::vector&ver);

void readfile(std::vector&ver);
void writefile(std::vector&ver);

};

void Control::readfile(std::vector&ver)
{
ifstream infile(“record.txt“ ios::in); //定义文件流对象用输入输出方式打开磁盘文件,文件可读可写
if (!infile)
{
cerr << “读取文件出错,可能是没有此文件“ << endl;
return;
}

if (infile.eof()) //判断文件是否到达尾部
{
cout << “ 文件为空 “ << endl;
return;
}

while (!infile.eof())
{
Worker wo;
infile >> wo.name >> wo.tel >> wo.adr >> wo.ID >> wo.code >> wo.section >> wo.salary >> wo.email;

ver.push_back(wo);
}
}

void Control::writefile(std::vector&ver)
{
ofstream outfile(“record.txt“ ios::out); //定义文件流对象用输入输出方式打开磁盘文件,文件可读可写
if (!outfile) //打开文件失败
{
cerr << “open error!“ << endl;
exit(1);
}

vector::iterator it = ver.begin(); //定义一个迭代器,通过它将容器内容写入文件
for (; it != ver.end(); it++)
{
outfile << ‘\t‘ << (*it).name << ‘\t‘ << (*it).tel << ‘\t‘ << (*it).adr << ‘\t‘ << (*it).ID << ‘\t‘ << (*it).code << ‘\t‘ << (*it).section << ‘\t‘ << (*it).salary << ‘\t‘ << (*it).email;
}

}

Control::Control()
{}
void Control::menu()       //主菜单界面
{
cout << endl;
cout << “***************************“ << endl;
cout << “* 容器实现的职工管理系统  *“ << endl;
cout << “* (1)增加       (2)删除   *“ << endl;
cout << “* (3)修改       (4)查询   *“ << endl;
cout << “* (5)显示       (6)排序   *“ << endl;
cout << “* (0)退出                 *“ << endl;
cout << “***************************“;
cout << endl;
c

评论

共有 条评论