小编找到了一个特别好的职工管理系统,分享给大家一份C语言职工管理系统的具体实现代码,供大家参考,也谢谢这位网友的分享,具体内容如下
person.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
#include "person.h"
//首页
int Print_Head( int * num)
{
printf ( "\t***********************************************\n" );
printf ( "\t| 职工管理系统 |\n" );
printf ( "\t*---------------------------------------------*\n" );
printf ( "\t| 【1】新增 【2】删改 |\n" );
printf ( "\t* 【3】查询 【4】退出 *\n" );
printf ( "\t***********************************************\n" );
printf ( "\t◎请输入想进行操作的序号【 】\b\b" );
*num= getchar ();
return SUCCESS;
}
//删改画面
int Print_DelRev( int * num)
{
printf ( "\t******************************************\n" );
printf ( "\t* *\n" );
printf ( "\t* 【1】删除 【2】修改 【3】返回 *\n" );
printf ( "\t* *\n" );
printf ( "\t******************************************\n" );
printf ( "\t◎请输入想进行操作的序号【 】\b\b" );
*num= getchar ();
return SUCCESS;
}
//查询画面
int Print_Que( int * num)
{
printf ( "\t******* ************************************\n" );
printf ( "\t* *\n" );
printf ( "\t* 【1】全部查询 【2】工号查询 *\n" );
printf ( "\t* *\n" );
printf ( "\t* 【3】模糊查询 【4】返回菜单 *\n" );
printf ( "\t* *\n" );
printf ( "\t******************************************\n" );
printf ( "\t◎请输入想进行操作的序号【 】\b\b" );
*num= getchar ();
return SUCCESS;
}
//增加画面
int Print_Add( int * num)
{
int n=0;
printf ( "\t******************************************\n" );
printf ( "\t* *\n" );
printf ( "\t* 【1】新增 【2】添加 【3】返回 *\n" );
printf ( "\t* *\n" );
printf ( "\t******************************************\n" );
printf ( "\t◎请输入想进行操作的序号【 】\b\b" );
*num= getchar ();
return SUCCESS;
}
//标题头
int Print_Tittle()
{
printf ( "-------------------------------------------\n" );
printf ( " 工号 | 姓名 |性别|年龄| 电话 \n" );
printf ( "-------------------------------------------\n" );
return SUCCESS;
}
//选择将要打开的文件
int File_name()
{
printf ( "\n\t◎请输入您想要打开的文件:" );
//memset(filename,0,sizeof(filename));
if ( scanf ( "%s" , filename)!=1){
printf ( "\a 选择文件错误!" );
return FAILED;
}
return SUCCESS;
}
//生成链表,参数n是add记录数量
struct Employee *Creat() {
printf ( "\n\t◎请输入要添加的数据个数:" );
int n;
if ( scanf ( "%d" , &n)!=1) {
printf ( "\a error!" );
}
struct Employee *head;
struct Employee *p1, *p2;
system ( "cls" );
for ( int i=1;i<n+1;i++) {
p1 = ( struct Employee*) malloc (SIZE);
Print_Tittle();
scanf ( "%s%s%s%d%s" ,p1->num,p1->name,p1->sex,
p1->age,p1->tel);
p1->next = NULL;
if (i==1) {
head = p2 = p1;
}
else {
p2->next = p1;
p2 = p1;
}
}
return (head);
}
//建立新文件
int WriteData_wb( struct Employee *head) {
FILE *fp;
struct Employee *p;
if ((fp = fopen (filename, "wb" ))==NULL)
printf ( "\a 打开文件错误!" );
p = head;
while (p!=NULL) {
if ( fwrite (p,SIZE,1,fp)!=1) {
printf ( "\b 写入数据出错\n" );
fclose (fp);
return FAILED;
}
p=p->next;
}
fclose (fp);
return SUCCESS;
}
//在已有文件添加
int WriteData_ab( struct Employee *head) {
FILE *fp;
struct Employee *p;
if ((fp = fopen (filename, "ab" ))==NULL)
printf ( "\a 打开文件错误!" );
p = head;
while (p!=NULL) {
if ( fwrite (p,SIZE,1,fp)!=1) {
printf ( "\b 写入数据出错\n" );
fclose (fp);
return FAILED;
}
p=p->next;
}
fclose (fp);
return SUCCESS;
}
//增加
int Emp_Add()
{
system ( "cls" );
getchar ();
int add;
while (1){
system ( "cls" );
Print_Add(&add);
while (add!= '1' && add!= '2' && add!= '3' ) {
putchar ( '\a' );
printf ( "◎请重新输入有效序号(1~3):【 】\b\b" );
add= getchar ();
}
switch (add){
case '1' :
WriteData_wb(Creat());
printf ( "\n◎新建文件成功数据已保存√\n" );
system ( "pause" );
system ( "cls" );
Emp_Add();
break ;
case '2' :
WriteData_ab(Creat());
printf ( "\n◎数据已成功添加√\n" );
system ( "pause" );
system ( "cls" );
Emp_Add();
break ;
case '3' :
system ( "cls" );
getchar ();
Emp_Return();
break ;
}
}
return SUCCESS;
}
//读取文件的数据到链表中返回链表head指针
struct Employee *Read() {
struct Employee *head = NULL;
struct Employee *p1, *p2; //s = p1;p = p2;
FILE *fp;
if ((fp= fopen (filename, "rb+" ))==NULL)
{
printf ( "打开文件出错\n" );
exit (0);
}
while (! feof (fp)) {
if ((p1=( struct Employee*) malloc (SIZE))==NULL){
printf ( "内存申请出错\n" );
fclose (fp);
exit (0);
}
if ( fread (p1,SIZE,1,fp)!=1){
free (p1);
break ;
}
if (head==NULL)
head=p2=p1;
else {
p2->next=p1;
p2=p1;
}
}
fclose (fp);
return (head);
}
//删除
int Emp_Del()
{
struct Employee* head;
struct Employee* pt1,*pt2;
char str_num[10];
memset (str_num,0, sizeof (str_num));
printf ( "\n◎请输入你要删除的学号信息:" );
scanf ( "%s" ,str_num);
pt1=Read();
pt2=pt1->next;
head=pt1;
while (pt2!=NULL){
if (! strcmp (pt1->num,str_num)){
WriteData_wb(pt2);
} else if (! strcmp (pt2->num,str_num)){
pt1->next=pt2->next;
WriteData_wb(head);
}
pt2=pt2->next;
pt1=pt1->next;
}
if (pt2!=NULL){
printf ( "\t◎没有你要删除的数据" );
}
return SUCCESS;
}
//修改
int Emp_Rev()
{
struct Employee* pt1,*pt2,*head;
char str_num[10];
printf ( "\t◎请输入要修改的学号信息:" );
scanf ( "%s" ,str_num);
pt1=Read();
pt2=pt1->next;
head=pt1;
while (pt2!=NULL){
if ( strcmp (pt1->num,str_num)==0) {
Print_Tittle();
scanf ( "%s%s%s%d%s" ,pt1->num,pt1->name,pt1->sex,
pt1->age,pt1->tel);
WriteData_wb(head);
}
else if ( strcmp (pt2->num,str_num)==0) {
Print_Tittle();
scanf ( "%s%s%s%d%s" ,pt1->num,pt1->name,pt1->sex,
pt1->age,pt1->tel);
WriteData_wb(head);
}
pt2 = pt2->next;
pt1 = pt1->next;
}
if (pt2!=NULL)
printf ( "数据库中没有存储您要删除的数据!\n" );
return 0;
}
//删改
int Emp_DelRev()
{
getchar ();
int delrev=0;
while (1){
system ( "cls" );
Print_DelRev(&delrev);
while (delrev!= '1' && delrev!= '2' && delrev!= '3' ){
putchar ( '\a' );
//getchar();
printf ( "○请重新输入有效序号(1~3):【 】\b\b" );
delrev= getchar ();
}
switch (delrev){
case '1' :
Emp_Del();
printf ( "\n◎已成功删除指定数据!" );
system ( "pause" );
getchar ();
break ;
case '2' :
Emp_Rev();
printf ( "\n◎已成功修改指定数据!" );
system ( "pause" );
getchar ();
break ;
case '3' :
system ( "cls" );
getchar ();
Emp_Return();
break ;
}
}
return SUCCESS;
}
//总体查询
int Emp_QueAll()
{
struct Employee *pt;
pt = Read();
Print_Tittle();
do {
printf ( "%2s%5s%4s%2d%3s\n" ,
pt->num,pt->name,pt->sex,pt->age,pt->tel);
pt = pt->next;
} while (pt!=NULL);
printf ( "\n\n" );
return SUCCESS;
}
//工号查询
int Emp_QueNum()
{
struct Employee *pt;
char str_num[10];
printf ( "\t◎请输入您要查询的学号:" );
scanf ( "%s" , str_num);
pt = Read();
Print_Tittle();
do {
if (! strcmp (pt->num,str_num)) {
printf ( "%2s%5s%4s%2d%3s\n" ,
pt->num,pt->name,pt->sex,pt->age,pt->tel);
printf ( "\n\n" );
return 0;
}
pt = pt->next;
} while (pt!=NULL);
printf ( "\t数据库中没有存储您要查询的数据!\n" );
printf ( "\n\n" );
return SUCCESS;
}
//模糊查询
int Emp_QueVague()
{
struct Employee *pt;
char str_vague[20];
int m=0;
printf ( "\t◎请输入您要查询的关键词:" );
scanf ( "%s" , str_vague);
pt = Read();
Print_Tittle();
do {
if ( strstr (pt->num,str_vague)!=0|| strstr (pt->name,str_vague)!=0
|| strstr (pt->sex,str_vague)!=0|| strstr (pt->age,str_vague)!=0
|| strstr (pt->tel,str_vague)!=0) {
printf ( "%2s%5s%4s%2d%3s\n" ,
pt->num,pt->name,pt->sex,pt->age,pt->tel);
m = 1;
}
pt = pt->next;
} while (pt!=NULL);
if (!m)
printf ( "数据库中没有存储您要查询的数据!\n" );
printf ( "\n\n" );
return SUCCESS;
}
//查询
int Emp_Que()
{
system ( "cls" );
int que;
while (1){
system ( "cls" );
Print_Que(&que);
while (que!= '1' && que!= '2' && que!= '3' ){
putchar ( '\a' );
printf ( "\t○请重新输入有效序号(1~3):【 】\b\b" );
que= getchar ();
}
switch (que){
case '1' :
Emp_QueAll();
system ( "pause" );
getchar ();
break ;
case '2' :
Emp_QueNum();
system ( "pause" );
getchar ();
break ;
case '3' :
Emp_QueVague();
system ( "pause" );
getchar ();
break ;
case '4' :
Emp_Return();
system ( "pause" );
getchar ();
break ;
}
}
return SUCCESS;
}
//返回
int Emp_Return()
{
Emp_Menu();
return SUCCESS;
}
//退出
void Emp_Quit()
{
printf ( "\n\t◎谢谢使用!" );
system ( "pause" );
exit (0);
}
//menu函数
int Emp_Menu()
{
//int* Menu=NULL;
int menu=0;
//Print_Head(&Menu);
//menu=*Menu;
//delete Menu;
//printf("%d\n",menu);
Print_Head(&menu);
while (menu!= '1' && menu!= '2' && menu!= '3' && menu!= '4' ) {
printf ( "error! please input the right number!\n" );
putchar ( '\a' );
//getchar();
printf ( "◎请重新输入有效序号(1~4):【 】\b\b" );
menu= getchar ();
}
switch (menu){
case '1' :
File_name();
Emp_Add();
break ;
case '2' :
File_name();
Emp_DelRev();
break ;
case '3' :
File_name();
Emp_Que();
break ;
case '4' :
Emp_Quit();
break ;
}
return SUCCESS;
}
/*void Sig(int n)
{
printf("\n\t◎程序将要退出,操作已保存!");
system("pause");
exit(0);
}*/
int main()
{
//printf("\t◎使用Ctrl+C终止现在程序的运行!\n");
//signal(SIGINT,Sig);
Emp_Menu();
return SUCCESS;
}
|
person.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#ifndef __PERSON_H__
#define __PERSON_H_
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <signal.h>
#define SUCCESS 0
#define FAILED (!SUCCESS)
#define SIZE sizeof(struct Employee)
//全局变量
int flag=0; //返回标志
char filename[30]; //要打开的文件名
//员工信息结构体
struct Employee{
char name[20];
char tel[15];
char sex[5];
char num[10];
char age[3];
struct Employee* next;
};
//建立一个链表
struct Employee* creat( int n);
//主函数
int Emp_Menu();
//首页打印
int Print_Head();
//删改打印
int Print_DelRev();
//增加打印
int Print_Add();
//表头打印
int Print_Tittle();
//新建文件写入
int WriteData_wb( struct Employee *head);
//在已有的文件写入
int WriteData_ab( struct Employee *head);
//添加
int Emp_Add();
//删除
int Emp_Del();
//修改
int Emp_Rev();
//查询
int Emp_Que();
//返回
int Emp_Return();
//退出
void Emp_Quit();
#endif //__PERSON_H__
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。