⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 employee.cpp

📁 实现员工工资管理,可以对员工的基本工资进行处理
💻 CPP
字号:
//****************************************
//       employee.cpp                    *
//       对人事管理中的类进行实现        *
//****************************************
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include "employee.h"
struct emp_struct {
	int number;
	char name[30];
	int gcode;
	int scode;
} estruct;//员工信息记录结构,用于记录员工信息
struct sal_struct {
	int number;
	char name[30];
	int month;
	float salary;
} sstruct;//员工工资信息记录结构,用于记录工资信息
/////////////// class employee ///////////////
void employee::addEmployee() { //增加员工信息
	cout << endl << "请输入员工姓名:";
	cin >> name;
	cout << "请输入员工编号(1000-9999):";
	cin >> number;
	while ( number<1000 || number>9999 )//检查员工编号
	{cout << endl << "员工编号应为4位整数,请重新输入:";
	cin >> number;
	}
	cout << "请输入员工所在柜组的代码(1.食品; 2.服装;  3.电器):";
	cin >> group_code;//获取员工柜组代码
	while ( group_code!='1' && group_code!='2' && group_code!='3' )//检查柜组代码
	{
		cout << "员工所在柜组的代码输入错误,请重新输入(1--3):";
		cin >> group_code;
	}
	group_code -=48;//将柜组代码由字符转换为数字
	cout << endl << "请输入员工的职务代码(1.售货员;2.经理;3.库管):";
	cin >> status_code;//获取员工职务代码
	while ( status_code!='1' && status_code!='2' && status_code!='3' )//检查职务代码
	{
		cout << "员工职务代码输入错误,请重新输入(1--3):";
		cin >> status_code;
	}
	status_code -=48;//将职务代码转换为数字
	strcpy( estruct.name,name ); //将信息写入结构estruct中
	estruct.number=number;
	estruct.gcode=group_code;
	estruct.scode=status_code;
	ofstream ofile("employee.txt",ios::app|ios::binary);//打开员工信息文件employee.txt   
	if (!ofile){
		cerr << endl << "员工信息文件打开错误!" << endl;
		return;
	}
	ofile.write( (char *)&estruct,sizeof(estruct) );//写入一条记录
	ofile.close();
}
void employee::delEmployee(){//删除员工信息
	int i;
	long inpos;//用于记录文件指针位置
	cout << endl<< "请输入待删除员工姓名:";
	cin >> name;
	fstream iofile("employee.txt",ios::in|ios::out|ios::binary);//打开员工信息文件    
	if (!iofile){
		cerr << endl << "员工信息文件读取错误!" << endl;
		return;
	}
	while ( !iofile.eof() )//一直读取到文件结束
	{
		inpos=iofile.tellg();//获取当前文件指针位置
		iofile.read( (char *)&estruct,sizeof(estruct) );//读取一条记录
		i=strcmp(estruct.name,name);
		if (i==0) {//找到待删除记录
			estruct.number=0;
			strcpy( estruct.name,"###");//标志该记录已被删除
			estruct.gcode=0;
			estruct.scode=0;
			iofile.seekp(inpos);//将文件指针从待删除记录的尾部移动到记录的头部
			iofile.write( (char *)&estruct,sizeof(estruct) );//删除该记录
			cout << endl << "删除成功!";
			break;//退出
		}
	}
	iofile.close();
	if (i!=0) cout<<"未发现待删除员工记录!";
}
void employee::statusChange(char name_str[30],int g_code,int s_code) {//员工状态改变函数
	int i;
	long inpos;//用于记录文件指针
	fstream iofile("employee.txt",ios::in|ios::out|ios::binary);//打开员工信息文件    
	if (!iofile)
	{cerr<< endl << "员工信息文件读取错误!" << endl;
	return;
	}
	while ( !iofile.eof() )//一直读到文件结束
	{
		inpos=iofile.tellg();//获取当前文件指针位置
		iofile.read( (char *)&estruct,sizeof(estruct) );//读取一条记录
		i=strcmp(estruct.name,name_str);
		if (i==0) { //找到待修改员工信息记录
			//将修改后的信息写入结构estruct
			estruct.gcode=g_code;//柜组代码
			estruct.scode=s_code;//职务代码
			iofile.seekp(inpos);//将文件指针从待修改记录的尾部移动到头部
			iofile.write( (char *)&estruct,sizeof(estruct) );//修改记录
			cout << endl<< "员工职务改变成功!";
			break;//退出
		}
	}
	iofile.close();
	if (i!=0) cout<<"未发现待删除员工记录!";
}
int employee::findStatus(char name_str[30],int *num)//查询员工职务代码及编号
{
	int i;
	ifstream ifile("employee.txt",ios::in|ios::binary);//打开员工信息文件employee.txt    
	if (!ifile)
	{cerr << endl << "员工信息文件读取错误!" << endl;
	return 0;
	}
	while ( !ifile.eof() )//一直读到文件结束
	{
		ifile.read( (char *)&estruct,sizeof(estruct) );//读取一条记录
		i=strcmp(estruct.name,name_str);
		if (i==0)//找到待查询员工记录
		{//读取该员工的编号和职务代码
			*num=estruct.number;
			return estruct.scode;
		}
	}
	cerr << endl << "无此名员工信息!";//未找到该名员工信息
	return 0;
}
void employee::countSalary(char namestr[30],int num){}//计算工资函数,用于重载
void employee::recordSalary()//记录工资信息函数
{
	char ch1;
	cout << "是否存档(Y/N):";
	cin >> ch1;
	while ( ch1!='Y' && ch1!='y' && ch1!='N' && ch1!='n' ) {cin >>ch1;}//检查输入
	if( ch1=='N' || ch1=='n' ) return; //不需要存档
	else//需要存档
	{
		cout << endl << "请输入当前月份:";
		cin >> month;
		while ( month <1 || month >12 )//简单检验输入月份
		{
			cout << endl << "输入月份错误,请重新输入:";
			cin >> month;
		}
		//将信息写入工资记录结构
		sstruct.number=number;
		strcpy( sstruct.name,name );
		sstruct.month=month;
		sstruct.salary=salary;
		ofstream ofile("salary.txt",ios::app|ios::binary);//打开员工工资文件salary.txt    
		if (!ofile)
		{cerr << endl << "员工工资文件打开错误!" << endl;
		return;
		}
		ofile.write( (char *)&sstruct,sizeof(sstruct) );//写一条记录
		ofile.close();
	}
}
void employee::query(char name_str[30])//查询员工个人和工资信息
{
	int i;//用于根据柜组代码显示对应的柜组信息
	char gname[4][10]={" ","食品柜组","服装柜组","电器柜组"}; 
	//用于根据职务代码显示对应的职务信息
	char sname[4][10]={" ","售货员","经理","库管"};
	ifstream iefile("employee.txt",ios::in|ios::binary);//打开员工信息文件employee.txt    
	if (!iefile)
	{cerr << endl << "员工信息文件读取错误!" << endl;
	return;
	}
	iefile.read( (char *)&estruct,sizeof(estruct) );//读取一条记录
	while ( !iefile.eof() )//一直读取到文件结束
	{
		i=strcmp(estruct.name,name_str);
		if (i==0)//找到员工信息
		{
			cout << endl << "[姓名] " << estruct.name 
				<< "    [编号] " << estruct.number
				<< "    [柜组] " << gname[estruct.gcode]
				<< "    [职务] " << sname[estruct.scode];
			break;//退出
		}
		iefile.read( (char *)&estruct,sizeof(estruct) );
	}
	iefile.close();
	if (i!=0)//未找到该名员工信息
	{cout << endl << "无此员工信息!";
	return;
	}
	ifstream isfile("salary.txt",ios::in|ios::binary);//打开工资信息文件salary.txt    
	if (!isfile){
		cerr << endl << "工资信息文件读取错误!" << endl;
		return;
	}
	isfile.read( (char *)&sstruct,sizeof(sstruct) );//读取一条记录
	while ( !isfile.eof() )//一直读到文件结束
	{
		i=strcmp(sstruct.name,name_str);
		if (i==0)//找到对应该员工的工资记录
			cout << endl << sstruct.month << "月份工资:" << sstruct.salary;
		isfile.read( (char *)&sstruct,sizeof(sstruct) );//读取工资记录
	}
	iefile.close();
}
/////////////// class salesman ///////////////
salesman::salesman(float x,float y)//售货员类的构造函数
{
	fixedSalary=x;//售货员固定工资为2000元
	getRate=y;//售货员提成比率为5%
}
void salesman::countSalary(char namestr[30],int num) {//售货员的计算工资函数
	cout << "请输入售货员" << namestr << "本月的营业额:";
	cin >> saleSum;//输入营业额
	salary=fixedSalary + saleSum * getRate;//工资=固定工资+营业额×提成比率
	//将信息写入对象中,用于recordSalary()函数将其写入文件
	number=num;
	strcpy(name,namestr);
	cout << "[售货员] " << name << "    [编号] " << number
		<< "    本月工资为:" << salary << endl;
}
/////////////// class manager ///////////////
manager::manager(float x,float y)
{
	fixedSalary=x;//经理的固定工资为4000元
	getRate=y;//经理的提成比率为5‰
}
void manager::countSalary(char namestr[30],int num)
{
	cout << "请输入经理" << namestr << "本月的营业总额:";
	cin >> saleSum;//输入销售总额
	salary=fixedSalary+saleSum * getRate; //工资=固定工资+销售总额×提成比率
	//将信息写入对象中,用于recordSalary()函数将其写入文件
	number=num;
	strcpy(name,namestr);
	cout << "[经理] " << name << "    [编号] " << number
		<< "    本月工资为:" << salary << endl;
}
/////////////// class warehouseman ///////////////
warehouseman::warehouseman(float x)
{
	fixedSalary=x;//库管的固定工资为3000元
}
void warehouseman::countSalary(char namestr[30],int num)
{
	cout << "请输入库管" << namestr << "本月的奖金额:";
	cin >> bonus;//输入奖金额
	salary=fixedSalary + bonus;//工资=固定工资+奖金
	//将信息写入对象中,用于recordSalary()函数将其写入文件
	number=num;
	strcpy(name,namestr);
	cout << "[库管] " << name << "    [编号] " << number
		<< "    本月工资为:" << salary << endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -