本文实例为大家分享了C++实现教职工信息管理系统的具体代码,供大家参考,具体内容如下
#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;
class employee
{
public:
employee()
{
}
employee(char na[50], char sex[50], int num, char tel[20], char off[50], char pos[50]) :m_number(num)
{
strcpy(m_name, na);
strcpy(m_sex, sex);
strcpy(m_telephone, tel);
strcpy(m_office, off);
strcpy(m_posting, pos);
}
int getnum()
{
return m_number;
}
char *getna() //
{
return m_name;
}
char *getoff()
{
return m_office;
}
char *getposting() //
{
return m_posting;
}
friend ostream& operator <<(ostream &os, const employee & s)
{
os << "姓名:" << s.m_name << " 性别:" << s.m_sex << " 工号:" << s.m_number << " 电话:" << s.m_telephone << " 所在系部:" << s.m_office << " 职称:" << s.m_posting << endl;
return os;
}
friend istream& operator >>(istream &is, employee &s)
{
is >> s.m_name >> s.m_sex >> s.m_number >> s.m_telephone >> s.m_office >> s.m_posting;
return is;
}
~employee()
{
}
private:
char m_name[50];
char m_sex[50];
int m_number;
char m_telephone[20];
char m_office[50];
char m_posting[50];
};
菜单功能选择:
void menu() //1)菜单功能选择
{
cout << "************************************* 教职工信息管理系统 *************************************" << endl;
cout << "**----------------------------------------------------------------------------------------------**" << endl;
cout << "** ----------------------------- **" << endl;
cout << "** | * 1.录入人员信息并保存 | **" << endl;
cout << "** | * 2.按工号查找人员 | **" << endl;
cout << "** | * 3.按姓名+科室查找人员 | **" << endl;
cout << "** | * 4.分系部职称人数统计 | **" << endl;
cout << "** | * 5.按职称对人员排序 | **" << endl;
cout << "** | * 6.按工号修改人员信息 | **" << endl;
cout << "** | * 7.按工号删除人员信息 | **" << endl;
cout << "** | * 8.查看所有人员信息 | **" << endl;
cout << "** | * 0.关闭系统 | **" << endl;
cout << "** ----------------------------- **" << endl;
cout << "**----------------------------------------------------------------------------------------------**" << endl;
cout << "**请输入序号使用对应的功能:(0,1,2,3,4,5,6,7,8) **" << endl;
cout << "**************************************************************************************************" << endl;
}
输入信息:
void input() //2)输入保存
{
fstream fs;
int len;
cout << "请输入教职工人数:" << endl;
cin >> len;
employee *emp = new employee[len]; //开辟空间,存教职工数据
fs.open("guanli.dat", ios::out | ios::app | ios::binary);
if (!fs)
cout << "Open failed." << endl;
else
{
cout << "Open succeedly." << endl;
cout << "请输入教职工的姓名,性别,工号,电话,所在系部,职称:" << endl;
for (int i = 0; i<len; i++)
{
cin >> emp[i];
fs.write((char *)& emp[i], sizeof(emp[i])); //fs<<emp[i]<<endl;二进制
}
}
fs.close();
delete[] emp;
}
查询功能(根据工号):
void find1() //3-1)查询---能够根据工号精确查询职工信息;
{
int num;
cout << "请输入教职工的工号:" << endl;
cin >> num;
fstream fs;
fs.open("guanli.dat", ios::in | ios::binary);
fs.seekg(0, ios::end); //文件调到末尾
int s = fs.tellg(); //告诉文件大小
int n = s / sizeof(employee); //计算职工人数
fs.seekg(ios::beg); //文件指针调到文件开头
employee *emp = new employee[n];
for (int i = 0; i<n; i++)
fs.read((char *)& emp[i], sizeof(emp[i])); //读入到内存
fs.close();
int a = -100;
for (i = 0; i<n; i++)
{
if (num == emp[i].getnum())
{
cout << emp[i];
a = i;
}
}
if (a == -100)
cout << "工号不正确!无此人!" << endl;
delete[] emp;
}
查询功能(根据姓名、科室):
void find2() //3-2)查询---能够根据姓名、科室查询职工信息
{
char na[50];
char off[50];
cout << "请输入教职工的姓名:" << endl;
cin >> na;
cout << "请输入教职工所属科室:" << endl;
cin >> off;
fstream fs;
fs.open("guanli.dat", ios::in | ios::binary);
fs.seekg(0, ios::end);
int s = fs.tellg();
int n = s / sizeof(employee);
fs.seekg(ios::beg);
employee *emp = new employee[n];
for (int i = 0; i<n; i++)
fs.read((char *)&emp[i], sizeof(emp[i]));
fs.close();
int a = -100;
for (i = 0; i<n; i++)
{
if (strcmp(na, emp[i].getna())==0 && strcmp(off, emp[i].getoff())==0)
{
cout << emp[i] << endl;
a = i;
}
}
if (a == -100)
cout << "名字或所属科室不正确!无此人!" << endl;
}
统计人数:
void find3() //3-3)分系部进行职称统计,计算各职称的人数
{
char off[50], posting[50];
cout << "请输入所查系部:" << endl;
cin >> off;
cout << "请输入所查职称:" << endl;
cin >> posting;
fstream fs;
fs.open("guanli.dat", ios::in | ios::binary);
fs.seekg(0, ios::end);
int s = fs.tellg();
int n = s / sizeof(employee);
fs.seekg(ios::beg);
employee *emp = new employee[n];
for (int i = 0; i<n; i++)
fs.read((char *)&emp[i], sizeof(emp[i]));
fs.close();
int sum = 0;
for (i=0; i<n; i++)
{
if (strcmp(off, emp[i].getoff()) == 0 && strcmp(posting, emp[i].getposting()) == 0)
sum++;
}
cout << "该部门此职称有" << sum << "人!" << endl;
delete[] emp;
}
排序输出:
void output() //4)根据职工的职称排序输出
{
fstream fs;
fs.open("guanli.dat", ios::in | ios::binary);
fs.seekg(0, ios::end);
int s = fs.tellg();
int n = s / sizeof(employee);
fs.seekg(ios::beg);
employee *emp = new employee[n];
for (int i = 0; i<n; i++)
fs.read((char *)&emp[i], sizeof(emp[i]));
fs.close();
employee temp;
for (int j = 0; j<n - 1; j++)
{
for (int k = 0; k<n - 1 - j; k++)
{
if (strcmp(emp[k].getposting(), emp[k + 1].getposting())>0)
{
temp = emp[k];
emp[k] = emp[k + 1];
emp[k + 1] = temp;
}
}
}
for (i = 0; i<n; i++)
cout << emp[i];
delete[] emp;
}
修改功能:
void modify() //5)根据工号修改职工信息
{
fstream fs;
fs.open("guanli.dat", ios::in | ios::out | ios::binary);
fs.seekg(0, ios::end);
int s = fs.tellg();
int n = s / sizeof(employee);
fs.seekg(ios::beg);
employee *emp = new employee[n];
for (int i = 0; i<n; i++)
fs.read((char *)&emp[i], sizeof(emp[i]));
int num;
cout << "请输入所修改的职工号:" << endl;
cin >> num;
int a=-100;
for (i = 0; i < n; i++)
{
if (num == emp[i].getnum())
{
fs.seekp(sizeof(employee)*i);
employee e;
cout << "请输入要修改的教职工的姓名,性别,工号,电话,所属系部,职称:" << endl;
cin >> e;
fs.write((char *)&e, sizeof(employee));
cout << "职工信息修改成功!" << endl;
a = i;
}
}
if(a==-100)
cout << "工号不正确!无此人!" << endl;
fs.close();
delete[] emp;
}
删除功能:
void del() // 6)根据工号删除职工信息
{
fstream fs;
fs.open("guanli.dat", ios::in | ios::out | ios::binary);
fs.seekg(0, ios::end);
int s = fs.tellg();
int n = s / sizeof(employee);
fs.seekg(ios::beg);
employee *emp = new employee[n];
for (int i = 0; i<n; i++)
{
fs.read((char *)&emp[i], sizeof(emp[i]));
}
fs.close();
int num;
cout << "请输入要删除的职工号:" << endl;
cin >> num;
int a=-100;
for ( i = 0; i<n; i++)
{
if (num == emp[i].getnum())
a = i;
}
if(a==-100)
cout << "工号不正确!无此人!" << endl;
fs.open("guanli.dat", ios::out | ios::binary);
if (!fs)
cout << "Open failed." << endl;
else
{
cout << "Open succeedly." << endl;
for (int i = 0; i<n; i++)
{
if (i == a)
continue;
else
fs.write((char *)& emp[i], sizeof(emp[i]));
}
}
if (a >= 0 && a <= n)
cout << "删除成功!" << endl;
else
cout << "删除失败!" << endl;
fs.close();
fs.clear();
delete[] emp;
}
查看功能:
void show() //8.查看所有人员信息
{
fstream fs;
fs.open("guanli.dat", ios::in | ios::binary);
fs.seekg(0, ios::end);
int s = fs.tellg();
int n = s / sizeof(employee);
fs.seekg(ios::beg);
employee *emp = new employee[n];
for (int i = 0; i<n; i++)
{
fs.read((char *)&emp[i], sizeof(emp[i]));
cout << emp[i];
}
fs.close();
delete[] emp;
}
主函数:
int main()
{
char flag = 'n';
while (flag == 'n' || flag == 'N') //由y/n控制循环
{
menu();
int judge;
cin >> judge;
if (judge >= 0 && judge <= 8)
{
switch (judge)
{
case 0:
cout << "是否退出系统(y/n):" << endl;
cin >> flag;
break;
case 1:
input();
break;
case 2:
find1();
break;
case 3:
find2();
break;
case 4:
find3();
break;
case 5:
output();
break;
case 6:
modify();
break;
case 7:
del();
break;
case 8:
show();
break;
default:
break;
}
}
else
cout << "输入错误,请重新输入!" << endl;
cout << "------------------------------------------Press any key to continue!------------------------------" << endl;
getchar();
getchar();
system("cls");
}
return 0;
}
运行主界面:(其余的图就不图啦)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。