教职工信息管理系统

(学生党一枚,希望给出建议并且多多支持,谢谢大家)
C++课程设计:

/*教职工信息管理
基本要求:
定义职工(employee )类,其中至少包括姓名、性别、工号、电话、所在系部和职称。
功能要求:
       1、设计菜单实现功能选择;   
       2、输入功能:输入职工信息,并保存到文件中;
       3、查询功能:
           1)能够根据工号精确查询职工信息;
           2)能够根据姓名、科室查询职工信息
	       3)分系部进行职称统计,计算各职称的人数
       4、根据职工的职称排序输出
       5、根据工号修改职工信息
       6、根据工号删除职工信息*/

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

运行主界面:(其余的图就不图啦)

在这里插入图片描述
谢谢。

  • 17
    点赞
  • 74
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值