C语言学生信息管理系统设计与实现

时间:2022-02-28 06:48:57

本文实例为大家分享了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
#include"stdio.h" //标准的输入输出函数文件头部说明
#include"math.h"  // 数学函数头部说明
#include"string.h"
#include"stdlib.h" //通过该函数头部里的函数,改变控制台的背景和颜色
#include"windows.h" //头文件声明,下文用到了改变控制台的宽度和高度
#define M 100  //宏定义说明
struct student{ //结构体定义并声明
 char name[25]; //姓名
 char num[25]; //学号
 char credit[20]; //身份证号
 char native[35]; //籍贯
 char tel[25]; //手机号
 int special; //专业
 int banji; //班级
 int math,yy,wl,cyy,pe; //数学、英语、物理、C语言、体育
 double ave;}; //平均分
//****************************************函数的声明********************************************
void input(struct student stu[M]); //输入函数
void output(struct student stu[M]); //各类用户自定义函数的声明
void lookfor(struct student stu[M]); //查询函数
void modify(struct student stu[M]); //修改函数
void order(struct student stu[M]); //排序函数
void delete_student(struct student stu[M]); //删除函数
void xuehao(struct student stu[M]);
void xingming(struct student stu[M]);
void fileread(struct student stu[M]);
void filewrite(struct student stu[M]);
void yanshi(char *p); //延时函数说明
//**********************************************************************************************
int count=0;
struct student t;
int main()
{
 int choice,sum;
 struct student stu[M]; 
 system("mode con:cols=400 lines=30000"); //调节控制台的宽度和高度
 system("color 0b"); //调节控制台的背景和字体颜色
point1:
 sum=0;
 yanshi("\t\t\t\t\t\t\t\t\3\3\3\3\3\3\3\3\3\3\3\3\3欢迎你使用学生信息管理系统\3\3\3\3\3\3\3\3\3\3\n");
 do {
 printf("\t\t\t\t\t\t\t\t-------------------------------------------------\n");
 printf("\t\t\t\t\t\t\t\t+ 学生信息管理系统  +\n");
 printf("\t\t\t\t\t\t\t\t-------------------------------------------------\n");
 printf("\t\t\t\t\t\t\t\t\t\t ***************\n");
 printf("\t\t\t\t\t\t\t\t\t\t 1、添加学生信息\n");
 printf("\t\t\t\t\t\t\t\t\t\t 2、浏览学生信息\n");
 printf("\t\t\t\t\t\t\t\t\t\t 3、查询学生信息\n");
 printf("\t\t\t\t\t\t\t\t\t\t 4、修改学生信息\n");
 printf("\t\t\t\t\t\t\t\t\t\t 5、删除学生信息\n");
 printf("\t\t\t\t\t\t\t\t\t\t 6、排列学生信息\n");
 printf("\t\t\t\t\t\t\t\t\t\t 7、读取文件学生\n");   //从文件读取
 printf("\t\t\t\t\t\t\t\t\t\t 8、保存到文件\n");   //保存到文件
 printf("\t\t\t\t\t\t\t\t\t\t 9、退出系统\n");
 printf("\t\t\t\t\t\t\t\t\t\t ***************\n");
 printf("请输入你的选择\n");
 scanf("%d",&choice);
 fflush(stdin); //清除输入缓冲区
 if (choice>9||choice<=0)
 {
 sum++;
 if (sum>=5)
 {
 printf("输入错误次数过多,程序将重新开始\n");
 system("pause"); //程序暂停
 system("cls"); //清屏语句
 goto point1;
 }
 }
 switch (choice) //根据选择,调用不同的函数来完成不同的任务
 {
 case 1:input(stu);break
 case 2:output(stu);break;
 case 3:lookfor(stu);break;
 case 4:modify(stu);break;
 case 5:delete_student(stu);break;
 case 6:order(stu);break;
 case 7:fileread(stu);break;
 case 8:filewrite(stu);;break;
 case 9:printf("感谢你使用学生信息管理系统,请关掉程序!!!\n");system("pause");break;
 default:printf("无效的选择!!!请重新输入!!!\n");break;
 }
 }while (choice!=9);
 printf("the program is over!!!\n");
 return 0;
}
void input(struct student stu[M]) //自定义输入函数
{
 int len,size;
 system("cls");
 printf("请添加要输入学生的信息\n");
 do {
 printf("请输入由11位数字组成的学生学号\n"); //do-while循环应用,提示输入位数为一确定数
 scanf("%s",&stu[count].num);
 len=strlen(stu[count].num);
 }while(len!=11);
 printf("请输入同学的姓名\n");
 scanf("%s",stu[count].name);
 do {
 printf("请输入由18位数字组成的学生身份证号\n"); //同上
 scanf("%s",&stu[count].credit);
 size=strlen(stu[count].credit);
 }while(size!=18);      
 printf("请输入学生的籍贯\n");
 scanf("%s",&stu[count].native);
 printf("请输入学生的手机号码\n");
 scanf("%s",&stu[count].tel);
 printf("请输入所需要的专业代号:1、计算机科学 2、通信工程 3、网络工程\n"); //采用如此方法解决了专业输入难问题
 scanf("%d",&stu[count].special);
 printf("请输入对应的学生班级号码:1、1301 2、1302 \n");
 scanf("%d",&stu[count].banji);
 do {
 printf("请依次输入不大于100的学生各科成绩\n");
 scanf("%d%d%d%d%d",&stu[count].math,&stu[count].yy,&stu[count].wl,&stu[count].cyy,&stu[count].pe);
 }while(stu[count].math>100||stu[count].yy>100||stu[count].wl>100||stu[count].cyy>100||stu[count].pe>100); //同上同上
 stu[count].ave=(stu[count].math+stu[count].yy+stu[count].wl+stu[count].cyy+stu[count].pe)/5.0; //求出平均值
 count++;
}
void output(struct student stu[M]) //自定义输出函数
{
 int j;
 system("cls");
 if (count==0)
 {
 printf("当前已存学生信息为0个\n");
 return;
 }
 for (j=0;j<count;j++)
 {
 printf("学号\t\t 姓名\t\t身份证号\t籍贯\t手机号\t\t专业\t\t班级\t数学\t英语\t物理\tC语言\t体育\t平均分\n");
 for (j=0;j<count;j++){ //for循环控制输出的个数
 printf("%s\t",stu[j].num);
 printf("%s\t",stu[j].name);
 printf("%s\t",stu[j].credit);
 printf("%s\t",stu[j].native);
 printf("%s\t",stu[j].tel);
 if (stu[j].special==1)
 printf("计算机科学\t");
 else if (stu[j].special==2)
 printf("通信工程\t");
 else
 printf("网络工程\t");
 printf("%d\t",stu[j].banji);
 printf("%d\t",stu[j].math);
 printf("%d\t",stu[j].yy);
 printf("%d\t",stu[j].wl);
 printf("%d\t",stu[j].cyy);
 printf("%d\t",stu[j].pe);
 printf("%.1lf\t\n",stu[j].ave);}
 }
}
void lookfor(struct student stu[M])   //自定义查询学生信息函数
{
 int j,flag=0;
 char xh[25];
 system("cls");
 if (count==0)
 {
 printf("当前已存学生信息为0个,无法查询!!!\n");
 return;
 }
 else
 {
 printf("请输入你想要查看的同学学号\n");
 scanf("%s",&xh);
 fflush(stdin);
 for (j=0;j<count;j++)
 {
 if (strcmp(stu[j].num,xh)==0) //通过字符函数对已存入的学生信息进行比较,找出要查看的学生
 {
 printf("学号\t\t姓名\t\t身份证号\t籍贯\t手机号\t\t专业\t\t班级\t数学\t英语\t物理\tC语言\t体育\t平均分\n");
 printf("%s\t",stu[j].num);
 printf("%s\t",stu[j].name);
 printf("%s\t",stu[j].credit);
 printf("%s\t",stu[j].native);
 printf("%s\t",stu[j].tel);
 if (stu[j].special==1)
 printf("计算机科学\t");
 else if (stu[j].special==2)
 printf("通信工程\t");
 else
 printf("网络工程\t");
 printf("%d\t",stu[j].banji); //考虑一个对齐的因数
 printf("%d\t",stu[j].math);
 printf("%d\t",stu[j].yy);
 printf("%d\t",stu[j].wl);
 printf("%d\t",stu[j].cyy);
 printf("%d\t",stu[j].pe);
 printf("%.1lf\t",stu[j].ave);
 }
 }
 if (j==count)
 printf("很抱歉,没有你所需要的学生信息\n");
 }
}
void modify(struct student stu[M]) //自定义修改函数
{
 int j,flag=0,course;
 char xh[25];
 system("cls");
 if (count==0)
 {
 printf("当前已存学生信息为0个,无法修改!!!\n");
 return;
 }
 else
 {
 printf("请输入你想要修改的同学学号\n");
 scanf("%s",&xh);
 fflush(stdin);
 for (j=0;j<count;j++)
 if (strcmp(stu[j].num,xh)==0) //同上
 {
 printf("你确定要修改学生的信息吗???如果不确定的话,请关掉本程序吧!!!\n");
 printf("选择课程: 1、数学 2、英语 3、物理 4、C语言 5、体育\n");
 scanf("%d",&course);
 printf("请输入你想要修改后的学生成绩\n");
 switch(course)
 {
 case 1:scanf("%d",&stu[j].math);break;
 case 2:scanf("%d",&stu[j].yy);break; //switch控制语句
 case 3:scanf("%d",&stu[j].wl);break;
 case 4:scanf("%d",&stu[j].cyy);break;
 case 5:scanf("%d",&stu[j].pe);break;
 default:printf("无效的选择!!!请重新输入!!!\n");break;
 }
 }
 }
}
void delete_student(struct student stu[M]) //自定义删除函数
{
 int choice;
 system("cls");
 if (count==0)
 {
 printf("当前已存学生信息为0个,无法删除!!!\n");
 return;
 }
 else
 {
 printf("请选择你所要删除的方式:1、学号 2、姓名(如果你的姓名是中文,那么无法删除,请选择学号删除) 3、取消\n");
 scanf("%d",&choice);
 switch(choice)  //switch语句对用户要删除的方式进行选择
 {
 case 3:return;break;
 case 2:xingming(stu);break//用户自定义函数之间的套用
 case 1:xuehao(stu);break;
 default:printf("无效的选择!!!请重新输入!!!\n");break;
 }
 }
}
void order(struct student stu[M]) //排序函数的定义
{
 int j,k,choice,index;
 system("cls");
 printf("请输入你想要进行排序的方式(所有排序的方式均是降序)!!!\n");
 printf("1、数学 2、英语 3、物理 4、C语言 5、体育 6、平均分\n");
 scanf("%d",&choice);
 switch (choice)
 {
 case 1:for (j=0;j<count;j++)
 { index=j;
 for (k=j+1;k<count;k++)
 if (stu[k].math>stu[index].math)
 {t=stu[k];stu[k]=stu[index];stu[index]=t;}
 };break;
 case 2:for (j=0;j<count;j++)
 { index=j;
 for (k=j+1;k<count;k++)
 if (stu[k].yy>stu[index].yy)
 {stu[k]=stu[index];}    //整个为排序部分,采用了所谓的选择排序的方法
 };break;
 case 3:for (j=0;j<count;j++)
 { index=j;
 for (k=j+1;k<count;k++)
 if (stu[k].wl>stu[index].wl)
 {t=stu[k];stu[k]=stu[index];stu[index]=t;}
 };break;
 case 4:for (j=0;j<count;j++)
 { index=j;
 for (k=j+1;k<count;k++)
 if (stu[k].cyy>stu[index].cyy)
 {t=stu[k];stu[k]=stu[index];stu[index]=t;}
 };break;
 case 5:for (j=0;j<count;j++)
 { index=j;
 for (k=j+1;k<count;k++)
 if (stu[k].pe>stu[index].pe)
 {t=stu[k];stu[k]=stu[index];stu[index]=t;}
 };break;
 case 6:for (j=0;j<count;j++)
 { index=j;
 for (k=j+1;k<count;k++)
 if (stu[k].ave>stu[index].ave)
 {t=stu[k];stu[k]=stu[index];stu[index]=t;} //强制类型转换符号只能用于有操作数,根据赋值运算中的类型转换问题可知
 };break;
 default:printf("无效的选择!!!请重新输入!!!\n");break;
 }
}
void xuehao(struct student stu[M])  //自定义通过学号方式删除学生信息函数
{
 int j,index=0,k=count;
 char xh[25];
 system("cls");
 printf("请输入你想要删除的同学学号\n");
 scanf("%s",xh);
 fflush(stdin);
 for (j=0;j<count;j++)
 { if (strcmp(stu[j].num,xh)==0)
 {
 for (j=index;j<count;j++)
 stu[j]=stu[j+1];
 count--;
 if (count<k)
 printf("你已经删除成功\n");
 }
 index++;}
 if (j==count)
 printf("抱歉!!!没有你所需要删除的学生信息!*_*!\n");
}
void xingming(struct student stu[M])  //自定义通过姓名方式删除学生信息函数
{
 int flag=0,j,k=count;
 char xm[25];
 system("cls");
 printf("请输入你想要删除的同学姓名\n");
 scanf("%s",xm);
 fflush(stdin);
 for (j=0;j<count;j++)
 {
 if (strcmp(stu[j].num,xm)==0)
 {
 
 for (j=flag-1;j<count;j++) 
 stu[j]=stu[j+1];
 count--;
 if (count<k)
 printf("你已经删除成功\n");
 }
 flag++;}
 if (j==count)
 printf("抱歉!!!没有你所需要删除的学生信息!*_*!\n");
}
void yanshi(char *p)  //延时函数的定义
{
 while (1)
 {
 if (*p!=0)
 printf("%c",*p++);
 else
 break;
 Sleep(100);  //延时控制间断语句
 }
}
void filewrite(struct student stu[M])   //写入文件函数定义
{
 int j=0;
 char c;
 FILE *fp;
 printf("请选择是否要存入已输入的学生信息:'y'还是'n'???\n");
 scanf("%c",&c);
 fflush(stdin);
 while(c!='y'&&c!='n'){
 if (c!='y'&&c!='n')
 printf("输入错误,请重新输入\n");
 printf("以下操作将会覆盖已存储的数据,确定请输入'y'或'n'???\n");
 scanf("%c",&c);
 fflush(stdin);
 }
 if (c=='y')
 {
 if((fp=fopen("d:\\stu.dat","wb"))==NULL)
 {
 printf("文件打开错误,程序无法进行\n");
 exit(0);
 }
 for(j=0;j<count;j++)
 {fwrite(&stu[j],sizeof(struct student),1,fp);
 }
 fclose(fp);
 if(count==0)
 printf("没有文件,无法保存\n");
 else
 printf("数据存储完毕\n");
 system("pause");
 }
 else
 return;
}
void fileread(struct student stu[M])   //读取文件信息函数定义
{
 int j=0;
 char c;
 FILE *fp;
 system("cls");
 printf("请选择是否要存入已输入的学生信息:'y'还是'n'???\n");
 scanf("%c",&c);
 fflush(stdin);
 while(c!='y'&&c!='n'){
 if (c!='y'&&c!='n')
 printf("输入错误,请重新输入\n");
 printf("以下操作将会覆盖已存储的数据,确定请输入'y'或'n'???\n");
 scanf("%c",&c);
 fflush(stdin);
 }
 if (c=='y')
 {
 if((fp=fopen("d:\\stu.dat","rb"))==NULL)
 {
 printf("文件打开错误,程序无法进行\n");
 exit(0);
 }
 fread(&stu[j],sizeof(struct student),1,fp);
 count=0;
 count++;
 j++;
 while(fread(&stu[j],sizeof(struct student),1,fp))
 {
 j++;
 count++;
 }
 fclose(fp);
 printf("数据读取完毕!!!\n");
 system("pause");
 }
 else
 return;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/huangqiang1363/article/details/41594203