C++职工管理系统

#include<iostream>
#include<string>
#include<cstring>
#include<stdlib.h>
#include<conio.h>
#include<fstream>
#include<Windows.h>
using namespace std;
class Employee//定义职工的基类
{
protected:
	int id;//工号
	string name;//姓名
	string sex;//性别
	string degree;//等级
	Employee* next;
public:
	virtual int getid();//返回id
	virtual void setid(int id);//给id赋值
	virtual string getname();//返回name
	virtual void setname(string name);//给name赋值
	virtual string getsex();//返回性别
	virtual void setsex(string sex);//给性别赋值
	virtual string getdegree();//返回职位
	virtual void setdegree(string degree);
	Employee* &getnext();//返回next
	virtual void get() {}
};

int Employee::getid()
{
	return id;
}

void Employee::setid(int id)
{
	this->id = id;
}
string Employee::getname()
{
	return name;
}
void Employee::setname(string name)
{
	this->name = name;
}
string Employee::getsex()
{
	return sex;
}
void Employee::setsex(string sex)
{
	this->sex = sex;
}
string Employee::getdegree()
{
	return degree;
}
void Employee::setdegree(string degree)
{
	this->degree = degree;
}
Employee* &Employee::getnext()
{
	return next;
}

class Worker :public Employee//普通职工继承
{
private:
	int daytime = 9;//日常工作时间
	int basemoney = 2000;//月基本工资
public:
		virtual void get()
		{
			cout << "工号:";
			cin >> id;
			ifstream fp;
			fp.open("职工信息", ios::in);
			int sum = 0;
			Employee* head6 = new Employee;
			Employee* q = head6;
			while (!fp.eof())
			{
				sum++;
				q->getnext()=new Employee;
				q = q->getnext();
				fp >> id >> name >> sex >> degree;
				q->setid(id);
				q->setname(name);
				q->setsex(sex);
				q->setdegree(degree);
			}
			while (sum--)
			{
				q = head6;
				while (q->getnext() != NULL)
				{
					q = q->getnext(); 
					if (q->getid() == id)
					{
						cout << "该职工已存在请重新输入!" << endl;
						cout << "工号:"; cin >> id;
						sum++;
						break;
					}
				}
			}
			cout << "姓名:"; cin >> name;
			cout << "性别:";
			while (1)
			{
				cin >> sex;
				if (sex != "男" && sex != "女")
				{
					cout << "输入错误,请输入男或女:" << endl;
				}
				else
				{
					break;
				}
			}
			cout << "职位:";
			while (1)
			{
				cin >> degree;
				if (degree != "普通职工")
				{
					cout << "请输入普通职工" << endl;
				}
				else
				{
					break;
				}
			}
			ofstream fq;
			fq.open("职工信息", ios::app);
			fq << id << " " << name << " " << sex << " " << degree << endl;
		}
};
class Saleman :public Employee
{
private:
	int daytime = 7;
	int basemoney = 3000;
public:
	virtual void get()
	{
		cout << "工号:";
		cin >> id;
		ifstream fp;
		fp.open("职工信息", ios::in);
		int sum = 0;
		Employee* head = new Employee;
		Employee* q = head;
		while (!fp.eof())
		{
			sum++;
			q->getnext()=new Employee;
			q = q->getnext();
			fp >> id >> name >> sex >> degree;
			q->setid(id);
			q->setname(name);
			q->setsex(sex);
			q->setdegree(degree);
		}
		while (sum--)
		{
			q = head->getnext();
			while (q != NULL)
			{
				if (q->getid() == id)
				{
					cout << "该职工已存在请重新输入!" << endl;
					cout << "工号:"; cin >> id;
					sum++;
					break;
				}
				q = q->getnext();
			}
		}
		cout << "姓名:"; cin >> name;
		cout << "性别:";
		while (1)
		{
			cin >> sex;
			if (sex != "男" && sex != "女")
			{
				cout << "输入错误,请输入男或女:" << endl;
			}
			else
			{
				break;
			}
		}
		cout << "职位:";
		while (1)
		{
			cin >> degree;
			if (degree != "销售人员")
			{
				cout << "请输入销售人员!" << endl;
			}
			else
			{
				break;
			}
		}
		ofstream fq;
		fq.open("职工信息", ios::app);
		fq << id << " " << name << " " << sex << " " << degree << endl;
	}
};
class Manager :public Employee
{
private:
	int daytime = 6;
	int basemoney = 4000;
public:
	virtual void get()
	{
		cout << "工号:";
		cin >> id;
		ifstream fp;
		fp.open("职工信息", ios::in);
		int sum = 0;
		Employee* head = new Employee;
		Employee* q = head;
		while (!fp.eof())
		{
			sum++;
			q->getnext()=new Employee;
			q = q->getnext();
			fp >> id >> name >> sex >> degree;
			q->setid(id);
			q->setname(name);
			q->setsex(sex);
			q->setdegree(degree);
		}
		fp.close();
		while (sum--)
		{
			q = head;
			while (q != NULL)
			{
				if (q->getnext()->getid() == id)
				{
					cout << "该职工已存在请重新输入!" << endl;
					cout << "工号:"; cin >> id;
					sum++;
					break;
				}
				q = q->getnext();
			}
		}
		cout << "姓名:"; cin >> name;
		cout << "性别:";
		while (1)
		{
			cin >> sex;
			if (sex != "男" && sex != "女")
			{
				cout << "输入错误,请输入男或女:" << endl;
			}
			else
			{
				break;
			}
		}
		cout << "职位:";
		while (1)
		{
			cin >> degree;
			if (degree != "经理")
			{
				cout << "请输入经理!" << endl;
			}
			else
			{
				break;
			}
		}
		ofstream fq;
		fq.open("职工信息", ios::app);
		fq << id << " " << name << " " << sex << " " << degree << endl;
	}
};
void Get(Employee* base)
{
	base->get();
}
class Function :public Employee
{
public:
	Employee* head = new Employee;
public:
	void add();//增
	void change();//改
	void Delete();//删
	void find(Employee* head);//查
	void show();//浏览信息
	void readfile();//读取文件内的数据
	void payoff(Employee* head);//查询工资
	void sort();//按照工号排序
	void quit();//退出
	void Menu();
};
void Function::add()
{
	Worker worker;
	Saleman sale;
	Manager manager;
	cout << "请输入要增加的员工类别:" << endl;
	cout << "1.普通职工 2.销售人员 3.经理" << endl;
	int a;
	while (1)
	{
		cin >> a;
		if (a<0|| a >3 )
		{
			cout << "输入错误请重新输入" << endl;
		}
		else
		{
			break;
		}
	}
	switch(a)
	{
	case 1:
		Get(&worker);
		break;
	case 2:
		Get(&sale);
		break;
	case 3:
		Get(&manager);
		break;
	}
}
void Function::change()
{
	Employee* head2 = new Employee;
	Employee* q = head2;
	ifstream fp;
	fp.open("职工信息", ios::in);
	while (fp >> id >> name >> sex >> degree)
	{
		q->getnext() = new Employee;
		q = q->getnext();
		q->setid(id);
		q->setname(name);
		q->setsex(sex);
		q->setdegree(degree);
	}
	fp.close();
	q = head2;
	int a;
	cout << "请输入要改动的职工工号:";
	cin >> a;
	q = q->getnext();
	while (q != NULL)
	{
		if (q->getid() == a)
		{
			cout << "1.职工工号" << endl;
			cout << "2.职工姓名" << endl;
			cout << "3.职工性别" << endl;
			cout << "4.职工等级" << endl;
			cout << "请选择要改动的数据:" << endl;
			int b;
			cin >> b;
			switch (b)
			{
			case 1:
			{
				cout << "请输入新的工号:" << endl;
				cin >> id;
				q->setid(id);
				cout << "改动成功!" << endl;
				Employee* p = head2;
				ofstream fp;
				fp.open("职工信息", ios::out);
				while (p->getnext() != NULL)
				{
					p= p->getnext();
					fp << p->getid() << " " << p->getname() << " " << p->getsex() << " " << p->getdegree() << endl;
				}
				return;
			}
			break;
			case 2:
			{
				cout << "请输入新的姓名" << endl;
				cin >>name;
				q->setname(name);
				cout << "改动成功!" << endl;
				Employee* q1 = head2;
				ofstream fp1;
				fp1.open("职工信息", ios::out);
				while (q1->getnext() != NULL)
				{
					q1 = q1->getnext();
					fp1 << q1->getid() << " " << q1->getname() << " " << q1->getsex() << " " << q1->getdegree() << endl;
				}
				return;
			}
			break;
			case 3:
			{
				cout << "请输入新的性别" << endl;
				cin >>sex;
				q->setsex(sex);
				cout << "改动成功!" << endl;
				Employee* q2 = head2;
				ofstream fp2;
				fp2.open("职工信息", ios::out);
				while (q2->getnext() != NULL)
				{
					q2 = q2->getnext();
					fp2 << q2->getid() << " " << q2->getname() << " " << q2->getsex() << " " << q2->getdegree() << endl;
				}
				return;
			}
			break;
			case 4:
			{
				cout << "请输入新的等级" << endl;
				cout << "普通职工  或者  销售人员   或者 经理" << endl;
				cin >> degree;
				q->setdegree(degree);
				cout << "改动成功!" << endl;
				Employee* q3 = head2;
				ofstream fp3;
				fp3.open("职工信息", ios::out);
				while (q3 ->getnext()!= NULL)
				{
					q3 = q3->getnext();
					fp3 << q3->getid() << " " << q3->getname() << " " << q3->getsex() << " " << q3->getdegree() << endl;
				}
				break;
			}
			return;
			}
		}
		q= q->getnext();
	}
}
void Function::Delete()
{
	Employee* head1 = new Employee;
	Employee* q = head1;
	ifstream fp;
	fp.open("职工信息", ios::in);
	while (!fp.eof())
	{
		q ->getnext()= new Employee;
		q = q->getnext();
		fp >> id >> name >> sex >> degree;
		q->setid(id);
		q->setname(name);
		q->setsex(sex);
		q->setdegree(degree);
	}
	fp.close();
	Employee* p = head1;
	int a;
	cout << "请输入要删除的职工工号:";
	cin >> a;
	int flag = 1;
	while (p->getnext() != NULL)
	{
		if (p->getnext()->getid() == a)
		{
			p->getnext() = p->getnext()->getnext();
			flag = 0;
			cout << "删除成功\n";
			break;
		}
		p = p->getnext();
	}
	if (flag == 1)
	{
		cout << "未找到该职工删除失败\n";
		return;
	}
	if (flag == 0)
	{
		ofstream fq;
		fq.open("职工信息",ios::out);
		p = head1->getnext();
		while (p->getnext() != NULL)
		{
			fq << p->getid() << " " << p->getname() << " " << p->getsex() << " " << p->getdegree() << endl;
			p = p->getnext();
		}
		fq.close();
	}
}

void Function::find(Employee* head)
{
	ifstream fp;
	fp.open("职工信息", ios::in);
	Employee* q = head;
	while (!fp.eof())
	{
		q->getnext() = new Employee;
		q = q->getnext();
		fp >> id >> name >> sex >> degree;
		q->setid(id);
		q->setname(name);
		q->setsex(sex);
		q->setdegree(degree);
	}
	fp.close();
	q = head->getnext();
	cout << "1.按工号查询   2.按姓名查询" << endl;
	cout << "请输入要查询的方式:";
	int a;
	cin >> a;
	if (a == 1)
	{
		cout << "请输入要查询的工号:";
		int b;
		cin >> b;
		while (q != NULL)
		{
			if (q->getid() == b)
			{
				cout << "工号:" << q->getid() << "  姓名:" << q->getname() << "  性别:" << q->getsex() << "  职位:" << q->getdegree() << endl;
				return;
			}
			q = q->getnext();
		}
	}
	if (a == 2)
	{
		cout << "请输入要查询的姓名:";
		int b;
		cin >> b;
		while (q != NULL)
		{
			if (q->getid() == b)
			{
				cout << "工号:" << q->getid() << "  姓名:" << q->getname() << "  性别:" << q->getsex() << "  职位:" << q->getdegree() << endl;
				return;
			}
			q = q->getnext();
		}
	}
	cout << "查无此人!" << endl;
}

void Function::readfile()
{
	ifstream fp;
	fp.open("职工信息", ios::in);
	Employee* head = new Employee;
	Employee* q = head;
	while (!fp.eof())
	{
		q->getnext()=new Employee;
		q = q->getnext();
		fp >> id >> name >> sex >> degree;
		q->setid(id);
		q->setname(name);
		q->setsex(sex);
		q->setdegree(degree);
	}
	fp.close();
}

void Function::payoff(Employee* head)
{
	ifstream fp;
	fp.open("职工信息", ios::in);
	Employee* q = head;
	while (!fp.eof())
	{
		q->getnext() = new Employee;
		q = q->getnext();
		fp >> id >> name >> sex >> degree;
		q->setid(id);
		q->setname(name);
		q->setsex(sex);
		q->setdegree(degree);
	}
	fp.close();
	q = head->getnext();
	int a;
	cout << "请输入要查询的职工工号:";
	cin >> a;
	while (q != NULL)
	{
		if (q->getid() == a)
		{
			cout << "职工工号:" << q->getid() << "\t职工姓名:" << q->getname() << "\t职工性别:" << q->getsex() << "\t职工等级:" << q->getdegree() << endl;
			cout << "请输入工作的月数:";
			int b;
			cin >> b;
			if (q->getdegree() == "普通职工")
			{
				cout << "工资为:" << 2000 * b << "元" << endl;
			}
			else if (q->getdegree() == "销售人员")
			{
				cout << "工资为:" << 3000 * b << "元" << endl;
			}
			else
			{
				cout << "工资为:" << 4000 * b << "元" << endl;
			}
			return;
		}
		q = q->getnext();
	}
	cout << "查无此人!" << endl;
}

void Function::sort()
{
	ifstream fp;
	fp.open("职工信息", ios::in);
	Employee* head4 = new Employee;
	Employee* q = head4;
	while (fp >> id >> name >> sex >> degree)
	{
		q->getnext() = new Employee;
		q = q->getnext();
		q->setid(id);
		q->setname(name);
		q->setsex(sex);
		q->setdegree(degree);
	}
	fp.close();
	Employee* f, * p, * x, * y;
	f = NULL;
	while (f != head4->getnext()->getnext())//外层判断少一次循环 
	{
		for (p = head4; p->getnext()->getnext() != f; p = p->getnext())
		{
			if (p->getnext()->getid() > p->getnext()->getnext()->getid())
			{
				x = p->getnext();//x和y首先指向这两个要交换的节点 
				y = p->getnext()->getnext();
				p->getnext() = y;//交换三部曲 
				x->getnext() = y->getnext();
				y->getnext() = x;
			}
		}
		f = p->getnext();//把 f 往前移 
	}
	p = head4;
	while (p->getnext() != NULL)
	{
		p = p->getnext();
		cout << "职工工号:" << p->getid() << "\t职工姓名:" << p->getname() << "\t职工性别:" << p->getsex() << "\t职工等级:" << p->getdegree() << endl;
	}
}
void Function::show()
{
	ifstream fp;
	fp.open("职工信息", ios::in);
	Employee* head3 = new Employee;
	Employee* q = head3;
	while (fp >> id >> name >> sex >> degree)
	{
		q->getnext() = new Employee;
		q = q->getnext();
		q->setid(id);
		q->setname(name);
		q->setsex(sex);
		q->setdegree(degree);
	}
	fp.close();
	q = head3->getnext();
	while (q!= NULL)
	{
		cout << "工号:" << q->getid() << "  姓名:" << q->getname() << "  性别:" << q->getsex() << "  职位:" << q->getdegree() << endl;
		q = q->getnext();
	}
}
void Function::quit()
{
	int a = 3;
	cout << " 秒后关闭程序";
	while (--a >= 1)
	{
		printf("\r%d", a);
		printf("\b");
		Sleep(1000);
	}
	cout << "感谢您的使用!再见\n";
	exit(0);
}
void Function::Menu()
{
	int a;
	while (1)
	{
		cout << "\n";
		cout << "                         欢迎使用公司职工信息管理软件\n" << 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 << "                         ----------------------------" << endl;
		cout << "\n\n请选择:";
		while (1)
		{
			cin >> a;
			if (a < 1 || a > 8)
			{
				cout << "输入错误请重新输入" << endl;
			}
			else
			{
				break;
			}
		}
		switch (a)
		{
		case 1:
			add();
			system("pause");
			system("cls");
			break;
		case 2:
			change();
			system("pause");
			system("cls");
			break;
		case 3:
			Delete();
			system("pause");
			system("cls");
			break;
		case 4:
			find(head);
			system("pause");
			system("cls");
			break;
		case 5:
			show();
			system("pause");
			system("cls");
			break;
		case 6:
			payoff(head);
			system("pause");
			system("cls");
			break;
		case 7:
			sort();
			system("pause");
			system("cls");
			break;
		case 8:
			quit();
			break;
		default:
			printf("输入无效!");
			system("pause");
			system("cls");
			break;
		}
	}
}
class administrator //管理员类
{
protected:
	string ID;
	string password;
	administrator* unext;
public:
	string getID();
	void setID(string ID);
	string getpassword();
	void setpassword(string ID);
	administrator*& getunext();
};

class Function1 :public administrator
{
public:
	void denglu();//登录
	void Register();//注册
	void Menu1();
};
string administrator::getID()
{
	return ID;
}

void administrator::setID(string ID)
{
	this->ID = ID;
}

string administrator::getpassword()
{
	return password;
}

void administrator::setpassword(string password)
{
	this->password = password;
}
administrator*& administrator::getunext()
{
	return unext;
}
void Function1::Menu1()
{
	while (1)
	{
		cout << "**************欢迎来到职工管理系统***************\n";
		cout << "**************     1.登录         ***************\n";
		cout << "**************     2.注册         ***************\n";
		cout << "**************     3.退出         ***************\n";
		cout << "*************************************************\n";
		cout << "\n\n请选择:";
		int a;
		cin >> a;
		while (1)
		{
			if (a != 1 && a != 2 && a != 3)
			{
				cout << "输入错误!请重新输入:" << endl;
				cin >> a;
			}
			else
			{
				break;
			}
		}
		Function1 f;
		if (a == 1)
		{
			f.denglu();
		}
		if (a == 2)
		{
			f.Register();
			system("pause");
			system("cls");
		}
		if (a == 3)
		{
			int b = 3;
			printf(" 秒后关闭程序");
			while (--b >= 1)
			{
				printf("\r%d", b);
				printf("\b");
				Sleep(1000);
			}
			printf("感谢您的使用!再见\n");
			exit(0);
		}
	}
}

void Function1::denglu()
{
	ifstream fp;
	fp.open("管理员信息", ios::in);
	administrator* uhead = new administrator;
	administrator* p = uhead;
	char ch;
	ch = fp.get();
	if (ch == EOF)
	{
		printf("无信息!");
		system("pause");
		return;
	}
	fp.seekg(0);
	while (fp >> ID >> password)
	{
		p->getunext() = new administrator;
		p = p->getunext();
		p->setID(ID);
		p->setpassword(password);
	}
	p= uhead->getunext();
	cout << "请输入账号:" << endl;
	cin >> ID;
	cout << "请输入密码:" << endl;
	char a[20];
	char c;
	int i=0;
	while ((c = _getch()) != '\r')         /*输入密码以及回删*/
	{
		if (c != '\b')
		{
			printf("*");
			a[i] = c;
			i++;
		}
		else if (i != 0)
		{
			printf("\b \b");
			i--;
		}
	}
	a[i] = '\0';
	p = uhead->getunext();
	while (p != NULL)
	{
		if (p->getID() == ID && p->getpassword() == a)
		{
			cout << "\n登录成功!" << endl;
			system("pause");
			system("cls");
			Function ff;
			ff.Menu();
		}
		p = p->getunext();
	}
	if (p == NULL)
	{
		cout << "\n登录失败!" << endl;
		system("pause");
		system("cls");
		return;
	}
	fp.close();
}

void Function1::Register()
{
	cout << "请输入账号:";
	cin >> ID;
	cout << "请输入密码:";
	cin >> password;
	ofstream fp;
	fp.open("管理员信息", ios::app);
	fp << ID << " " << password << endl;
	cout << "注册成功!" << endl;
}
int main()
{
	system("color f1");
	Function1 f1;
	f1.Menu1();
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值