本文实例为大家分享了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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
|
/*copyright(c)2016.烟台大学计算机学院 * All rights reserved, * 文件名称:text.Cpp * 作者:吴敬超 * 完成日期:2016年7月1日 * 版本号:codeblock * * 问题描述: 学生信息管理系统 * 输入描述: * 程序输出: 输出结果 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #include <time.h> # define LEN sizeof(struct Student) struct Student { char num[100]; //学号 char name[200]; //姓名 char sex[100]; //性别 int age; //年龄 char phone[120]; //电话 char address[120]; //地址 char birthday[200]; //生日 char mail[200] ; //邮件 struct Student *next; }; void menu(); void start(); void welcome(); char filename[30]; //全局变量,用来保存要打开的文件名字 struct Student *Creat( int n); //生成链表 struct Student *Creat( int n) { void menu_print_in( void ); struct Student *head; struct Student *p1, *p2; system ( "cls" ); for ( int i=1;i<n+1;i++) { p1 = ( struct Student*) malloc (LEN); menu_print_in(); scanf ( "%s%s%s%d%s%s%s%s" ,p1->num,p1->name,p1->sex, &p1->age,p1->phone,p1->address,p1->birthday,p1->mail); p1->next = NULL; if (i==1) { head = p2 = p1; } else { p2->next = p1; p2 = p1; } } return (head); } //数据存盘(wb只写) void WriteData_wb( struct Student *head) { FILE *fp; struct Student *p; if ((fp = fopen (filename, "wb" ))==NULL) printf ( "\a 文件打开错误" ); p = head; while (p!=NULL) { if ( fwrite (p,LEN,1,fp)!=1) { printf ( "写入数据出错\n" ); fclose (fp); return ; } p=p->next; } fclose (fp); } //数据存盘(ab追加) void WriteData_ab( struct Student *head) { FILE *fp; struct Student *p; if ((fp = fopen (filename, "ab" ))==NULL) printf ( "\a 文件打开错误!" ); p = head; while (p!=NULL) { if ( fwrite (p,LEN,1,fp)!=1) { printf ( "写入数据出错\n" ); fclose (fp); return ; } p=p->next; } fclose (fp); } //读取数据文件保存到链表中 ,返回指向此链表头指针 struct Student *ReadData( void ) { struct Student *head = NULL; struct Student *p1, *p2; //s = p1;p = p2; FILE *fp; if ((fp= fopen (filename, "rb+" ))==NULL) { printf ( "打开文件出错\n" ); exit (0); } while (! feof (fp)) { if ((p1=( struct Student*) malloc (LEN))==NULL){ printf ( "内存申请出错\n" ); fclose (fp); exit (0); } if ( fread (p1,LEN,1,fp)!=1){ free (p1); break ; } if (head==NULL) head=p2=p1; else { p2->next=p1; p2=p1; } } fclose (fp); return (head); } //全部查询 void display() { system ( "cls" ); void menu_print_out( void ); struct Student *p; p = ReadData(); menu_print_out(); do { printf ( "%-10s%6s%8s%4d%13s%11s %4s %4s" ,p->num,p->name,p->sex, p->age,p->phone,p->address,p->birthday,p->mail); p = p->next; printf ( "\n\n" ); } while (p!=NULL); printf ( "\n\n" ); } //学号查询 int query_num() { system ( "cls" ); void menu_print_out(); struct Student *p; char str_num[10]; printf ( "请输入您要查询的学号:" ); scanf ( "%s" , str_num); p = ReadData(); menu_print_out(); do { if ( strcmp (p->num,str_num)==0) { printf ( "%-10s%6s%8s%4d%13s%11s %4s %4s" ,p->num,p->name,p->sex, p->age,p->phone,p->address,p->birthday,p->mail); printf ( "\n\n" ); return 0; } p = p->next; } while (p!=NULL); printf ( "数据库中没有存储您要查询的数据!\n" ); printf ( "\n\n" ); return 0; } //姓名查询 int query_name() { system ( "cls" ); void menu_print_out( void ); struct Student *p; char str_name[20]; printf ( "请输入您要查询的姓名:" ); scanf ( "%s" , str_name); p = ReadData(); menu_print_out(); do { if ( strcmp (p->name,str_name)==0) { printf ( "%-8s%6s%8s%4d%13s%11s %4s %4s" ,p->num,p->name,p->sex, p->age,p->phone,p->address,p->birthday,p->mail); printf ( "\n\n" ); return 0; } p = p->next; } while (p!=NULL); printf ( "数据库中没有存储您要查询的数据!\n" ); printf ( "\n\n" ); return 0; } //【1】修改数据之删除记录 int delStudent() { struct Student *p1, *p2, *head; char str_num[20]; printf ( "\n请输入您要删除的学号信息:" ); scanf ( "%s" , str_num); p1 = ReadData(); p2 = p1->next; head = p1; while (p2!=NULL) { if ( strcmp (p1->num,str_num)==0) { WriteData_wb(p2); } else if ( strcmp (p2->num,str_num)==0) { p1->next = p2->next; WriteData_wb(head); } p2 = p2->next; p1 = p1->next; } if (p2!=NULL) printf ( "数据库中没有存储您要删除的数据!\n" ); printf ( "\n\n" ); return 0; } //【2】修改数据之修改记录 int change1() { void menu_print_in( void ); struct Student *p1, *p2, *head; char str_num[20]; printf ( "请输入您要修改的学号信息:" ); scanf ( "%s" , str_num); p1 = ReadData(); p2 = p1->next; head = p1; while (p2!=NULL) { if ( strcmp (p1->num,str_num)==0) { menu_print_in(); scanf ( "%s%s%s%d%s%s%s%s" ,p1->num,p1->name,p1->sex, &p1->age,p1->phone,p1->address,p1->birthday,p1->mail); WriteData_wb(head); } else if ( strcmp (p2->num,str_num)==0) { menu_print_in(); scanf ( "%s%s%s%d%s%s%s%s" ,p2->num,p2->name,p2->sex, &p2->age,p2->phone,p2->address,p2->birthday,p2->mail); WriteData_wb(head); } p2 = p2->next; p1 = p1->next; } if (p2!=NULL) printf ( "数据库中没有存储您要删除的数据!\n" ); return 0; } void start() //欢迎界面 { system ( "color 1E" ); printf ( "\n\n\n\n\n\n\n\n\n\t\t\t⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙\n" ); printf ( "\t\t\t⊙ ⊙\n" ); printf ( "\t\t\t⊙ 欢迎使用学生信息管理系统 ⊙\n" ); printf ( "\t\t\t⊙ ⊙\n" ); printf ( "\t\t\t⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙⊙\n" ); printf ( "\n\t\t\t\t系统开始启动.........\n" ); printf ( "===============================================================================\r" ); for ( int i=1;i<80;i++) { Sleep(40); printf ( ">" ); } system ( "CLS" ); welcome(); } void welcome() //登录界面 { system ( "CLS" ); system ( "color 2" ); printf ( "\n\n\n\n\n\n\t\t\t★★★★★★★★★★★★★★★★\n" ); printf ( "\t\t\t★ ★\n" ); printf ( "\t\t\t★ 欢迎使用学生信息管理系统 ★\n" ); printf ( "\t\t\t★ ★\n" ); printf ( "\t\t\t★ 设计者:吴敬超 ★\n" ); printf ( "\t\t\t★ ★\n" ); printf ( "\t\t\t★ 日期:2016.7.1 ★\n" ); printf ( "\t\t\t★ ★\n" ); printf ( "\t\t\t★★★★★★★★★★★★★★★★\n" ); printf ( "\n\t\t\t按任意键继续..." ); getchar (); system ( "CLS" ); menu(); } //主菜单 void menu() { void addStudent(); void query(); int File_name(); void change(); system ( "CLS" ); system ( "color D" ); printf ( "\t\t\t★★★★★★主菜单※※※※※\n" ); printf ( "\n\n\n\n\n\t\t\t※※※※※※※※※※※※※※\n" ); printf ( "\t\t\t※ ※\n" ); printf ( "\t\t\t※ 1.录入学生信息 ※\n" ); printf ( "\t\t\t※------------------------※\n" ); printf ( "\t\t\t※ 2.查询学生信息 ※\n" ); printf ( "\t\t\t※------------------------※\n" ); printf ( "\t\t\t※ 3.修改学生信息 ※\n" ); printf ( "\t\t\t※------------------------※\n" ); printf ( "\t\t\t※ 4. 退出系统 ※\n" ); printf ( "\t\t\t※------------------------※\n" ); printf ( "\t\t\t※※※※※※※※※※※※※※\n" ); printf ( "\n\t\t请输入功能前的序号选择服务项目:" ); int a = 0; a = getchar (); while (a!= '1' &&a!= '2' &&a!= '3' &&a!= '4' ) { printf ( "错误,请输入正确的号码!\n" ); putchar ( '\a' ); getchar (); printf ( "\n\t\t请输入功能前的序号选择服务项目:" ); a = getchar (); } switch (a) { case '1' : File_name();addStudent(); break ; case '2' : File_name();query(); break ; case '3' : File_name();change(); break ; case '4' : exit (0); break ; } getchar (); } int File_name() { printf ( "\n\t\t请输入您要打开的文件:" ); if ( scanf ( "%s" , filename)!=1) printf ( "\a error!" ); return 0; } //二级菜单之添加数据 void addStudent() { int Creat_num(); system ( "cls" ); getchar (); system ( "CLS" ); system ( "color B" ); printf ( "\t\t\t★★★★★★添加数据※※※※※\n" ); printf ( "\n\n\n\n\n\t\t\t※※※※※※※※※※※※※\n" ); printf ( "\t\t\t※ ※\n" ); printf ( "\t\t\t※ 1. 新建文件 ※\n" ); printf ( "\t\t\t※----------------------※\n" ); printf ( "\t\t\t※ 2. 增添数据 ※\n" ); printf ( "\t\t\t※----------------------※\n" ); printf ( "\t\t\t※ 3. 返回菜单 ※\n" ); printf ( "\t\t\t※----------------------※\n" ); printf ( "\t\t\t※※※※※※※※※※※※※\n" ); printf ( "\n\t\t请输入功能前的序号选择服务项目:" ); int a = 0; a = getchar (); while (a!= '1' &&a!= '2' &&a!= '3' ) { printf ( "错误,请输入正确的号码!\n" ); putchar ( '\a' ); getchar (); printf ( "\n\t\t请输入功能前的序号选择服务项目:" ); a = getchar (); } switch (a) { case '1' : WriteData_wb(Creat(Creat_num())); printf ( "\n新建文件成功且数据已成功保存\n" ); system ( "pause" ); system ( "cls" ); addStudent(); break ; case '2' : WriteData_ab(Creat(Creat_num())); printf ( "\n数据已成功添加\n" ); system ( "pause" ); system ( "cls" ); addStudent(); break ; case '3' : system ( "cls" ); getchar (); menu(); break ; } } //二级菜单之查询数据 void query() { system ( "cls" ); getchar (); while (1) { system ( "CLS" ); system ( "color C" ); printf ( "\t\t\t★★★★★查询数据※※※※※\n" ); printf ( "\n\n\n\n\n\t\t\t※※※※※※※※※※※※※※\n" ); printf ( "\t\t\t※ ※\n" ); printf ( "\t\t\t※ 1.全部查询 ※\n" ); printf ( "\t\t\t※------------------------※\n" ); printf ( "\t\t\t※ 2.学号查询 ※\n" ); printf ( "\t\t\t※------------------------※\n" ); printf ( "\t\t\t※ 3.姓名查询 ※\n" ); printf ( "\t\t\t※------------------------※\n" ); printf ( "\t\t\t※ 4.返回菜单 ※\n" ); printf ( "\t\t\t※------------------------※\n" ); printf ( "\t\t\t※※※※※※※※※※※※※※\n" ); printf ( "\n\t\t请输入功能前的序号选择服务项目:" ); int a = 0; a = getchar (); while (a!= '1' &&a!= '2' &&a!= '3' &&a!= '4' ) { printf ( "错误,请输入正确的号码!\n" ); putchar ( '\a' ); getchar (); printf ( "\n\t\t请输入功能前的序号选择服务项目:" ); a = getchar (); } switch (a) { case '1' : display(); system ( "pause" ); getchar (); break ; case '2' : query_num(); system ( "pause" ); getchar (); break ; case '3' : query_name(); system ( "pause" ); getchar (); break ; case '4' : system ( "cls" ); getchar ();menu(); break ; } } } int Creat_num() { printf ( "\n\t\t请输入您要添加的数据个数:" ); int n; if ( scanf ( "%d" , &n)!=1) { printf ( "\a error!" ); } return n; } //二级菜单之修改数据/ void change() { system ( "cls" ); getchar (); while (1) { system ( "CLS" ); system ( "color A" ); printf ( "\t\t\t★★★★★★修改数据※※※※※\n" ); printf ( "\n\n\n\n\n\t\t\t※※※※※※※※※※※※※※\n" ); printf ( "\t\t\t※ ※\n" ); printf ( "\t\t\t※ 1.删除记录 ※\n" ); printf ( "\t\t\t※------------------------※\n" ); printf ( "\t\t\t※ 2.修改记录 ※\n" ); printf ( "\t\t\t※------------------------※\n" ); printf ( "\t\t\t※ 3.返回菜单 ※\n" ); printf ( "\t\t\t※------------------------※\n" ); printf ( "\t\t\t※※※※※※※※※※※※※※\n" ); printf ( "\n\t\t请输入功能前的序号选择服务项目:" ); int a = 0; a = getchar (); while (a!= '1' &&a!= '2' &&a!= '3' ) { printf ( "错误,请输入正确的号码!\n" ); putchar ( '\a' ); getchar (); printf ( "\n\t\t请输入功能前的序号选择服务项目:" ); a = getchar (); } switch (a) { case '1' : delStudent(); printf ( "\n\n已成功删除指定数据\n" ); system ( "pause" ); getchar (); break ; case '2' : change1(); printf ( "\n\n已成功修改指定数据\n" ); system ( "pause" ); getchar (); break ; case '3' : system ( "cls" ); getchar (); menu(); break ; } } } //输入输出提示栏 void menu_print_in() { printf ( "------------------------------------------------------------------------\n" ); printf ( "学号 姓名 性别 年龄 电话 地址 生日 邮件 \n" ); printf ( "------------------------------------------------------------------------\n" ); } void menu_print_out( void ) { printf ( "--------------------------------------------------------------------------\n" ); printf ( "学号 姓名 性别 年龄 电话 地址 生日 邮件 \n" ); printf ( "--------------------------------------------------------------------------\n" ); } //主函数 int main() { start(); return 0; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/jnwujingchao/article/details/51801500