// PersonnelManagementSystem.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
struct Node
{
string m_Code; // 职工代码
string m_Name; // 职工姓名
unsigned short int m_Year; // 职工出生年份
string m_Sex; // 职工性别
string m_Post; // 职工职称
string m_Department; // 职工部门
unsigned int m_Wage; // 职工工资
struct Node* Next; // 链表节点的指针域
};
//------ 函数声明 ------
Node* Create(Node* Head);
void Release(Node* Head);
Node* Add(Node* Head);
bool Search(Node* Head);
Node* Search_Unique_Front(Node* Head);
void Display_List(Node* Head);
void Display_Node(Node* pNode);
Node* Modify(Node* Head);
Node* Del(Node* Head);
void Save_ByFile(Node* Head, fstream& ofile);
Node* Sort(Node* Head);
int _tmain(int argc, _TCHAR* argv[])
{
system("color b");
Node* Head=0;
Head=Create(Head);
fstream iofile;
iofile.open("salary.txt");//文件以三种方式打开。
if(!iofile)
{
cout<<"打开文件失败!"<<endl;
return -1;
}
int menu;
while(1)
{
cout<<"*****************************************************"<<endl;
cout<<"*====================菜单选顶=======================*"<<endl;
cout<<"*===================================================*"<<endl;
cout<<"* 1.注册职工 2.修改信息 3.删除信息 4.信息查询 *"<<endl;
cout<<"* 5.保存文件 6.工资排行 7.信息显示 0.退出系统 *"<<endl;
cout<<"*****************************************************"<<endl;
cout<<endl<<"请选择相应操作菜单项:";
cin>>menu;
while(cin.fail())
{
cout<<"请选择正确的菜单选项。"<<endl;
cin.clear();
fflush(stdin);
cin>>menu;
}
switch(menu)
{
case 0:
cout<<"成功退出系统!"<<endl;
return 0;
case 1:
Head=Add(Head);
break;
case 2:
Head=Modify(Head);
break;
case 3:
Head=Del(Head);
break;
case 4:
Search(Head);
break;
case 5:
Save_ByFile(Head,iofile);
break;
case 6:
Sort(Head);
break;
case 7:
Display_List(Head);
break;
default:
cout<<"请选择正确的菜单项进行操作。多谢合作!"<<endl;
}
}
Release(Head);
iofile.close();
system("pause");
return 0;
}
//------ 函数实现 ------
Node* Create(Node* Head)
{
Head = new Node;
if (!Head)
{
cout<<"分配内存失败!"<<endl;
return NULL;
}
Head->m_Code = "";
Head->m_Name = "";
Head->m_Year = 0;
Head->m_Sex = "";
Head->m_Post = "";
Head->m_Department = "";
Head->m_Wage = 0;
Head->Next = NULL;
return Head;
}
void Release(Node* Head)
{
Node* ptr;
while (Head != NULL)
{
ptr = Head;
Head = Head->Next;
delete ptr;
}
}
Node* Add(Node* Head)
{
//前插法添加数据
Node* pNew = NULL; // 声明一个新的节点
char again;
string code, name, sex, post, department;
unsigned short int year;
unsigned int wage;
do
{
pNew = new Node;
cout<<"请输入职工代码:";
cin>>code;
cout<<endl<<"请输入职工姓名:";
cin>>name;
cout<<endl<<"请输入职工出生年份(如:1990):";
cin>>year;
while (cin.fail())
{
cout<<"请输入正确的年份格式:"<<endl;
cin.clear();
fflush(stdin);
cin>>year;
}
cout<<endl<<"请输入职工性别:";
cin>>sex;
cout<<endl<<"请输入职工职称:";
cin>>post;
cout<<endl<<"请输入职工部门:";
cin>>department;
cout<<endl<<"请输入职工工资:";
cin>>wage;
while (cin.fail())
{
cout<<"请输入正确的工资格式:"<<endl;
cin.clear();
fflush(stdin);
cin>>wage;
}
cout<<endl;
pNew->m_Code = code;
pNew->m_Name = name;
pNew->m_Year = year;
pNew->m_Sex = sex;
pNew->m_Post = post;
pNew->m_Department = department;
pNew->m_Wage = wage;
pNew->Next = Head->Next;
Head->Next = pNew;
cout<<"数据添加成功!是否继续添加?(Y/N)"<<endl;
cin>>again;
} while (again == 'Y' || again == 'y');
return Head;
}
bool Search(Node* Head)
{
// 查询同时满足“姓名”和“部门”的职工信息
Node* ptr = NULL;
string name, department;
ptr = Head->Next;
cout<<"请输入部门:"<<endl;
cin>>department;
cout<<endl<<"请输入姓名:";
cin>>name;
cout<<endl<<"--------------- 查询结果 ---------------"<<endl;
while (ptr)
{
if ((ptr->m_Name == name) && (ptr->m_Department == department))
{
Display_Node(ptr);
return true;
}
ptr = ptr->Next;
}
cout<<"无此职工信息!"<<endl;
return false;
}
Node* Search_Unique_Front(Node* Head)
{
// 查询满足“职工代码”的职工信息(职工代码必需唯一)
Node* ptr;
string code;
ptr = Head;
cout<<"请输入职工代码:";
cin>>code;
cout<<endl<<"--------------- 查询结果 ---------------"<<endl;
while (ptr->Next)
{
if (ptr->Next->m_Code == code)
{
//Display_Node(ptr); // 打印
return ptr;
}
ptr->Next = ptr->Next->Next;
}
return ptr;
}
void Display_List(Node* Head)
{
Node* ptr;
ptr = Head->Next;
cout<<"=============== 所有职工信息 ==============="<<endl;
while (ptr)
{
Display_Node(ptr);
ptr = ptr->Next;
}
}
void Display_Node(Node* pNode)
{
cout<<setw(10)<<left<<pNode->m_Code
<<setw(10)<<left<<pNode->m_Name
<<setw(10)<<left<<pNode->m_Year
<<setw(10)<<left<<pNode->m_Sex
<<setw(10)<<left<<pNode->m_Post
<<setw(10)<<left<<pNode->m_Department
<<setw(10)<<left<<pNode->m_Wage<<endl;
}
Node* Modify(Node* Head)
{
Node* ptr;
ptr = Search_Unique_Front(Head);
string code, name, sex, post, department;
unsigned short int year;
unsigned int wage;
if (ptr->Next)
{
cout<<"-------你现在可以修改此职工的信息了-------"<<endl;
//数据域。
cout<<"请输入职工代码:";
cin>>code;
cout<<endl<<"请输入职工姓名:";
cin>>name;
cout<<endl<<"请输入职工出生年份:";
cin>>year;
while(cin.fail())
{
cout<<"请输入正确的年份格式。"<<endl;
cin.clear();
fflush(stdin);
cin>>year;
}
cout<<endl<<"请输入职工性别:";
cin>>sex;
cout<<endl<<"请输入职工职称:";
cin>>post;
cout<<endl<<"请输入职工部门:";
cin>>department;
cout<<endl<<"请输入职工工资:";
cin>>wage;
while(cin.fail())
{
cout<<"请输入正确的工资数据。"<<endl;
cin.clear();
fflush(stdin);
cin>>wage;
}
cout<<endl;
ptr->Next->m_Code=code;//因ptr是前趋节点,所以要用ptr->Next;
ptr->Next->m_Name=name;
ptr->Next->m_Year=year;
ptr->Next->m_Sex=sex;
ptr->Next->m_Post=post;
ptr->Next->m_Department=department;
ptr->Next->m_Wage=wage;
cout<<"修改成功"<<endl;
}
else
cout<<"没找到此职工的记录,无法修改。"<<endl;
return Head;
}
Node* Del(Node* Head)
{
Node* ptr;
char flag;
Node* ptr_front;
ptr_front = Search_Unique_Front(Head);
ptr = ptr_front->Next;
if(ptr)
{
cout<<"是否确认该职工离职?(Y/N)";
cin>>flag;
if (flag == 'Y')
{
ptr_front->Next = ptr->Next;
delete ptr;//删除此节点。
}
}
else
cout<<"没找到此职工的记录,无法删除。"<<endl;
return Head;
}
void Save_ByFile(Node* Head, fstream& ofile)
{
Node* pNode;
pNode=Head->Next;
ofile.clear();//清除文件结束状态。
while(pNode)
{
ofile<<setw(10)<<left<<pNode->m_Code
<<setw(10)<<left<<pNode->m_Name
<<setw(10)<<left<<pNode->m_Year
<<setw(10)<<left<<pNode->m_Sex
<<setw(10)<<left<<pNode->m_Post
<<setw(10)<<left<<pNode->m_Department
<<setw(10)<<left<<pNode->m_Wage<<endl;//setw(10)表示占10个字符位置。
pNode=pNode->Next; }
cout<<"数据文件保存成功!"<<endl;
}
Node* Sort(Node* Head)
{
//创建的是带头节点的链表。用直接插入法。
if((Head->Next==NULL)||(Head->Next->Next==NULL))//此步条件判断非常有价值。
{
cout<<"数据节点数少于2个,不用排序!"<<endl;
return Head;
}
//-----------第二步;
Node* ptr;
Node* ptr_F;
Node* ptr_N;
ptr=Head->Next->Next;
ptr_F=Head;
Head->Next->Next=NULL;//到此,分成了两个链表。
//第三步。
while(ptr)
{
ptr_N=ptr->Next;
ptr_F=Head;//ptr_F的归位。
while(ptr_F->Next)
{
if(ptr->m_Wage>ptr_F->Next->m_Wage)
{
ptr->Next=ptr_F->Next;
ptr_F->Next=ptr;
break;
}//if
else
{
ptr_F=ptr_F->Next;
}
}//while(ptr_F->Next)
if(ptr_F->Next==NULL)
{
ptr->Next=ptr_F->Next;
ptr_F->Next=ptr;//表示插到有序链表的最后面了。
}
ptr=ptr_N;//归位,准备下一次排序。
}//while(ptr)
cout<<"从高到低,排序成功!"<<endl;
return Head;
}