首页 > 技术文章 > 数据结构大作业之员工信息管理系统

ynly 2021-01-13 16:45 原文

综合应用算法训练:员工管理系统

一、 简要概述

      [问题描述]

          每个员工的信息包括:编号、姓名、性别、出生年月、学历、职务、电话、住址等。

     [基本要求]

          根据实验内容编程,上机调试、得出正确的运行程序。系统能够完成员工信息的查询、更新、插入、删除、排序功能。写出实验报告(包括源程序和运行结果)。

     [实现提示]

         (1)建立一个带头结点的单向链表(无序)。

   (2)对单链表进行插入,删除,更新操作。

   (3)在主函数中设计一个简单的菜单,分别调试上述算法。编制一个演示单链表插入、删除、查找等操作的程序

二、需求分析

    员工管理系统:包括员工的编号、姓名、性别、出生年月、学历、职务、电话、住址等信息,此系统没有连数据库,当想进行一些操作时必须先得创建员工信息,需要先把一些员工的信息导入系统中,系统才会存储一些信息。

   当在系统中存储了一些信息后,就可以进行员工信息的查询、更新、插入、删除、排序功能,查询是根据员工的编号进行查询,当输入需要查询员工的编号后,就可以查询到有关于该员工的所有信息。

   删除信息:也是输入所需要删除员工信息的编号,就可以将此员工所有的信息删除掉,在通过更新则会显示删除该员工后还剩下的其他员工的信息。

   插入信息:首先判断需要插入员工的位置,在链表中很容易的确定所有员工的位置,输入所插员工的位置,紧接着输入所插员工的所有信息。再更新后,所插员工信息就在你输入的序号中。

   排序功能是将所有的员工信息通过序号的大小进行排序,待系统更新后,所有员工的信息都按照员工的编号由小到大进行排序,也会生成所有员工的信息。

 

三、  概要设计

 1 typedef struct LNode
 2 {
 3     int number;
 4     string name;
 5     string sex;
 6     string birthday;
 7     string education;
 8     string job;
 9     string phone;
10     string address;
11     struct LNode* next;
12 }LNode, * LinkList;//链表存储结构
13 void show()//显示菜单
14 void InitList_L(LinkList& L)//创建单链表
15 void GetElem_L(LinkList L)//查询员工信息
16 void gx(LinkList L)//更新员工信息
17 void ListInsert_L(LinkList& L)//插入员工
18 void ListDelede_L(LinkList& L)//删除员工信息
19 void px(LinkList L)//对员工的编号进行排序
20 int main()//主函数
21 typedef struct sz
22 {
23     int number;
24     string name;
25     string sex;
26     string birthday;
27     string education;
28     string job;
29     string phone;
30     string address;
31 };//接受链表信息的数组

 

四、 详细设计

 

1 int number;
2     string name;
3     string sex;
4     string birthday;
5     string education;
6     string job;
7     string phone;
8     string address;

       

 

 

     

 

 1 status InitList_L(LinkList& L)//创建单链表
 2 {
 3 L = new LNode;
 4     L->next = NULL;
 5 Return ok:
 6 }
 7 status GetElem_L(LinkList L,int I,ElemType &e)//查询
 8 {
 9 p = L->next;
10 while (p &&j<i)
11          p = p->next;
12     if (!p || j>i)
13 return ERROR;
14 e=p->data;
15 return OK;
16 }
17 status ListInsert_L(LinkList& L,int I,ElemType e)//插入
18 {
19 p = L;j=0;
20 while (p && j < i - 1)
21     {
22          p = p->next;
23          j++;
24     }
25     if (!p || j > i - 1)  return ERROR:
26 s = new LNode;
27 s->next = p->next;
28     p->next = s;
29 return OK:
30 }
31 void ListDelede_L(LinkList& L,int I, ElemType &e)//删除
32 {
33 P=L;j=0;
34 while ((p->next) && (j < i - 1))
35     {
36          p = p->next;
37          j++;
38     }
39     if (!(p->next) || j > i - 1)  return ERROR;
40 q = p->next;
41     p->next = q->next;
42     delete q;
43 return OK:
44 }

 

 

五、 调试分析

      Step 1:首先需要选择1.创建员工信息

       Step 2:其次再输入创建员工的信息个数3

       Step 3:输入必须严格按照规则输入,如下:

      

 

 

      当选择2:查询员工信息,输入想要查询员工的编号003

       

 

      选择3更新员工信息,就可以看到最后插入编号为3的员工在第二个位置上,最初在第二个位置上的员工依次向后排一位

        

 

 

      当选择4时,插入员工的信息,选择想将新员工插入的位置,此时我插入的位置是2

        

 

 

      当选择5时删除员工信息,请输入删除员工的位置,此时输入2时,更新之后则显示

        

 

 

六、 程序代码

  1 # include <iostream>
  2 #include<string>
  3 using namespace std;
  4 typedef struct LNode
  5 {
  6     int number;
  7     string name;
  8     string sex;
  9     string birthday;
 10     string education;
 11     string job;
 12     string phone;
 13     string address;
 14     struct LNode* next;
 15 }LNode, * LinkList;
 16 void show()
 17 {
 18     cout << "**********************************" << endl;
 19     cout << "*********1.创建员工信息***********" << endl;
 20     cout << "*********2.查询员工信息***********" << endl;
 21     cout << "*********3.更新员工信息***********" << endl;
 22     cout << "*********4.插入员工信息***********" << endl;
 23     cout << "*********5.删除员工信息***********" << endl;
 24     cout << "*********6.排序员工信息***********" << endl;
 25     cout << "*********7.退出程序***************" << endl;
 26     cout << "**********************************" << endl;
 27     cout << "请选择你要进行的操作:" << endl;
 28 }
 29 void InitList_L(LinkList& L)//创建单链表
 30 {
 31     int n;
 32     cout << "输入创建员工信息个数:";
 33     cin >> n;
 34     LinkList p, r;
 35     L = new LNode;
 36     L->next = NULL;
 37     r = L;
 38     for (int i = 0; i < n; i++)
 39     {
 40          p = new LNode;
 41          cout << "请输入员工信息.(编号,姓名,性别,出生年月,学历,职务,电话,住址)" << endl;
 42          cin >> p->number >> p->name >> p->sex >> p->birthday >> p->education >> p->job >> p->phone >> p->address;
 43          p->next = NULL;
 44          r->next = p;
 45         r = p;
 46     }
 47 }
 48 void GetElem_L(LinkList L)//查询
 49 {
 50     int n;
 51     LinkList p;
 52     p = L->next;
 53     cout << "请输入你要查询员工信息的编号:";
 54     cin >> n;
 55     while (p && p->number != n)
 56          p = p->next;
 57     if (!p || p->number != n)
 58          cout << "输入的员工编号有误。";
 59     else
 60     {
 61          cout << "您输入的员工信息如下:";
 62          cout << "编号:" << p->number << endl;
 63          cout << "姓名:" << p->name << endl;
 64          cout << "性别:" << p->sex << endl;
 65          cout << "出生年月:" << p->birthday << endl;
 66          cout << "学历:" << p->education << endl;
 67          cout << "职务:" << p->job << endl;
 68          cout << "电话:" << p->phone << endl;
 69          cout << "住址:" << p->address << endl;
 70     }
 71  
 72 }
 73 void gx(LinkList L)//更新
 74 {
 75     LinkList p;
 76     p = L->next;
 77     cout << "员工信息如下:" << endl;
 78     while (p)
 79     {
 80          cout << endl;
 81          cout << "员工:" << p->name << "信息如下" << endl;
 82          cout << "编号:" << p->number << endl;
 83          cout << "性别:" << p->sex << endl;
 84          cout << "出生年月:" << p->birthday << endl;
 85          cout << "学历:" << p->education << endl;
 86          cout << "职务:" << p->job << endl;
 87          cout << "电话:" << p->phone << endl;
 88          cout << "住址:" << p->address << endl;
 89          p = p->next;
 90     }
 91 }
 92 void ListInsert_L(LinkList& L)//插入
 93 {
 94     int i;
 95     cout << "请输入添加员工位置:";
 96     cin >> i;
 97     LinkList p, s; int j = 0;
 98     p = L;
 99     while (p && j < i - 1)
100     {
101          p = p->next;
102          j++;
103     }
104     if (!p || j > i - 1)
105     {
106          cout << "添加的位置出错!" << endl;
107          return;
108     }
109     s = new LNode;
110     cout << "请输入添加员工信息.(编号,姓名,性别,出生年月,学历,职务,电话,住址)" << endl;
111     cin >> s->number >> s->name >> s->sex >> s->birthday >> s->education >> s->job >> s->phone >> s->address;
112     s->next = p->next;
113     p->next = s;
114  
115 }
116 void ListDelede_L(LinkList& L)//删除
117 {
118     int i;
119     cout << "请输入删除员工位置:";
120     cin >> i;
121     LinkList p, q;
122     p = L; int j = 0;
123     while ((p->next) && (j < i - 1))
124     {
125          p = p->next;
126          j++;
127     }
128     if (!(p->next) || j > i - 1)
129     {
130          cout << "删除位置出错!";
131          return;
132     }
133     q = p->next;
134     p->next = q->next;
135     delete q;
136 }
137 typedef struct sz
138 {
139     int number;
140     string name;
141     string sex;
142     string birthday;
143     string education;
144     string job;
145     string phone;
146     string address;
147 };
148 void px(LinkList L)//排序
149 {
150     LinkList p;
151     sz a[100];
152     int i = 0, j = 0, m = 0, n = 0;
153     sz tmp;
154     p = L->next;
155     while (p)
156     {
157          a[m].number = p->number;
158          a[m].name = p->name;
159          a[m].sex = p->sex;
160          a[m].birthday = p->birthday;
161          a[m].education = p->education;
162          a[m].job = p->job;
163          a[m].phone = p->phone;
164          a[m].address = p->address;
165          m++;
166          n++;
167     }
168     for (int i = 0; i < n; i++)
169     {
170          for (int j = 0; j < n - i; j++)
171          {
172              if (a[j].number > a[j + 1].number)
173              {
174                   tmp = a[j + 1];
175                   a[j + 1] = a[j];
176                   a[j] = tmp;
177              }
178          }
179     }
180  
181     cout << "*********员工信息排序结果如下:*********" << endl;
182     for (int i = 1; i < n + 1; i++)
183     {
184          cout << endl;
185          cout << "员工" << a[i].name << "信息如下." << endl;
186          cout << "编号:" << a[i].number << endl;
187          cout << "姓名:" << a[i].name << endl;
188          cout << "性别:" << a[i].sex << endl;
189          cout << "出生年月:" << a[i].birthday << endl;
190          cout << "学历:" << a[i].education << endl;
191          cout << "职务:" << a[i].job << endl;
192          cout << "电话:" << a[i].phone << endl;
193          cout << "住址:" << a[i].address << endl;
194     }
195 }
196 int main()
197 {
198     LinkList L = NULL;
199     int m = 0;
200     while (m != 7)
201     {
202          show();
203          cin >> m;
204          switch (m)
205          {
206          case 1:InitList_L(L); break;
207          case 2:GetElem_L(L); break;
208          case 3:gx(L); break;
209          case 4:ListInsert_L(L); break;
210          case 5:ListDelede_L(L); break;
211          case 6:px(L); break;
212  
213          }
214     }
215 }
源代码

 

七、 实验总结

 

     通过本次学习,我明白了链表的创建,熟悉了链表的结构和编程方法,学会了增删改查各个算法的编译,尤其是更新函数,更新函数是在一些插入删除操作之后显示链表内部的结构,对于排序,只是将编码进行排序,随后的一些信息自然而然地就能跟着排序出来。通过本次的实习,我对编程有了更深的理解,对于各种算法也更加熟练,在老师的指导和帮助下,顺利完成了本次实习,受益匪浅。

 

推荐阅读