用链表写职工管理系统

职工信息管理系统:

  1. 存在一个数据文件,用来存储职工各种信息:职工号,姓名,年龄,性别,
    邮编,部门,工资

  2. 可以注册新职工;

  3. 允许修改职工信息

  4. 允许删除职工信息;

4,按照按照姓名和部门查询职工信息;

  1. 可以按照工资多少进行排名,

  2. 可以浏览所有职工信息;

.有一个主界面,供选择和调用上述选项。

.用C++中,文件和链表实现

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;

typedef struct node
{
    int ID;
    char name[20];
    int age;
    char sex[20];
    char postcodes[20];
    char department[20];
    int salary;
    struct node *next;   // 结点指针
}t;

typedef t *PNode;     // 重命名结点指针类型

class Opt{
public:
    int Creat_list_tail(PNode h);
    int Display(PNode h);
    int Search(PNode h, char *p);
    int Del(PNode h, char *p);  
    int Change(PNode h, char *p);
    int Sort(PNode h);
    int Quit(PNode h);
}s;

PNode head_node;
int MARK;
int RUN;

//INSERT————添加通讯录好友 尾插法
int Opt::Creat_list_tail(PNode h)
{
    cout<<"#1";
    if (h == NULL)
    {
        return -1;
    }
    PNode node = new t;

    cout<<"输入职工号";
    cin>>node->ID;
    cin.ignore(100, '\n');
    cout<<"输入姓名";
    cin>>node->name;
    cin.ignore(100, '\n');
    cout<<"输入年龄";
    cin>>node->age;
    cin.ignore(100, '\n');
    cout<<"输入性别";
    cin>>node->sex;
    cin.ignore(100, '\n');
    cout<<"输入邮编";
    cin>>node->postcodes;
    cin.ignore(100, '\n');
    cout<<"输入部门";
    cin>>node->department;
    cin.ignore(100, '\n');
    cout<<"输入工资";
    cin>>node->salary;
    cin.ignore(100, '\n');

    node->next = NULL;

    PNode temp = h;
    while (temp->next)
    {
        temp = temp->next;
    }
    temp->next = node;

    return 0;
}
int Opt::Display(PNode h)
{
    if (h == NULL)
    {
        return -1;
    }
    PNode temp = h->next;  // 链表第一个结点指针
    while (temp)
    {
        cout<<"职工号";
        cout<<temp->ID<<endl;
        cout<<"姓名";
        cout<<temp->name<<endl;
        cout<<"年龄";
        cout<<temp->age<<endl;
        cout<<"性别";
        cout<<temp->sex<<endl;
        cout<<"邮编";
        cout<<temp->postcodes<<endl;
        cout<<"部门";
        cout<<temp->department<<endl;
        cout<<"工资";
        cout<<temp->salary<<endl;
        temp = temp->next;
    }
    return 0;
}

int Opt::Search(PNode h, char *p)
{
    if (h == NULL)
    {
        return -1;
    }
    PNode temp = h->next;
    int flag = 1;
    while(temp)
    {   
        if(strcmp(temp->name, p) == 0)
        {
            flag = 0;
            cout<<"输入职工号";
            cout<<temp->ID<<endl;
            cout<<"输入姓名";
            cout<<temp->name<<endl;
            cout<<"输入年龄";
            cout<<temp->age<<endl;
            cout<<"输入性别";
            cout<<temp->sex<<endl;
            cout<<"输入邮编";
            cout<<temp->postcodes<<endl;
            cout<<"输入部门";
            cout<<temp->department<<endl;
            cout<<"输入工资";
            cout<<temp->salary<<endl;
            break;
        }
        temp = temp->next;
    }
    if (flag)
    {
        printf("\t没有此联系人!\n");
    }
    return 0;
} 
int Opt::Del(PNode h, char *p)
{
    //1搜索联系人
    if (h == NULL)
    {
        return -1;
    }
    PNode temp = h->next;   

    int i;
    int name_count = 0;
    int a[20];
    while(temp)
    {
        for(i = 0; i < 21; i++)
        {
            if(*(p+i) != temp->name[i])
            {
                break;
            }
            else if( (*(p+i) == '\0') && (temp->name[i] == '\0') )
            {
                a[name_count] = temp->ID;   
                name_count++;
                break;
            }
        }
        temp = temp->next;
    }   

    //2删除联系人
    PNode front = NULL;

    if (name_count == 0)
    {
        cout<<"\t没有此联系人!"<<endl;
    }   
    else
    {
        printf("\t查找到联系人%d个\n",name_count);
        printf("\t选择联系人删除:\n");
        i = 0;
        while(i < name_count )
        {
            printf("\t\tID%d: %08d\n",i+1,a[i]);
            i++;
        }
        int choose;
        printf("\t输入要删除好友ID:");
        cin>>choose;
        cin.ignore(100, '\n');
        temp = h->next;

        if(temp->next == NULL)
        {
            free(temp);
            h->next = NULL;
        }
        else
        {   
            while(temp)
            {                       
                if(temp->ID == choose)
                {                   
                    PNode m = front->next;
                    front->next = m->next;
                    free(m);
                    m=NULL;                                                     
                } 
                front=temp;
                temp = temp->next;              
            }
        }
    }

    return 0;
} 
int Opt::Change(PNode h, char *p)
{
    //1搜索联系人
    if (h == NULL)
    {
        return -1;
    }
    PNode temp = h->next;   

    int i;
    int name_count = 0;
    int a[20];
    while(temp)
    {
        for(i = 0; i < 21; i++)
        {
            if(*(p+i) != temp->name[i])
            {
                break;
            }
            else if( (*(p+i) == '\0') && (temp->name[i] == '\0') )
            {
                a[name_count] = temp->ID;   
                name_count++;
                break;
            }
        }
        temp = temp->next;
    }
    //2修改联系人
    PNode front = NULL;

    if (name_count == 0)
    {
        cout<<"\t没有此联系人!"<<endl;
    }   
    else
    {
        printf("\t查找到联系人%d个\n",name_count);
        printf("\t选择联系人修改:\n");
        i = 0;
        while(i < name_count )
        {
            printf("\t\tID%d: %08d\n",i+1,a[i]);
            i++;
        }
        int choose;
        printf("\t输入要修改好友ID:");
        cin>>choose;
        cin.ignore(100, '\n');
        temp = h->next;
        while(temp)
        {                       
            if(temp->ID == choose)
            {                   
                cout<<"输入职工号";
                cin>>temp->ID;
                cin.ignore(100, '\n');
                cout<<"输入姓名";
                cin>>temp->name;
                cin.ignore(100, '\n');
                cout<<"输入年龄";
                cin>>temp->age;
                cin.ignore(100, '\n');
                cout<<"输入性别";
                cin>>temp->sex;
                cin.ignore(100, '\n');
                cout<<"输入邮编";
                cin>>temp->postcodes;
                cin.ignore(100, '\n');
                cout<<"输入部门";
                cin>>temp->department;
                cin.ignore(100, '\n');
                cout<<"输入工资";
                cin>>temp->salary;  
                cin.ignore(100, '\n');
                return 0;
            } 
            front=temp;
            temp = temp->next;              
        }
    }

    return 1;
}
int Opt::Sort(PNode h)
{
    if (h == NULL)
    {
        return -1;
    }
    PNode temp = h->next;
    int count = 0;
    int maxsalary = 0;
    int t = 0;
    while(temp)
    {
        count++;
        if(maxsalary < temp->salary) 
        {
            maxsalary = temp->salary;
        }
        temp = temp->next;
    }
    cout<<"总人数为"<<count<<endl;
    cout<<"最高薪为"<<maxsalary<<endl;
    int i = 0, j = 0;
    int flag = 0;
    while(count != 0)
    {
        temp = h->next;
        t = maxsalary;
        maxsalary = 0;
        while(temp)
        {
            //cout<<"#1";

            if(temp->salary == t)//打印当前薪水的人的信息
            {
                cout<<"职工号";
                cout<<temp->ID<<endl;
                cout<<"姓名";
                cout<<temp->name<<endl;
                cout<<"年龄";
                cout<<temp->age<<endl;
                cout<<"性别";
                cout<<temp->sex<<endl;
                cout<<"邮编";
                cout<<temp->postcodes<<endl;
                cout<<"部门";
                cout<<temp->department<<endl;
                cout<<"工资";
                cout<<temp->salary<<endl<<endl;
                //system("pause");
            }
            else if((maxsalary < temp->salary) && (temp->salary < t))//找到下一个的人的薪水
            {
                maxsalary = temp->salary;
                flag = 1;
            }
            temp = temp->next;
        }
        if(flag != 1)
        {
            count = 0;
        }
        flag = 0;
    }


    return 0;

}

int Opt::Quit(PNode h)
{
    PNode temp = h->next;
    int count = 0;
    fstream outfile("stud.txt",ios::out);
    if(!outfile)
    {
        cerr<<"open error!"<<endl;
        abort( );
    }
    while(temp)
    {
        outfile.write((char *)temp,sizeof(t));
        temp = temp->next;
    }    
    outfile.close( );
    RUN = 0;
    return 0;
}
void System_init()
{
    RUN = 1;
    MARK = 0;
    system("color A4");
    head_node = new t;
    head_node->next = NULL;   // 空链表

    fstream infile("stud.txt",ios::in);
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        abort( );
    }
    PNode temp = new t;
    if(infile.read((char *)temp,sizeof(t)))
    {       
        head_node->next = temp;         
    }
    else
    {
        return;
    }
    PNode end = head_node->next;
    while(1)
    {   
        PNode temp = new t;
        if(infile.read((char *)temp,sizeof(t)))
        {                   
            end->next = temp;
            end = end->next;
            end->next = NULL;
        }
        else
        {
            free(temp);
            infile.close( );
            break;
        }
    }
}

void welcome()
{

}
void Menu(int menu)
{
    switch (menu)
    {
    case 0://Main menu
        {
            cout<<"Main menu"<<endl;
            cout<<"1-------------------------注册新职工"<<endl;
            cout<<"2-------------------------修改职工信息"<<endl;
            cout<<"3-------------------------删除职工信息"<<endl;
            cout<<"4-------------------------按照按照姓名和部门查询职工信息"<<endl;
            cout<<"5-------------------------按照工资多少进行排名"<<endl;
            cout<<"6-------------------------浏览所有职工信息"<<endl;
            cout<<"9-------------------------退出程序"<<endl;
            break;
        }   
    case 1://Register~
        {
            system("cls");
            cout<<"注册新职工"<<endl;
            break;
        }
    case 2://Change
        {
            system("cls");
            cout<<"修改职工信息"<<endl;
            break;
        }
    case 3://Delete~
        {
            system("cls");
            cout<<"删除职工信息"<<endl;
            break;
        }
    case 4://Search~
        {
            system("cls");
            cout<<"按照按照姓名和部门查询职工信息"<<endl;
            break;
        }
    case 5://Sort_salary
        {
            system("cls");
            cout<<"按照工资多少进行排名"<<endl;
            break;
        }
    case 6://Display~
        {
            system("cls");
            cout<<"浏览所有职工信息"<<endl;
            break;
        }
    case 9://quit~
        {
            system("cls");
            cout<<"退出程序"<<endl;
            break;
        }
    }
}
int Choose()
{
    cout<<"请输入选择";
    char choose[10];
    cin>>choose;
    cin.ignore(100, '\n');
    //fflush(stdin);
    return (int)*choose - 48;
}
void Task(int choose)
{
    cout<<"choose = "<<choose<<endl;
    Menu(choose);
    switch(choose)
    {
    case 0:
        {
            break;
        }
    case 1:
        {   
            if(s.Creat_list_tail(head_node))
                cout<<"ERROR1"<<endl;
            system("pause");
            system("cls");
            Menu(0);
            break;
        }
    case 2:
        {
            char name[20];
            cout<<"输入姓名";
            cin>>name;
            cin.ignore(100, '\n');
            if(s.Change(head_node,name))
                cout<<"不存在该员工信息"<<endl;
            system("pause");
            system("cls");
            Menu(0);
            break;
        }
    case 3:
        {
            char name[20];
            cout<<"输入姓名";
            cin>>name;
            cin.ignore(100, '\n');
            if(s.Del(head_node,name))
                cout<<"ERROR3"<<endl;
            system("pause");
            system("cls");
            Menu(0);
            break;
        }
    case 4:
        {
            char name[20];
            cout<<"输入姓名";
            cin>>name;
            cin.ignore(100, '\n');
            if(s.Search(head_node,name))
                cout<<"ERROR4"<<endl;
            system("pause");
            system("cls");
            Menu(0);
            break;
        }
    case 5:
        {
            if(s.Sort(head_node))
                cout<<"ERROR5"<<endl;
            system("pause");
            system("cls");
            Menu(0);
            break;
        }
    case 6:
        {
            if(s.Display(head_node))
                cout<<"ERROR6"<<endl;
            system("pause");
            system("cls");
            Menu(0);
            break;
        }
    case 9:
        {
            if(s.Quit(head_node))
                cout<<"ERROR9"<<endl;
            break;
        }
    }

}
int main()
{
    System_init();
    Menu(0);
    while (RUN)
    {
        Task(Choose());

        //system ("pause");
    }
    return 0;
}
  • 5
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【员工管理系统】 问题描述:每个员工的信息包括:编号、姓名、性别、出生年月、学历、职务、电话、住址等。系统能够完成员工信息的查询、更新、插入、删除、排序等功能。 基本要求:排序:按不同关键字,对所有员工的信息进行排序;查询:按特定条件查找员工;更新,按编号对某个员工的某项信息进行修改;插入,加入新员工的信息;删除,按编号删除已离职的员工的信息。 选作内容:实现图形用户界面。 通过链表实现 数据结构: #include #include #include #include #include using namespace std; typedef struct workers{ char name[15];//姓名 char department[18];//单位 char gender;//性别 unsigned int age;//年龄 unsigned long long telephone;//电话 unsigned long wage;//工资 unsigned long num;//职工号 struct workers *next; }*Linklist,Lnode; void frist_print() { printf("\t\t⊙▽⊙ ⊙▽⊙ ⊙▽⊙ ⊙▽⊙ ⊙▽⊙ ⊙▽⊙ \n\n"); printf("\t\t\t欢迎进入员工管理系统\n"); } void menu() { printf("\n\t\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"); printf("\t\t \t ◎1.创建员工信息\t \n"); printf("\t\t \t ◎2.插入员工信息\t \n"); printf("\t\t \t ◎3.修改员工信息\t \n"); printf("\t\t \t ◎4.删除员工信息\t \n"); printf("\t\t \t ◎5.查询员工信息\t \n"); printf("\t\t \t ◎6.员工信息排序\t \n"); printf("\t\t \t ◎7.显示员工信息\t \n"); printf("\t\t \t ◎8.员工工资情况\t \n"); printf("\n\t\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"); printf("注意:输入均以回车作为结束\n"); printf("please choise 1--8:\t "); //putchar(12); } void Inset(Linklist Head){ Linklist s,L; unsigned int agee; unsigned long wagee,numm;

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值