本文实例为大家分享了C语言职工信息管理系统的具体代码,供大家参考,具体内容如下
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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct worker
{
char ID[20]; //工号
char name[20]; //姓名
char sex[5]; //性别
char born[20]; //生日
char edu[20]; //学历
char position[20]; //职务
char wage[10]; //工资
char address[100]; //住址
char tel[15]; //电话
}work[1000];
int n ;
//文件读写模块
void read() //读入数据
{
FILE *fp;
int i = 0;
if ( (fp = fopen ( "d:\\date.txt" , "r" )) == NULL )
{
printf ( "请在D盘根目录下新建一个文本文件,文件名为date\n\n\n" );
system ( "pause" );
system ( "cls" );
return ;
}
fscanf (fp, "%d\n" ,&n);
while (i < n)
{
fscanf (fp, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" ,work[i].ID,work[i].name,work[i].sex,work[i].born,work[i].edu,work[i].position,work[i].wage,work[i].address,work[i].tel);
i ++;
}
fclose (fp);
}
void write() //写入数据
{
FILE *fp;
int i = 0;
if ( (fp = fopen ( "d:\\date.txt" , "w" )) == NULL )
{
printf ( "无法打开文件!\n" );
return ;
}
fprintf (fp, "%d\n" ,n);
while (i < n)
{
fprintf (fp, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" ,work[i].ID,work[i].name,work[i].sex,work[i].born,work[i].edu,work[i].position,work[i].wage,work[i].address,work[i].tel);
i ++;
}
fclose (fp);
}
//输入模块
void input() // 从键盘输入
{
int i;
int flag; //标记工号是否已存在
char c; //指令Y?N
read(); //输入数据前先将文件中原有的数据读入
do
{
flag = 0;
printf ( "\t\t请输入职工的工号: \n\t\t" );
scanf ( "%s" ,work[n].ID);
for (i = 0 ; i < n ; i ++)
{
if ( strcmp (work[i].ID,work[n].ID) == 0)
{
flag = 1;
break ;
}
}
if (flag)
{
printf ( "该工号已存在!\n\n" );
printf ( "\t\t是否继续输入?(Y/N): " );
scanf ( "%*c%c" ,&c);
if (c == 'N' || c == 'n' ) break ;
continue ;
}
printf ( "\t\t请输入职工的姓名: \n\t\t" );
scanf ( "%s" ,work[n].name);
printf ( "\t\t请输入职工的性别: \n\t\t" );
scanf ( "%s" ,work[n].sex);
printf ( "\t\t请输入职工的出生年月: \n\t\t" );
scanf ( "%s" ,work[n].born);
printf ( "\t\t请输入职工的学历: \n\t\t" );
scanf ( "%s" ,work[n].edu);
printf ( "\t\t请输入职工的职务: \n\t\t" );
scanf ( "%s" ,work[n].position);
printf ( "\t\t请输入职工的工资: \n\t\t" );
scanf ( "%s" ,work[n].wage);
printf ( "\t\t请输入职工的住址: \n\t\t" );
scanf ( "%s" ,work[n].address);
printf ( "\t\t请输入职工的电话: \n\t\t" );
scanf ( "%s" ,work[n].tel);
printf ( "\t\t是否继续输入?(Y/N): " );
scanf ( "%*c%c" ,&c);
n ++;
} while ( c != 'N' && c != 'n' );
write();
printf ( "\n\n数据已保存!\n\n" );
}
//显示模块
void output_inf( int id)
{
printf ( "\t\t 工号: %s\n" ,work[id].ID);
printf ( "\t\t 姓名: %s\n" ,work[id].name);
printf ( "\t\t 性别: %s\n" ,work[id].sex);
printf ( "\t\t出生年月: %s\n" ,work[id].born);
printf ( "\t\t 学历: %s\n" ,work[id].edu);
printf ( "\t\t 职务: %s\n" ,work[id].position);
printf ( "\t\t 工资: %s\n" ,work[id].wage);
printf ( "\t\t 住址: %s\n" ,work[id].address);
printf ( "\t\t 电话: %s\n" ,work[id].tel);
printf ( "\n\n\n" );
}
void show() //显示所有职工信息
{
int i;
read();
if (n == 0) printf ( "无记录!\n\n" );
else
for (i = 0 ; i < n ; i ++)
{
output_inf(i);
}
}
//查询模块
void que() //查询信息
{
char num;
int flag,i;
char q_id[20] ; //查询工号
char q_name[20] ; //查询姓名
char q_edu[20] ; //查询学历
char q_position[20] ; //查询职位
char c; //指令Y?N
read(); //打开程序后,要查询以前的数据,要先读入
while (1)
{
system ( "cls" );
printf ( "\t\t******************查询********************\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 1.按工号查询 *\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 2.按姓名查询 *\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 3.按学历查询 *\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 4.按职务查询 *\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 输入其他任意键退出本模块 *\n" );
printf ( "\t\t******************************************\n" );
printf ( "\t\t请输入指令(1-4):" );
scanf ( "%*c%c" ,&num);
if (num < '1' || num > '4' )
{
printf ( "错误指令!\n\n" );
system ( "pause" );
continue ;
}
system ( "cls" );
flag = 0; //用来标记职工信息是否存在
if (num == '1' ) //按工号查询
{
printf ( "\t\t请输入您要查询职工的工号:" );
scanf ( "%s" ,q_id);
for (i = 0 ; i < n ;i ++)
{
if ( strcmp (work[i].ID,q_id) == 0)
{
output_inf(i);
flag = 1; //标记
break ;
}
}
}
else if (num == '2' ) //按姓名查询
{
printf ( "\t\t请输入您要查询职工的姓名:" );
scanf ( "%s" ,q_name);
for (i = 0 ; i < n ;i ++)
{
if ( strcmp (work[i].name,q_name) == 0)
{
output_inf(i);
flag = 1; //标记
}
}
}
else if (num == '3' ) //按学历查询
{
printf ( "\t\t请输入您要查询职工的学历:" );
scanf ( "%s" ,q_edu);
for (i = 0 ; i < n ;i ++)
{
if ( strcmp (work[i].edu,q_edu) == 0)
{
output_inf(i);
flag = 1; //标记
}
}
}
else if (num == '4' ) //按职务查询
{
printf ( "\t\t请输入您要查询职工的职务:" );
scanf ( "%s" ,q_position);
for (i = 0 ; i < n ;i ++)
{
if ( strcmp (work[i].position,q_position) == 0)
{
output_inf(i);
flag = 1; //标记
}
}
}
if (!flag) //判断是否有该职工信息
{
printf ( "\n\n无该职工信息!\n\n" );
}
printf ( "\n\n是否继续查询(Y/N):" );
scanf ( "%*c%c" ,&c);
if (c == 'N' || c == 'n' ) break ;
}
}
//修改信息模块
void mod() //修改职工信息
{
char change_ID[20]; //需修改的职工的工号
int number; //职工信息在数据中的位置
char num; //指令
char message[100]; //修改的信息
char c; //指令Y?N
int flag;
int i;
read();
while (1)
{
flag = 0;
system ( "cls" );
printf ( "请输入需修改的职工的工号:" );
scanf ( "%s" ,change_ID);
for (i = 0 ; i < n ; i ++)
{
if ( strcmp (work[i].ID,change_ID) == 0 )
{
number = i;
flag = 1;
break ;
}
}
if (!flag)
{
printf ( "\n\n无此员工信息!!\n\n" );
printf ( "\n\n是否继续修改(Y/N):" );
scanf ( "%*c%c" ,&c);
if (c == 'N' || c == 'n' ) break ;
continue ;
}
printf ( "\n\n\t\t******************修改********************\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 1.修改姓名 *\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 2.修改学历 *\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 3.修改职务 *\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 4.修改工资 *\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 5.修改住址 *\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 6.修改电话 *\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 输入其他任意键退出本模块 *\n" );
printf ( "\t\t******************************************\n" );
printf ( "\t\t请输入指令(1-6):" );
scanf ( "%*c%c" ,&num);
if (num < '1' || num > '6' )
{
printf ( "错误指令!\n\n" );
system ( "pause" );
continue ;
}
system ( "cls" );
printf ( "请输入修改后的信息:" );
scanf ( "%s" ,message);
switch (num)
{
case '1' : strcpy (work[number].name,message); break ;
case '2' : strcpy (work[number].edu,message); break ;
case '3' : strcpy (work[number].position,message); break ;
case '4' : strcpy (work[number].wage,message); break ;
case '5' : strcpy (work[number].address,message); break ;
case '6' : strcpy (work[number].tel,message); break ;
}
system ( "cls" );
printf ( "\n\n是否继续修改(Y/N):" );
scanf ( "%*c%c" ,&c);
if (c == 'N' || c == 'n' ) break ;
}
write();
}
//删除信息模块
void del() //删除职工信息
{
char delete_ID[20]; //删除职工工号
int i,j;
int flag; //标记是否有该职工信息
char c;
read();
while (1)
{
system ( "cls" );
flag = 0;
printf ( "请输入需删除职工信息的工号:" );
scanf ( "%s" ,delete_ID);
for (i = 0 ; i < n ; i ++)
{
if ( strcmp (work[i].ID,delete_ID) == 0)
{
n --;
flag = 1;
for (j = i ; j < n ; j ++)
{
work[j] =work[j + 1];
}
break ;
}
}
if (!flag) printf ( "\n\n无此员工信息!\n\n" );
else printf ( "\n\n删除成功!\n" );
printf ( "\n\n是否继续删除(Y/N):" );
scanf ( "%*c%c" ,&c);
if (c == 'N' || c == 'n' ) break ;
}
write();
}
//菜单模块
void menu() //菜单
{
printf ( "\t\t************职工信息管理系统菜单**********\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 1.录入职工信息 *\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 2.显示职工信息 *\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 3.查询职工信息 *\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 4.修改职工信息 *\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 5.删除职工信息 *\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t* 0.退出 *\n" );
printf ( "\t\t* *\n" );
printf ( "\t\t******************************************\n" );
printf ( "\t\t请输入指令(0-5): " );
}
int main()
{
int num;
n = 0;
system ( "color 0B" );
while (1)
{
menu();
scanf ( "%d" ,&num);
system ( "cls" );
switch (num)
{
case 1: input(); break ;
case 2: show(); break ;
case 3: que(); break ;
case 4: mod(); break ;
case 5: del(); break ;
case 0: printf ( "谢谢使用!\n\n" ); return 0;
default : printf ( "\n--->无效的指令!\n\n\n" );
}
system ( "pause" );
system ( "cls" );
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/jzmzy/article/details/9147407