纯C++写的人事管理系统

#include <iostream> 
#include <fstrem> 
#include <iomanip> 
#include <memory.h> 
#include <stdio.h > 
#include <conio.h  > 
#include <st dlib.h  > 
using namespace std;
struct           Employee 
{//             声明职工的结构作为链表节点。 
//           -----数据域----- 
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 Employee* Next;
};
//-----个人习惯:取别名------- 
typedef struct Employee Node;
typedef Node* Link;
//-------函数声明------------- 
Link Create(Link Head); 
void Release(Link Head); 
Link Add(Link Head); 
bool Search(Link Head); 
Link Search_Unique(Link Head); 
void Display_List(Link Head); 
void Display_Node(Link pNode); 
Link Modify(Link Head); 
Link Del(Link Head); 
void Save_ByFile(Link Head,fstream& ofile); 
Link Sort(Link Head); 
//-------函数实现-------------------------- 
Link Create(Link Head) 
{
     //创建一个带头节点的空链表。 
     Head=(Link)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(Link Head) 
{
     //释放链表。
     Link ptr;//声明一个操作用的指针。 
     while(Head!=NULL) 
     { 
                       ptr=Head; 
                       Head=Head->Next; 
                       delete ptr;//释放节点资源。 
     } 

Link Add(Link Head) 
{
     //前插法添加数据。
     Link pNew;// 声明一个新节点。 
     char again; 
     string code,name,sex,post,department; 
     unsigned short int year; 
     unsigned int wage; 
     do 
     { 
              pNew=(Link)new Node;               //数据域。
              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; 
              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(Link Head) 
{//查询同时满足“姓名”和“部门”的职工信息。 
Link ptr; 
string department; 
string name; 
ptr=Head->Next; 
cout<<"请输入部门:"; 
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; 

Link Search_Unique_Front(Link Head) 
{
     //查询满足“职工代码“的职工信息(职工代码必需唯一)。 
     Link 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(Link Head) 

     Link ptr; 
     ptr=Head->Next; 
     cout<<"==================所有职工信息=================="<<endl; 
     while(ptr) 
     { 
          Display_Node(ptr); 
                ptr=ptr->Next; 
     } 

void Display_Node(Link 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;//setw(10)表示占10个字符位置。 

Link Modify(Link Head) 
{
     // 修改单一个节点。
     Link 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; 

Link Del(Link Head) 

     Link ptr; 
     char flag;
     Link 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(Link Head,fstream& ofile) 

     Link 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; 

Link Sort(Link Head) 
{
     //创建的是带头节点的链表。用直接插入法。 
     if((Head->Next==NULL)||(Head->Next->Next==NULL))//此步条件判断非常有价值。
     { 
         cout<<"数据节点数少于2个,不用排序!"<<endl; 
         return Head; 
     } 
     //-----------第二步; 
     Link ptr; 
     Link ptr_F; 
     Link 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; 

int main() 

    Link Head=0; 
    Head=Create(Head); 
    fstream iofile; 
    iofile.open("f:\\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; 

 

你可能感兴趣的:(人事管理系统)