C++实现人员工资管理系统

(1)几类人员Employee:经理Manager、技术人员Technician、销售人员Salesman,还有销售经理SalesManager。

(2)经理:固定月薪monthly_salary(8000元/月);技术人员:hourly_salary(100元/小时);推销人员:提成Rate (4%);销售经理:固定月薪5000元/月+5%提成。

(3)提供菜单,对所有人员选择字段进行查找、删除、排序等操作;

(4)用文件实现人员信息的读写。

平时作业随便写了一下,有些许小问题可能。之前刚开始学的时候感觉到处找资料很麻烦就,就想写完了传一下,大佬看到有问题或写的不好的地方轻喷,球球啦。

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<algorithm>
#include<stdio.h>
//男1女2 
using namespace std;
void find();//查找
void dele();//删除
void Sort();//排序,按工资顺序
void set();//输入数据
void menu();//菜单

class employee{//用于最后统计人员工资
public:
	string name;
	int age;
	string gender;
	int money;
	void e_set(string n,int a,string g,int m){
		name=n;age=a;gender=g;money=m;
	}
};
class Manager{
	public:
		string name;
		int age;
		string gender;
		int monthly_salary;
		Manager(){}
		Manager(string n,int a,string g,int m=8000)
		{
			name=n;age=a;gender=g;
			monthly_salary=m;
		}
		void m_set(string n,int a,string g,int m=8000)
		{
			name=n;age=a;gender=g;monthly_salary=m;
		}
		~Manager(){}
};
class Technician{
	public:
		string name;
		int age;
		string gender;
		int hourly_salary;
		Technician(){}
		Technician(string n,int a,string g,int h)
		{
			name=n;age=a;gender=g;hourly_salary=100*h;
		}
		void m_set(string n,int a,string g,int h)
		{
			name=n;age=a;gender=g;hourly_salary=100*h;
		}
		~Technician(){}
};
class Salesman{
	public:
		string name;
		int age;
		string gender;
		int rate;
		Salesman(){}
		Salesman(string n,int a,string g,int r)
		{
			name=n;age=a;gender=g;rate=r*0.04;
		}
		void S_set(string n,int a,string g,int r)
		{
			name=n;age=a;gender=g;rate=r*0.04;
		}
		~Salesman(){}
};
class SalesManager{
	public:
		string name;
		int age;
		string gender;
		double rate;
		int monthly_salary=5000;
		SalesManager(){}
		SalesManager(string n,int a,string g,int r)
		{
			name=n;age=a;gender=g;rate=r*0.04+5000;
		}
		void Sm_set(string n,int a,string g,int r)
		{
			name=n;age=a;gender=g;rate=r*0.04+5000;
		}
		~SalesManager(){}
};
void set()
{
	int k=0;
	vector<Manager>t1;
	vector<Technician>t2;
	vector<Salesman>t3;
	vector<SalesManager>t4;
    //因为各个人员之间的数据不一样,所以分开存
	while(1)
	{
		cout<<"请选择要录入的人员类型:1.经理  2.技术人员  3.销售人员  4.销售经理  5.退出"<<endl;;
		cin>>k;
		if(k==1)
		{
			Manager m;
			cout<<"请输入姓名,年龄,性别"<<endl;
			string n,g;
			int a;
			cin>>n>>a>>g;
			m.m_set(n,a,g);
			t1.push_back(m);
			cout<<"录入成功"<<endl;
		}
		else if(k==2)
		{
			Technician t;
			cout<<"请输入姓名,年龄,性别,工作时间"<<endl;
			string n,g;
			int a,h;
			cin>>n>>a>>g>>h;
			t.m_set(n,a,g,h);
			t2.push_back(t);
			cout<<"录入成功"<<endl;
		}
		else if(k==3)
		{
			Salesman S;
			cout<<"请输入姓名,年龄,性别,提成"<<endl;
			string n,g;
			int a,r;
			cin>>n>>a>>g>>r;
			S.S_set(n,a,g,r);
			t3.push_back(S);
			cout<<"录入成功"<<endl;
		}
		else if(k==4)
		{
			SalesManager S;
			cout<<"请输入姓名,年龄,性别,提成"<<endl;
			string n,g;
			int a,r;
			cin>>n>>a>>g>>r;
			S.Sm_set(n,a,g,r);
			t4.push_back(S);
			cout<<"录入成功"<<endl;
		}
		else if(k==5)
		{
			cout<<"录入完毕"<<endl;
			break;
		}
	}
	
	
	ofstream ofs;
	ofs.open("data.txt",ios::app);
	for(vector<Manager>::iterator i=t1.begin();i!=t1.end();i++)
	{
		ofs<<i->name<<' '<<i->age<<' '<<i->gender<<' '<<i->monthly_salary<<endl;
	}
	ofs.close();
	
	
	ofs.open("data.txt",ios::app);
	
	for(vector<Technician>::iterator i=t2.begin();i!=t2.end();i++)
	{
		ofs<<i->name<<' '<<i->age<<' '<<i->gender<<' '<<i->hourly_salary<<endl;
	}
	ofs.close();
	
	ofs.open("data.txt",ios::app);
	
	for(vector<Salesman>::iterator i=t3.begin();i!=t3.end();i++)
	{
		ofs<< i->name << ' ' << i->age << ' ' << i->gender << ' ' << i->rate <<endl;
	}
	ofs.close();
	
	
	ofs.open("data.txt",ios::app);
	
	for(vector<SalesManager>::iterator i=t4.begin();i!=t4.end();i++)
	{
		ofs<< i->name << ' ' << i->age << ' ' << i->gender << ' ' << i->rate <<endl;
	}
	ofs.close();
	cout<<endl;
	menu();
}
void find()
{
	int p=1;//1找到继续,2找到退出 
	int f=0;//是否找到 
	while(p==1)
	{
		cout<<"请输入需要查找的姓名"<<endl;
		string n;
		cin>>n; 
		string t,g;
		int a,m;
		ifstream i;
		i.open("data.txt",ios::in);
		while(i>>t && i>>a&&i>>g&&i>>m)
		{
			if(t==n)
			{
				cout<<"查找完成:"<<endl;
				cout<<"姓名:"<<n<<' '<<"年龄:"<<a<<' '<<g<<' '<<"收入:"<<m<<endl;
				cout<<"继续查找请按1,退出请按2"<<endl; 
				cin>>p;
				f=1;
				break; 
			}
		}
		if(f==0)
		{
			cout<<"未找到该人员信息"<<endl;
			cout<<"继续查找请按1,退出请按2"<<endl;
			cin>>p;
		}
		i.close();
	}
	cout<<endl;
	cout<<endl;
	menu();
}
bool cmp(employee a,employee b)//自定义排序方式的参数
{
	return a.money<b.money;
}
void Sort()
{
	fstream fs;
	fs.open("data.txt",ios::in);
	vector<employee>ans;
	string n,g;
	int a,m;
	while(fs>>n && fs>>a && fs>>g && fs>>m)
	{
		employee t;
		t.e_set(n,a,g,m);
		ans.push_back(t);
	}
	fs.close();
	fs.open("data.txt",ios::out|ios::trunc);
	sort(ans.begin(),ans.end(),cmp);
	for(unsigned int i=0;i<ans.size();i++)
	{
		fs<<ans[i].name<<' '<<ans[i].age<<' '<<ans[i].gender<<' '<<ans[i].money<<endl;
	}
	cout<<"排序完毕"<<endl; 
	fs.close();
	menu();
}
void dele()
{
	string N;
	cout<<"请输入要删除的人员姓名:"<<endl;
	cin>>N;
	string n,g;
	int a,m;
	fstream fs;
	vector<employee>T;
	fs.open("data.txt",ios::in);
	while(fs>>n&&fs>>a&&fs>>g&&fs>>m)
	{
		employee t;
		if(n!=N)
		{
			t.e_set(n,a,g,m);
			T.push_back(t);
		}
	}
	fs.close();
	fs.open("data.txt",ios::out|ios::trunc);
	for(unsigned int i=0;i<T.size();i++)
	{
		fs<<T[i].name<<' '<<T[i].age<<' '<<T[i].gender<<' '<<T[i].money<<endl;
	}
	fs.close();
	cout<<"删除完成"<<endl;
	cout<<endl;
	menu();
}
void print()
{
	fstream fs;
	fs.open("data.txt",ios::in);
	string n,g;
	int a,m;
	while(fs>>n&&fs>>a&&fs>>g&&fs>>m)
	{
			cout<<n<<' '<<a<<' '<<g<<' '<<m<<endl;
	}
	fs.close();
	getchar();
	cout<<endl;
	cout<<endl;
	menu();
}
void menu()
{
	cout<<"请选择需要的操作:1:查找  2:删除  3:排序  4:录入  5:显示  6:退出"<<endl;
	int k;
	
	while(1)
	{
		cin>>k;
		switch (k)
		{
			case 1:find();break;
			case 2:dele();break;
			case 3:Sort();break;
			case 4:set();break;
			case 5:print();break;
			case 6:exit(0);
		}
	}
}
int main()
{
	menu();
}

  • 8
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,一个学校的工资管理系统可以包含以下基本功能: 1. 员工信息的录入:包括员工姓名、员工编号、身份证号、岗位、薪资等信息。 2. 员工信息的查询、修改和删除功能。 3. 工资计算功能:根据员工的薪资和出勤情况计算工资。 4. 工资发放功能:将计算出的工资发放给员工,并记录发放时间和金额。 5. 工资统计功能:统计每个月的工资支出情况,包括总工资支出和每个员工的工资支出情况。 下面是一个简单的C++代码示例,实现了上述基本功能: ```c++ #include <iostream> #include <string> #include <vector> using namespace std; class Employee { public: Employee(string name, int id, string idCard, string position, double salary) : name(name), id(id), idCard(idCard), position(position), salary(salary) {} void printInfo() const { cout << "Name: " << name << endl; cout << "ID: " << id << endl; cout << "ID Card: " << idCard << endl; cout << "Position: " << position << endl; cout << "Salary: " << salary << endl; } int getId() const { return id; } double getSalary() const { return salary; } void setSalary(double salary) { this->salary = salary; } private: string name; int id; string idCard; string position; double salary; }; class SalarySystem { public: void addEmployee(Employee employee) { employees.push_back(employee); } void deleteEmployee(int id) { for (auto iter = employees.begin(); iter != employees.end(); iter++) { if (iter->getId() == id) { employees.erase(iter); break; } } } void modifyEmployee(int id) { for (auto& employee : employees) { if (employee.getId() == id) { double salary; cout << "Input new salary: "; cin >> salary; employee.setSalary(salary); } } } void displayAllEmployees() const { for (const auto& employee : employees) { employee.printInfo(); cout << endl; } } void calculateSalary() { for (auto& employee : employees) { double salary = employee.getSalary(); int attendance; cout << "Input attendance of employee " << employee.getId() << ": "; cin >> attendance; salary *= attendance / 30.0; employee.setSalary(salary); } } void paySalary() { double totalSalary = 0; for (auto& employee : employees) { totalSalary += employee.getSalary(); } cout << "Total salary: " << totalSalary << endl; } private: vector<Employee> employees; }; int main() { SalarySystem salarySystem; salarySystem.addEmployee(Employee("Alice", 1, "123456789", "Teacher", 5000)); salarySystem.addEmployee(Employee("Bob", 2, "987654321", "Student", 2000)); while (true) { cout << "Menu:" << endl; cout << "1. Add employee" << endl; cout << "2. Delete employee" << endl; cout << "3. Modify employee" << endl; cout << "4. Display all employees" << endl; cout << "5. Calculate salary" << endl; cout << "6. Pay salary" << endl; cout << "7. Exit" << endl; cout << "Input your choice: "; int choice; cin >> choice; switch (choice) { case 1: { string name, idCard, position; int id; double salary; cout << "Input name: "; cin >> name; cout << "Input ID: "; cin >> id; cout << "Input ID card: "; cin >> idCard; cout << "Input position: "; cin >> position; cout << "Input salary: "; cin >> salary; salarySystem.addEmployee(Employee(name, id, idCard, position, salary)); break; } case 2: { int id; cout << "Input ID: "; cin >> id; salarySystem.deleteEmployee(id); break; } case 3: { int id; cout << "Input ID: "; cin >> id; salarySystem.modifyEmployee(id); break; } case 4: salarySystem.displayAllEmployees(); break; case 5: salarySystem.calculateSalary(); break; case 6: salarySystem.paySalary(); break; case 7: return 0; default: cout << "Invalid choice" << endl; break; } } return 0; } ``` 以上是一个简单的学校工资管理系统的C++代码示例,您可以根据实际需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值