职工管理系统(文件保存)

  今天,做了一个通讯录的文件保存,然后拿了这个职工管理系统再熟悉一下,感觉和通讯录差不多,唯一有点不同的就是c++和Linux c对文件操作的不同了。感觉文件操作掌握的还行了,除了C语言自带的文件操作(还没有练习)。

#ifndef _CONTROL_H_
#define _CONTROL_H_
#include <iostream>
#include <string>
#include <fstream>
#include<windows.h> 

using namespace std;

typedef struct node
{
	string num;			//职工号
	string name;			//名字
	int    age;			//年龄
	string x;			//性别     f为女  m为男
	string yobi;			//邮编
	string bum;			//部门
	int    salary;		//工资
	struct node *next;
}Node;
typedef struct node * PNode;

class Control
{
public:
	void jiemian();					//界面显示
	void zhuce();						//注册职工
	void xiugai();					//修改职工信息
	void shanchu();					//删除职工信息
	void chaxun();					//查询职工信息
	void paiming();					//根据薪资排名
	void pint();						//打印所有信息
};

#endif

#include "control.h"

void Control::jiemian()
{
	system("cls"); 
	cout<<"\n\n\n";
	cout<<"\t\t\t**************************************\n";
	cout<<"\t\t\t*        欢迎来到职工管理系统        *\n";
	cout<<"\t\t\t*          1  注册新职工             *\n";
	cout<<"\t\t\t*          2 修改职工信息            *\n";
	cout<<"\t\t\t*          3 删除职工信息            *\n";
	cout<<"\t\t\t*          4 查询职工信息            *\n";
	cout<<"\t\t\t*          5 薪资排名顺序            *\n";
	cout<<"\t\t\t*          6 浏览所有信息            *\n";
	cout<<"\t\t\t**************************************\n";
	cout<<"\n\n\t\t请输入您的选择:";
}

void Control::zhuce()
{
	PNode p = new Node;
	system("cls"); 
	cout<<"\n\n\n";
	cout<<"\t\t\t请输入职工号:";
	cin>>p->num;
	cout<<"\n\t\t\t请输入名字:";
	cin>>p->name;
	cout<<"\n\t\t\t请输入年龄:";
	cin>>p->age;
	cout<<"\n\t\t\t请输入性别:";
	cin>>p->x;
	cout<<"\n\t\t\t请输入邮编:";
	cin>>p->yobi;
	cout<<"\n\t\t\t请输入部门:";
	cin>>p->bum;
	cout<<"\n\t\t\t请输入工资:";
	cin>>p->salary;

	ofstream out;
	out.open("职工档案.txt", ios::out | ios::app);
	if(!out)
	{
		cerr<<"open error!"<<endl;
		return;
	}
	out.write((char*)p, sizeof(Node));

	out.close();
}

void Control::xiugai()
{
	string str1;
	int flag = 0;

	system("cls"); 
	cout<<"\n\n\n";
	cout<<"\t\t\t请输入要修改人的职工号:";
	cin>>str1;

	ifstream in;
	in.open("职工档案.txt", ios::in | ios::_Nocreate);
	if(!in)
	{
		cerr<<"open error!"<<endl;
		return;
	}

	PNode h = new Node;
	PNode p = new Node;

	if(in.read((char*) p, sizeof(Node)))
	{
		h->next = p;
		while(1)
		{
			PNode t = new Node;

			if (in.read((char*) t, sizeof(Node)))
			{
				p->next = t;
				p = p->next;
				continue;
			}
			else
			{
				p->next = h;
				break;
			}
		}
	}
	else
	{
		cout<<"\n\n\n\n\n\n\t\t\t\t系统中暂时无人!"<<endl;
		in.close();
		return;
	}
	in.close();

	PNode temp = h->next;
	while(temp != h)
	{
		if(temp->num == str1)
		{
			flag = 1;
			cout<<"\n\t\t\t请输入修改后的名字:";
			cin>>temp->name;
			cout<<"\n\t\t\t请输入修改后的年龄:";
			cin>>temp->age;
			cout<<"\n\t\t\t请输入修改后的性别:";
			cin>>temp->x;
			cout<<"\n\t\t\t请输入修改后的邮编:";
			cin>>temp->yobi;
			cout<<"\n\t\t\t请输入修改后的部门:";
			cin>>temp->bum;
			cout<<"\n\t\t\t请输入修改后的薪资:";
			cin>>temp->salary;
			break;
		}

		temp = temp->next;
	}
	if (flag)
	{
		Sleep(2);
		system("cls");
		cout<<"\n\n\n\n\n\n\t\t\t\t修改成功!"<<endl;
	}
	else
	{
		Sleep(2);
		system("cls");
		cout<<"\n\n\n\n\n\n\t\t\t\t查无此人!"<<endl;
		return;
	}

	remove("职工档案.txt");
	ofstream out;
	out.open("职工档案.txt", ios::out | ios::app);
	if(!out)
	{
		cerr<<"open error"<<endl;
		return;
	}

	temp = h->next;
	while (temp != h)
	{
		out.write((char*)temp, sizeof(Node));
		temp = temp->next;
	}
	out.close();
}

void Control::shanchu()
{
	system("cls"); 
	string str1;
	int flag = 0;
	cout<<"\n\n\n";
	cout<<"\t\t\t请输入要删除人的职工号:";
	cin>>str1;

	ifstream in;
	in.open("职工档案.txt", ios::in | ios::_Nocreate);
	if(!in)
	{
		cerr<<"open error!"<<endl;
		return;
	}

	PNode h = new Node;
	PNode p = new Node;

	if(in.read((char*) p, sizeof(Node)))
	{
		h->next = p;
		while(1)
		{
			PNode t = new Node;

			if (in.read((char*) t, sizeof(Node)))
			{
				p->next = t;
				p = p->next;
				continue;
			}
			else
			{
				p->next = h;
				break;
			}
		}
	}
	else
	{
		cout<<"\n\n\n\n\n\n\t\t\t\t系统中暂时无人!"<<endl;
		in.close();
		return;
	}
	in.close();

	PNode temp = h;
	if(temp->next->next == h)           //一个结点的表
	{
		if(temp->next->num == str1)
		{
			PNode tmp = temp->next;
			temp->next = h;
			delete tmp;

			remove("职工档案.txt");
			ofstream outfile;
			outfile.open("职工档案.txt", ios::out);
			outfile.close();

			system("cls");
			cout<<"\n\n\n\n\n\n\t\t\t\t删除成功!"<<endl;
			return;
		}
		else
		{
			system("cls");
			cout<<"\n\n\n\n\n\n\t\t\t\t查无此人!"<<endl;
			return;
		}
	}

	while (temp->next != h)
	{
		if (temp->next->num == str1)
		{
			flag = 1;
			PNode p = temp->next;
			temp->next = p->next;
			free(p);
			break;
		}
		
		temp = temp->next;
	}

	if(flag)
	{
		system("cls");
		cout<<"\n\n\n\n\n\n\t\t\t\t删除成功!"<<endl;
	}
	else
	{
		system("cls");
		cout<<"\n\n\n\n\n\n\t\t\t\t查无此人!"<<endl;
		return;
	}

	remove("职工档案.txt");
	ofstream out;
	out.open("职工档案.txt", ios::out | ios::app);
	if(!out)
	{
		cerr<<"open error"<<endl;
		return;
	}

	temp = h->next;
	while (temp != h)
	{
		out.write((char*)temp, sizeof(Node));
		temp = temp->next;
	}
	out.close();
}

void Control::chaxun()
{
	system("cls"); 
	string str1, str2;
	int flag = 0;
	cout<<"\n\n\n";
	cout<<"\t\t\t请输入要查询的职工号:";
	cin>>str1;
	cout<<"\n\n\t\t\t请输入要查询的名字:";
	cin>>str2;

	ifstream in;
	in.open("职工档案.txt", ios::in | ios::_Nocreate);
	if(!in)
	{
		cerr<<"open error!"<<endl;
		return;
	}

	PNode h = new Node;
	PNode p = new Node;

	if(in.read((char*) p, sizeof(Node)))
	{
		h->next = p;
		while(1)
		{
			PNode t = new Node;

			if (in.read((char*) t, sizeof(Node)))
			{
				p->next = t;
				p = p->next;
				continue;
			}
			else
			{
				p->next = h;
				break;
			}
		}
	}
	else
	{
		cout<<"\n\n\n\n\n\n\t\t\t\t系统中暂时无人!"<<endl;
		in.close();
		return;
	}
	in.close();

	PNode temp = h->next;
	while (temp != h)
	{
		if(temp->num == str1 && temp->name == str2)
		{
			flag = 1;
			cout<<"\n\n年龄:"<<temp->age<<" 性别:"<<temp->x<<" 邮编:"<<temp->yobi<<" 部门:"
				<<temp->bum<<" 薪资:"<<temp->salary<<endl;
			break;
		}
		temp = temp->next;
	}

	if (flag == 0)
	{
		Sleep(2);
		system("cls");
		cout<<"\n\n\n\n\n\n\t\t\t\t查无此人!"<<endl;
	}
}

void Control::paiming()
{
	ifstream in;
	in.open("职工档案.txt", ios::in | ios::_Nocreate);
	if(!in)
	{
		cerr<<"open error!"<<endl;
		return;
	}

	PNode h = new Node;
	PNode p = new Node;

	if(in.read((char*) p, sizeof(Node)))
	{
		h->next = p;
		while(1)
		{
			PNode t = new Node;

			if (in.read((char*) t, sizeof(Node)))
			{
				p->next = t;
				p = p->next;
				continue;
			}
			else
			{
				p->next = h;
				break;
			}
		}
	}
	else
	{
		cout<<"\n\n\n\n\n\n\t\t\t\t系统中暂时无人!"<<endl;
		in.close();
		return;
	}
	in.close();

	PNode temp, max, n, m;
	int i = 0;
	n = h;
	m = max = h->next;
	while (n->next != h)			//大循环,控制循环次数//
	{
		temp = h->next;			//temp为下面找最大值的前节点所用,赋初值//
		for (m = n->next; m != h; m = m->next)	//小循环,依次找出当前最大值//
		{
			if (max->salary > m->salary)
			{
				max = m;
			}
		}
		while(temp->next != max)
		{
			temp = temp->next;
		}
		temp->next = max->next;
		max->next = h->next;
		h->next = max;
		if(i == 0)
		{
			n = max;
		}
		max = n->next;
		i++;
	}

	remove("职工档案.txt");
	ofstream out;
	out.open("职工档案.txt", ios::out | ios::app);
	if(!out)
	{
		cerr<<"open error"<<endl;
		return;
	}

	temp = h->next;
	while (temp != h)
	{
		out.write((char*)temp, sizeof(Node));
		temp = temp->next;
	}
	out.close();

	pint();
}

void Control::pint()
{
	system("cls"); 
	cout<<"\n\n\n";
	ifstream in;
	in.open("职工档案.txt", ios::in | ios::_Nocreate);
	if(!in)
	{
		cerr<<"open error!"<<endl;
		return;
	}

	PNode h = new Node;
	PNode p = new Node;

	if(in.read((char*) p, sizeof(Node)))
	{
		h->next = p;
		while(1)
		{
			PNode t = new Node;

			if (in.read((char*) t, sizeof(Node)))
			{
				p->next = t;
				p = p->next;
				continue;
			}
			else
			{
				p->next = h;
				break;
			}
		}
	}
	else
	{
		cout<<"\n\n\n\n\n\n\t\t\t\t系统中暂时无人!"<<endl;
		in.close();
		return;
	}

	PNode temp = h->next;
	while (temp != h)
	{
		cout<<"职工号:"<<temp->num<<" 名字:"<<temp->name<<" 年龄:"<<temp->age<<" 性别:"<<temp->x
			<<" 邮编:"<<temp->yobi<<" 部门:"<<temp->bum<<" 薪资:"<<temp->salary<<endl;
		temp = temp->next;
	}
	in.close();
}

#include "control.h"

int main()
{ 
	Control con;

	char ecf;   //选项
	char sel[10];

	while(1)
	{
		con.jiemian();
		cin>>ecf;

		switch (ecf)
		{
			case '1':				//注册新职工
			{
				con.zhuce();
				break;
			}
			case '2':				//修改职工信息
			{
				con.xiugai();
				cout<<"\n\n\t\t输入任意键返回:";
				cin>>sel;
				break;
			}
			case '3':				//删除职工信息
			{
				con.shanchu();
				cout<<"\n\n\t\t输入任意键返回:";
				cin>>sel;
				break;
			}
			case '4':				//查询职工信息
			{
				con.chaxun();
				cout<<"\n\n\t\t输入任意键返回:";
				cin>>sel;
				break;
			}
			case '5':				//薪资排名顺序
			{
				con.paiming();
				cout<<"\n\n\t\t输入任意键返回:";
				cin>>sel;
				break;
			}
			case '6':				//浏览所有信息
			{
				con.pint();
				cout<<"\n\n\t\t输入任意键返回:";
				cin>>sel;
				break;
			}
			default:
			{
				system("cls");
				cout<<"\n\n\n\n\n请输入1--6!"<<endl;
				cout<<"\n\n\t\t输入任意键返回:";
				cin>>sel;
				break;
			}
		}
	}
	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值