本文实例为大家分享了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
|
#include<stdio.h> #include<stdlib.h> #include <string.h> struct Worker { int id; //工号 char name[16]; //姓名 char sex[8]; //性别 int age; //年龄 char edu_exp[32]; //学历 int wage; //工资 char adress[32]; //地址 char phong[16]; //电话 Worker* next; }; //创建表头 Worker m_head = { 0 }; //用户输入信息 int user_input(Worker* w) { printf ( "ID:" ); scanf ( "%d" , &w->id); getchar (); //吸收回车字符 printf ( "name:" ); gets_s(w->name); printf ( "sex:" ); gets_s(w->sex); printf ( "age:" ); scanf ( "%d" , &w->age); getchar (); //吸收回车字符 printf ( "edu_exp:" ); gets_s(w->edu_exp); printf ( "wage:" ); scanf ( "%d" , &w->wage); getchar (); //吸收回车字符 printf ( "adress:" ); gets_s(w->adress); printf ( "phone:" ); gets_s(w->phong); return 0; } //添加对象 void add(Worker* w) { w->next = m_head.next; m_head.next = w; } //按插入对象 int insert(Worker* w) { Worker* cur = m_head.next; // 当前节点current Worker* pre = &m_head; // 上一个节点previous while (cur) { if (w->id < cur->id) // 找到这个位置 break ; pre = cur; cur = cur->next; // 找到最后一个对象 } // 插入到pre节点的后面 w->next = pre->next; pre->next = w; return 0; } //删除对象 int remove ( int id) { Worker* cur = m_head.next; //当前节点为第一个对象 Worker* pre = &m_head; //当前节点的前一个节点,为头节点,指向第一个对象 while (cur) { if (id == cur->id) //找到要删除的节点 { pre->next = cur->next; free (cur); //删除该节点 return 1; //成功删除返回1 } pre = cur; cur = cur->next; } return 0; } //查找对象 int find( int id) { Worker* p = m_head.next; while (p) { if (id == p->id) { printf ( "ID:%d\nName:%s\nSex:%s\nAge:%d\nEdu-exp:%s\nWage:%d\nAdress:%s\nPhone:%s\n" , p->id, p->name, p->sex, p->age, p->edu_exp, p->wage, p->adress, p->phong); return 1; //找到目标返回1 } p = p->next; } return 0; } //遍历 void show_all() { Worker* p = m_head.next; if (!p) { printf ( "暂无员工信息!\n" ); return ; } printf ( "工 号 姓 名 性 别 年 龄 学 历 工 资 住 址 电 话\n" ); printf ( "----------------------------------------------------------------\n" ); while (p) { printf ( "%d %s %s %d %s %d %s %s\n" , p->id, p->name, p->sex, p->age, p->edu_exp, p->wage, p->adress, p->phong); p = p->next; } } //向文件存入数据 int save() { const char * filename = "F:\\data.txt" ; FILE * fp = fopen (filename, "a" ); if (fp == NULL) { printf ( "can not open the file...." ); exit (0); } Worker* p = m_head.next; while (p) { fprintf (fp, "\r" ); fprintf (fp, "%d " , p->id); fprintf (fp, "%s " , p->name); fprintf (fp, "%s " , p->sex); fprintf (fp, "%d " , p->age); fprintf (fp, "%s " , p->edu_exp); fprintf (fp, "%d " , p->wage); fprintf (fp, "%s " , p->adress); fprintf (fp, "%s " , p->phong); fprintf (fp, "\t\r\n" ); p = p->next; } fclose (fp); return 0; } //修改对象 void rewrite( int id) { Worker* p = m_head.next; while (p) { if (id == p->id) { user_input(p); } p = p->next; } } //主菜单 void List() { Worker* w = NULL; char key; while (1) { do { system ( "cls" ); printf ( "*******************************************************\n" ); printf ( "* 主菜单操作界面 *\n" ); printf ( "* *\n" ); printf ( "* 1.录入职工信息 *\n" ); printf ( "* 2.浏览职工信息 *\n" ); printf ( "* 3.查询职工信息 *\n" ); printf ( "* 4.删除职工信息 *\n" ); printf ( "* 5.修改职工信息 *\n" ); printf ( "* 6.使用说明 *\n" ); printf ( "* 7.退出 *\n" ); printf ( "* *\n" ); printf ( "*******************************************************\n" ); key = getchar (); } while (key != '1' &&key != '2' &&key != '3' &&key != '4' &&key != '5' &&key != '6' &&key != '7' ); switch (key) { case '1' : system ( "cls" ); while (1) { Worker* w = (Worker*) malloc ( sizeof (Worker)); user_input(w); insert(w); if ( getchar () == 'N' ) { break ; } } save(); free (w); break ; case '2' : system ( "cls" ); show_all(); system ( "pause" ); break ; case '3' : system ( "cls" ); if (!m_head.next) { printf ( "暂无员工信息!\n" ); system ( "pause" ); break ; } while (1) { int n; printf ( "---------------------------------\n" ); printf ( "请输入要查找的ID号:" ); scanf ( "%d" , &n); if (!find(n)) { printf ( "找不到ID号为%d 的职工信息...\n" , n); } char ch = getchar (); if (ch == 'N' ) { break ; } } break ; case '4' : system ( "cls" ); if (!m_head.next) { printf ( "暂无员工信息!\n" ); system ( "pause" ); break ; } while (1) { int n; printf ( "---------------------------------\n" ); printf ( "请输入要删除的ID号:" ); scanf ( "%d" , &n); if ( remove (n)) { printf ( "成功删除ID号为%d的职工信息.\n" , n); } else printf ( "无法删除ID号为%d的职工信息.\n" , n); char ch = getchar (); if (ch == 'N' ) //摁N退出 { break ; } } break ; case '5' : system ( "cls" ); // if (!m_head.next) { printf ( "暂无员工信息!\n" ); system ( "pause" ); break ; } while (1) { int n; printf ( "请输入要修改的ID号:\n" ); scanf ( "%d" , &n); printf ( "请修改:\n" ); rewrite(n); if ( getchar () == 'N' ) { break ; } } break ; case '6' : system ( "cls" ); printf ( "职工系统使用说明:摁相应数字进入相应的子菜单,若想退出则摁N\n开始无数据,需要先进行录入.\n" ); system ( "pause" ); break ; case '7' : system ( "cls" ); // return ; break ; } } } int main() { List(); return 0; } |
这是一个简单的小例子,主要是给大家一些思路,我这里只是简单的写了保存输入的数据到文件中,你也可用写一个从文件中读取数据到控制台,并且可以通过链表进行修改.这就麻烦一点,但是思路很简单.有兴趣的同学可以do better.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。