本文实例为大家分享了C语言学生成绩管理系统的具体代码,供大家参考,具体内容如下
一、本设计涉及的重要C语言知识有:
1.单链表的各种操作
2.文件流的操作
3.单链表的排序
二、本设计主要以下几个模块组成
1.学生成绩添加模块
2.学生成绩删除模块
3.学生成绩修改模块
4.学生成绩检索模块
5.学生成绩显示模块(包括成绩排序)
6.学生成绩保存模块
效果图:
本设计大概5、6百行代码左右,通过这个设计,本人感觉C语言熟练了很多,
希望能对像我一样的C语言菜鸟同学会有点帮助,废话少说,咱们上代码!
三、程序代码
1.main.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
|
#include <stdio.h>
#include "students.h"
int select_menu( void );
void hand_menu( int cmd, int *flag);
void quit();
int main( int argc, char *argv[])
{
int cmd;
int flag = 0;
while (1){
cmd = select_menu();
hand_menu(cmd, &flag);
}
}
int select_menu( void )
{
int select = 0;
printf ( "**************************************************************\n\n" );
printf ( " 学生管理系统 \n" );
printf ( " 1:添加学生信息 2:删除学生信息\n" );
printf ( " 3:修改学生信息 4:查询学生信息\n" );
printf ( " 5:查看学生信息 6:保存\n" );
printf ( " 0:退出\n\n" );
printf ( "**************************************************************\n" );
printf ( "请选择:" );
select=getch();
while (select< '0' ||select> '6' ){
printf ( "\n输入错误,请重新选择:" );
select=getch();
}
return select;
}
void hand_menu( int cmd, int *flag)
{
static STU *stu_head = NULL;
if (*flag == 0){
stu_head = init_student(stu_head);
*flag = 1;
}
switch (cmd){
case '0' :
save_student(stu_head);
quit();
break ;
case '1' :
system ( "cls" );
stu_head = add_student(stu_head);
system ( "cls" );
break ;
case '2' :
system ( "cls" );
stu_head = del_student(stu_head);
system ( "cls" );
break ;
case '3' :
system ( "cls" );
modify_student(stu_head);
system ( "cls" );
break ;
case '4' :
system ( "cls" );
search_student(stu_head);
system ( "cls" );
break ;
case '5' :
system ( "cls" );
stu_head = display_student(stu_head);
system ( "cls" );
break ;
case '6' :
system ( "cls" );
save_student(stu_head);
system ( "cls" );
break ;
default :
break ;
}
}
void quit()
{
printf ( "\n感谢使用学生管理系统!\n" );
exit (0);
}
|
2.students.h
这个文件主要包含students.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
|
#ifndef _STUDENTS_H_
#define _STUDENTS_H_
typedef struct st{
unsigned long ID;
char name[10];
float chinese;
float math;
float english;
double all_sub;
float average;
struct st* next;
}STU;
#define ASK_STU(p) do{p=(STU *)malloc(sizeof(STU));\
if (p==NULL){ printf ( "malloc memory failed!\n" ); exit (1);}\
} while (0)
STU *search_by_nb(STU *stu_head, unsigned long nb);
STU *search_by_name(STU *stu_head, char name[]);
STU *select_sort(STU *g, char flag);
STU *add_student(STU *stu_head);
STU *del_all(STU *stu_head);
STU *del_one(STU *stu_head, int flag);
STU *del_student(STU *stu_head);
void modify_student(STU *stu_head);
STU *display_student(STU *stu_head);
STU *search_by_name(STU *stu_head, char name[]);
STU *search_by_nb(STU *stu_head, unsigned long nb);
#endif
|
3.store.h
1
2
3
4
5
|
#ifndef _STORE_H_
#define _STORE_H_
void save_student(STU *stu_head);
STU *init_student(STU *stu_head);
#endif
|
4.students.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
|
#include <stdio.h>
#include "students.h"
STU *select_sort(STU *g, char flag)
{
STU *p,*q,*t,*s,*h;
h=(STU *) malloc ( sizeof (STU));
h->next=g;
p=h;
while (p->next->next!=NULL)
{
for (s=p,q=p->next;q->next!=NULL;q=q->next)
{
if (flag == '1' )
if (q->next->ID<s->next->ID)
s=q;
if (flag == '2' )
if (q->next->chinese<s->next->chinese)
s=q;
if (flag == '3' )
if (q->next->math<s->next->math)
s=q;
if (flag == '4' )
if (q->next->english<s->next->english)
s=q;
if (flag == '5' )
if (q->next->average<s->next->average)
s=q;
}
if (s!=q)
{
t=s->next;
s->next=t->next;
t->next=p->next;
p->next=t;
}
p=p->next;
}
g=h->next;
free (h);
return g;
}
STU *add_student(STU *stu_head)
{
STU *stu;
STU *head = stu_head;
STU *node = stu_head;
STU *node_fd;
int sel;
ASK_STU(stu);
stu->next = NULL;
printf ( "请输入学生学号:" );
scanf ( "%d" , &stu->ID);
while (search_by_nb(node, stu->ID)){
printf ( "已经存在该学号!请重新输入一个学号:" );
scanf ( "%d" , &stu->ID);
}
printf ( "请输入学生名字:" );
scanf ( "%s" , stu->name);
printf ( "请输入该学生英语成绩:" );
scanf ( "%f" , &stu->english);
while (stu->english < 0.0 || stu->english > 100.0){
printf ( "输入错误,请重新输入英语成绩:" );
scanf ( "%f" , &stu->english);
}
printf ( "请输入该学生数学成绩:" );
scanf ( "%f" , &stu->math);
while (stu->math < 0.0 || stu->math > 100.0){
printf ( "输入错误,请重新输入数学成绩:" );
scanf ( "%f" , &stu->math);
}
printf ( "请输入该学生语文成绩:" );
scanf ( "%f" , &stu->chinese);
while (stu->chinese < 0.0 || stu->chinese > 100.0){
printf ( "输入错误,请重新输入数学成绩:" );
scanf ( "%f" , &stu->chinese);
}
stu->all_sub = stu->chinese+stu->math+stu->english;
stu->average = stu->all_sub/3;
if (!head){
head = stu;
return head;
}
while (node->next)
node = node->next;
node->next = stu;
head = select_sort(head, '1' );
return head;
}
STU *del_all(STU *stu_head)
{
if (!stu_head)
return NULL;
del_all(stu_head->next);
free (stu_head);
stu_head = NULL;
return NULL;
}
STU *del_one(STU *stu_head, int flag)
{
STU *node = stu_head;
STU *head = stu_head;
STU *pre;
char name[10];
unsigned long ID;
if (head = NULL){
printf ( "没有任何学生信息!\n" );
return head;
}
if (flag == '2' ){
printf ( "请输入要删除的学生姓名:" );
scanf ( "%s" , name);
if (! strcmp (node->name, name)){
free (node);
head = node->next;
printf ( "成功删除!按任意键返回。\n" );
getch();
return head;
}
while (node){
if (! strcmp (node->name, name)){
pre->next = node->next;
free (node);
printf ( "成功删除!按任意键返回。\n" );
getch();
return head;
}
pre = node;
node = node->next;
}
printf ( "没有找到该学生信息!\n" );
return head;
}
if (flag == '3' ){
printf ( "请输入要删除的学生学号:" );
scanf ( "%d" , &ID);
if (node->ID == ID){
free (node);
head = node->next;
printf ( "成功删除!按任意键返回。\n" );
getch();
return head;
}
while (node){
if (! strcmp (node->name, name)){
pre->next = node->next;
free (node);
printf ( "成功删除!按任意键返回。\n" );
getch();
return head;
}
pre = node;
node = node->next;
}
printf ( "没有找到该学生信息!按任意键返回\n" );
getch();
return head;
}
}
STU *del_student(STU *stu_head)
{
int sel;
STU *head = stu_head;
printf ( "1:删除全部 2:按名字删除 3:按学号删除 0:返回\n" );
printf ( "请选择:\n" );
sel = getch();
while (sel< '0' ||sel> '3' ){
printf ( "输入错误,请重新选择:\n" );
sel = getch();
}
if (sel == '0' )
return head;
if (!head){
printf ( "没有任何学生信息!按任意键返回.\n" );
getch();
return head;
}
if (sel == '1' ){
head = del_all(head);
printf ( "删除成功!按任意键返回." );
getch();
return head;
}
if (sel == '2' ||sel == '3' ){
head = del_one(head, sel);
return head;
}
}
void modify_student(STU *stu_head)
{
STU *head = stu_head;
STU *node_fd;
int sel;
char name[10];
unsigned long nb;
recyc3:
printf ( "1:修改指定学号的学生信息 2:修改指定姓名的学生信息 0:返回\n请选择:\n" );
sel = getch();
while (sel < '0' || sel> '2' ){
printf ( "输入错误!请重新选择:\n" );
sel = getch();
}
if (sel == '1' ||sel== '2' ){
if (sel == '1' ){
printf ( "请输入要修改的学生学号:" );
scanf ( "%d" , &nb);
node_fd = search_by_nb(head, nb);
}
if (sel == '2' ){
printf ( "请输入要修改的学生姓名:" );
scanf ( "%s" , name);
node_fd = search_by_name(head, name);
}
if (node_fd){
printf ( "请输入该学生英语成绩:" );
scanf ( "%f" , &node_fd->english);
while (node_fd->english < 0.0 || node_fd->english > 100.0){
printf ( "输入错误,请重新输入英语成绩:" );
scanf ( "%f" , &node_fd->english);
}
printf ( "请输入该学生数学成绩:" );
scanf ( "%f" , &node_fd->math);
while (node_fd->math < 0.0 || node_fd->math > 100.0){
printf ( "输入错误,请重新输入数学成绩:" );
scanf ( "%f" , &node_fd->math);
}
printf ( "请输入该学生语文成绩:" );
scanf ( "%f" , &node_fd->chinese);
while (node_fd->chinese < 0.0 || node_fd->chinese > 100.0){
printf ( "输入错误,请重新输入数学成绩:" );
scanf ( "%f" , &node_fd->chinese);
}
node_fd->all_sub = node_fd->chinese+node_fd->math+node_fd->english;
node_fd->average = node_fd->all_sub/3;
printf ( "修改成功!\n" );
goto recyc3;
}
else {
printf ( "没有该学生信息!\n" );
goto recyc3;
}
}
}
STU *display_student(STU *stu_head)
{
int sel;
STU *head = stu_head;
STU *node = stu_head;
if (node == NULL){
printf ( "没有任何学生信息,按任意键返回" );
getch();
return head;
}
recyc:
printf ( "学号 姓名 语文 数学 英文 平均分 总分\n" );
while (node){
printf ( "%d %s %0.1f %0.1f %0.1f %0.1f %0.1f\n" , node->ID, node->name,\
node->chinese, node->math, node->english, node->average, node->all_sub);
node=node->next;
}
printf ( "排序方式: 1:按学号排序 2:按语文成绩排序 3:按数学成绩排序\n 4:按英文成绩排序 5:按总分排序 0:返回\n请选择:" );
sel = getch();
while (sel < '0' || sel> '5' ){
printf ( "输入错误!请重新选择:" );
sel = getch();
}
if (sel == '0' )
return head;
head = node = select_sort(head, sel);
system ( "cls" );
goto recyc;
return head;
}
STU *search_by_name(STU *stu_head, char name[])
{
STU *node = stu_head;
if (!node)
return NULL;
while (node){
if (! strcmp (node->name, name))
return node;
node=node->next;
}
return NULL;
}
STU *search_by_nb(STU *stu_head, unsigned long nb)
{
STU *node = stu_head;
if (!node)
return NULL;
while (node){
if (node->ID == nb)
return node;
node=node->next;
}
return NULL;
}
void search_student(STU *stu_head)
{
STU *head = stu_head;
STU *node;
int sel = 0;
char name[10];
unsigned long ID;
recyc2:
printf ( "1:按姓名查找 2:按学号查找 0:返回\n请选择:" );
sel = getch();
while (sel< '0' || sel> '2' ){
printf ( "\n输入错误,请重新选择:" );
sel = getch();
}
if (sel == '0' )
return ;
if (sel == '1' ||sel== '2' ){
if (sel == '1' ){
printf ( "\n请输入你要查找的学生姓名:" );
scanf ( "%s" , name);
node = search_by_name(head, name);
}
else {
printf ( "\n请输入你要查找的学生学号:" );
scanf ( "%d" , &ID);
node = search_by_nb(head, ID);
}
if (node){
printf ( "学号 姓名 语文 数学 英文 平均分 总分\n" );
printf ( "%d %s %0.1f %0.1f %0.1f %0.1f %0.1f\n" , node->ID, node->name,\
node->chinese, node->math, node->english, node->average, node->all_sub);
goto recyc2;
}
else {
printf ( "没有找到该学生信息!\n" );
goto recyc2;
}
}
}
|
5.store.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
|
#include <stdlib.h>
#include <stdio.h>
#include "students.h"
#define FILE_NAME "students"
void save_student(STU *stu_head)
{
STU *node = stu_head;
FILE *fp;
fp = fopen (FILE_NAME, "w+" );
while (node){
fprintf (fp, "%ld %s %0.1f %0.1f %0.1f %0.1lf %0.1f\n" , node->ID, node->name,\
node->chinese, node->math, node->english, node->all_sub, node->average);
node=node->next;
}
fclose (fp);
printf ( "保存成功!按任意键返回。\n" );
getch();
}
STU *init_student(STU *stu_head)
{
STU *node = stu_head;
STU *head = stu_head;
STU *new_node;
int i;
FILE *fp;
char name[10];
int num;
ASK_STU(new_node);
new_node->next=NULL;
fp = fopen (FILE_NAME, "r+" );
rewind (fp);
while ((i = fscanf (fp, "%ld%s%f%f%f%lf%f\n" , &new_node->ID, new_node->name,\
&new_node->chinese, &new_node->math, &new_node->english, &new_node->all_sub, &new_node->average)) == 7){
if (!head)
head = node = new_node;
else {
while (node->next)
node = node->next;
node->next = new_node;
}
ASK_STU(new_node);
new_node->next = NULL;
}
fclose (fp);
return head;
}
|
四、总结
菜鸟修炼C语言基础部分到此告一段落了,通过这段时间的修炼对C语言有了更加深入的理解,
能够熟练运用指针、数组、结构体和链表进行代码的编写了。
如果以后有时间,可能会修炼一下C算法,下一段时间我会修炼C++。
希望能对像我一样的C语言菜鸟能提供到帮助,大家共同努力,共同奋斗!
原文链接:http://blog.csdn.net/forever_key/article/details/6962100