学生信息管理系统

基于Linux系统下的学习

简单的学生信息管理系统:支持管理员和学生本人登录

学生登录:账号是学生的ID,密码是学生的名字;登录成功后会看到自己的信息

管理员登录:账号和密码都是admin,允许3次登录;登录成功后可对学生信息进行增、删、改、查等操作;学生信息保存在文件

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <unistd.h>
  4 #include <stdlib.h>
  5 typedef struct stu
  6 {
  7     char name[30];
  8     char class[30];
  9     int nId;
 10     double EnScore;
 11     double ChScore;
 12     double MathScore;
 13 }Stu;
 14 
 15 void Welcome();//欢迎界面
 16 void AdminMenu();//管理员登录界面
 17 int Login();//3次登录
 18 void Select();//各种功能选择
 19 void Insert(); //添加学生信息
 20 void ShowStu(); //显示学生信息
 21 void Delete();//删除学生信息
 22 void Modify();//修改学生信息
 23 void Query();//同过学号查询学生信息
 24 void Sort();//总分排序
 25 int FindBiaohao(int nID, int size);//通过学号返回标号
 26 void SmallMenu();
 27 void StuLogin();
 28 void ShowClass();
 29 
 30 Stu stu[100];
 31 
 32 int FindBiaohao(int n, int size)
 33 {
 34     int ret = -1;
 35     int i = 0;
 36     for(i = 0; i < size; i++)
 37     {
 38         if(stu[i].nId == n)
 39         {
 40             return i;
 41             //break;
 42         }
 43     }
 44     return ret;
 45 }
 46 
 47 void ShowClass()
 48 {
 49     int UnPassNum = 0;
 50     int ClassNum = 0;
 51     int GoodNum = 0;
 52     int PassNum = 0;
 53     double Sum = 0;
 54     double dblPass = 0;
 55     int i = 0;
 56     double dbl[100];
 57     char Cla[30];
 58     int nCount = 0;
 59     int nPtr = 0;
 60     //double dbl[100];
 61     FILE* fp = NULL;
 62     fp = fopen("stu.txt", "r");
 63     if(fp == NULL)
 64     {
 65         puts("文件打开失败!!!");
 66         return ;
 67     }
 68     fseek(fp, 0, SEEK_END);
 69     nPtr = ftell(fp);
 70     nCount = nPtr / sizeof(stu[0]);
 71     fseek(fp, 0, SEEK_SET);
 72     for(i = 0; i < nCount; i++)
 73     {
 74         fread(&stu[i], sizeof(stu[i]), 1, fp);
 75     }
 76     if(fp != NULL)
 77     {
 78         fclose(fp);
 79         fp = NULL;
 80     }
 81     printf("请输入你要查询的班级名:");
 82     scanf("%29s", Cla);
 83     int flag=0;
 84     for(i = 0; i < nCount; i++)
 85     {
 86         if(strcmp(Cla, stu[i].class) == 0)
 87         {
 88             flag=1;
 89             int j = 0;
 90             for(j = 0; j < nCount; j++)
 91             {
 92                 dbl[j] = stu[i].EnScore + stu[i].ChScore + stu[i].MathScore;
 93                 //printf("%lf  ", dbl[i]);
 94             }
 95             ClassNum++;
 96         }
 97     }
 98     if(flag==0)
 99     {
100         puts("查无此班!!!");
101         return;
102     }
103     for(i = 0; i < ClassNum; i++)
104     {
105         if(dbl[i] > 240)
106         {
107             GoodNum++;
108         }
109         if(dbl[i] > 180)
110         {
111             PassNum++;
112         }
113         else
114         {
115             UnPassNum++;
116         }
117         Sum += dbl[i];
118     }
119     dblPass = PassNum / ClassNum;
120     printf("班级报表信息如下:\r\n");
121     printf("班级人数:%d\n", ClassNum);
122     printf("班级总成绩:%6.2lf\n", Sum);
123     printf("优秀人数(总分240以上):%d\n", GoodNum);
124     printf("及格人数(总分180以上):%d\n", PassNum);
125     printf("不及格人数(总分180以下): %d\n", UnPassNum);
126     printf("班级及格率:%6.1lf\%\n", dblPass*100);
127 }
128 
129 //总分排序
130 void Sort()
131 {
132     int i = 0;
133     int nCount = 0;
134     int nPtr = 0;
135     double dbl[100];
136     FILE* fp = NULL;
137     fp = fopen("stu.txt", "r");
138     if(fp == NULL)
139     {
140         puts("文件打开失败!!!");
141         return ;
142     }
143     fseek(fp, 0, SEEK_END);
144     nPtr = ftell(fp);
145     nCount = nPtr / sizeof(stu[0]);
146     fseek(fp, 0, SEEK_SET);
147     for(i = 0; i < nCount; i++)
148     {
149         fread(&stu[i], sizeof(stu[i]), 1, fp);
150     }
151     if(fp != NULL)
152     {
153         fclose(fp);
154         fp = NULL;
155     }
156     for(i = 0; i < nCount; i++)
157     {
158         dbl[i] = stu[i].EnScore + stu[i].ChScore + stu[i].MathScore;
159     }
160     int j = 0;
161     Stu temp = {0};
162     double tmp = 0;
163     for(i = 0; i < nCount-1; i++)
164     {
165         for(j = 0;j < nCount - i - 1; j++)
166         {
167             if(dbl[j] < dbl[j+1])
168             {
169                 tmp = dbl[j];
170                 dbl[j] = dbl[j + 1];
171                 dbl[j+1] = tmp;
172                 temp = stu[j];
173                 stu[j] = stu[j+1];
174                 stu[j+1] = temp;
175             }
176         }
177     }
178 
179     for(i = 0; i < nCount; i++)
180     {
181         //fread(&stu[i], sizeof(stu[i]), 1, fp);
182         printf("姓名:%s  班级:%s  学号:%d  英语:%6.2lf  语文:%6.2lf  数学:%6.2lf  总分:%6.2lf 第%d名\r\n",
183                 stu[i].name, stu[i].class, stu[i].nId, 
184                 stu[i].EnScore, stu[i].ChScore, stu[i].MathScore,
185                 stu[i].EnScore+stu[i].ChScore+stu[i].MathScore, i+1);
186     }
187 
188 }
189 
190 //欢迎界面
191 void Welcome()
192 {
193     printf("*************************************\r\n");
194     printf("*                                   *\r\n");
195     printf("*            欢迎使用               *\r\n");
196     printf("*                                   *\r\n");
197     printf("*      学 生 信 息 管 理 系 统      *\r\n");
198     printf("*                                   *\r\n");
199     printf("*************************************\r\n");
200 }
201 
202 //管理员登录界面
203 void AdminMenu()
204 {
205     //printf("欢迎管理员admin登录系统!             \r\n");
206     printf("**************************************\r\n");
207     printf("*    ID编号    |        功能项       *\r\n");
208     printf("*      1       |     添加学员信息    *\r\n");
209     printf("*      2       |     删除学员信息    *\r\n");
210     printf("*      3       |     修改学员信息    *\r\n");
211     printf("*      4       |     查询学员信息    *\r\n");
212     printf("*      5       |     学员成绩排名    *\r\n");
213     printf("*      6       |     显示学员信息    *\r\n");
214     printf("*      7       |     退出学员系统    *\r\n");
215     printf("*      8       |     打印班级报表    *\r\n");
216     printf("**************************************\r\n");
217 }
218 
219 void StuLogin()
220 {
221     int i = 0;
222     int nCount = 0;
223     int nPtr = 0;
224     FILE* fp = NULL;
225     fp = fopen("stu.txt", "r");
226     if(fp == NULL)
227     {
228         puts("文件打开失败!!!");
229         return ;
230     }
231     fseek(fp, 0, SEEK_END);
232     nPtr = ftell(fp);
233     nCount = nPtr / sizeof(stu[0]);
234     fseek(fp, 0, SEEK_SET);
235     for(i = 0; i < nCount; i++)
236     {
237         fread(&stu[i], sizeof(stu[i]), 1, fp);
238     }
239     if(fp != NULL)
240     {
241         fclose(fp);
242         fp = NULL;
243     }
244     
245     int id = 0;
246     char na[30];
247     printf("请输入学生帐号和密码登录\r\n");
248     printf("请输入帐号:");
249     scanf("%d", &id);
250     printf("请输入密码:");
251     scanf("%29s", na);
252 
253     int nRet = FindBiaohao(id, nCount);
254     if(nRet == -1)
255     {
256         puts("登录失败!!!");
257     }
258     else
259     {
260         if(strcmp(na, stu[nRet].name) == 0)
261         {
262             printf("该学生信息:\n");
263             printf("姓名:%s  班级:%s  学号:%d  英语:%6.2lf  语文:%6.2lf  数学:%6.2lf\n",
264                 stu[nRet].name, stu[nRet].class, stu[nRet].nId, 
265                 stu[nRet].EnScore, stu[nRet].ChScore, stu[nRet].MathScore);
266         }
267         else
268         {
269             puts("登录失败!!!");
270         }
271     }
272 }
273 
274 //登录
275 int Login()
276 {
277     char strName[30];
278     char strPwd[30];
279     int i = 0;
280     puts("请输入帐号名和密码登录系统");
281     while(1)
282     {    
283         if(i == 3)
284         {
285             return 0;
286             break;
287         }
288         printf("请输入帐号名称:");
289         scanf("%29s", strName);
290         printf("请输入帐号密码:");
291         scanf("%29s", strPwd);
292         if(strcmp(strName, "admin") == 0 && strcmp(strPwd, "admin") == 0)
293         {
294             return 1;
295         }
296         else
297         {
298             i++;
299             sleep(1);
300         }
301     }
302 
303 }
304 
305 //各种功能操作
306 void Select()
307 {
308     int n = 0;
309     while(1)
310     {
311         AdminMenu();
312         puts("请选择你要执行操作对应的菜单编号:");
313         scanf("%d", &n);
314         switch(n)
315         {
316             case 1:
317                 printf("添加学员信息\r\n");
318                 Insert();
319                 //ShowStu();
320                 break;
321             case 2:
322                 printf("删除学员信息\r\n");
323                 Delete();
324                 //ShowStu();
325                 break;
326             case 3:
327                 printf("修改学员信息\r\n");
328                 Modify();
329                 //ShowStu();
330                 break;
331             case 4:
332                 printf("查询学员信息\r\n");
333                 Query();
334                 break;
335             case 5:
336                 printf("学员成绩排名\r\n");
337                 Sort();
338                 break;
339             case 6:
340                 printf("显示学员信息\r\n");
341                 ShowStu();
342                 break;
343             case 7:
344                 printf("保存信息中...\r\n");
345                 sleep(3);
346                 printf("退出系统\r\n");
347                 //getchar();
348                 exit(0);
349                 break;
350             case 8:
351                 printf("打印班级报表\n");
352                 ShowClass();
353                 break;
354         }
355         if( n < 1 && n > 8)
356         {
357             continue;
358         }
359     }
360 }
361 //添加学生信息
362 void Insert()
363 {
364     int nCount = 0;
365     int nPtr = 0;
366     FILE *fp = NULL;
367     fp = fopen("stu.txt", "a+");
368     if(fp == NULL)
369     {
370         puts("文件打开失败!!!");
371         return ;
372     }
373     fseek(fp, 0, SEEK_END);
374     nPtr = ftell(fp);
375     nCount = nPtr / sizeof(stu[0]);
376 
377     if(nCount == 0)
378     {
379         printf("输入学生姓名:");
380         scanf("%29s", stu[nCount].name);
381         scanf("%*[^\n]");
382         scanf("%*c");
383         printf("输入学生的班级:");
384         scanf("%29s", stu[nCount].class);
385         scanf("%*[^\n]");
386         scanf("%*c");
387         printf("输入学生的ID:");
388         scanf("%d", &stu[nCount].nId);
389         printf("请输入英语成绩:");
390         scanf("%lf", &stu[nCount].EnScore);
391         printf("请输入语文成绩:");
392         scanf("%lf", &stu[nCount].ChScore);
393         printf("请输入数学成绩:");
394         scanf("%lf", &stu[nCount].MathScore);
395 
396 
397     }
398     else
399     {
400         int i = 0;
401         for(i = 0; i < nCount; i++)
402         {
403             fread(&stu[i], sizeof(stu[i]), 1, fp);
404         }
405         printf("输入学生姓名:");
406         scanf("%29s", stu[nCount].name);
407         scanf("%*[^\n]");
408         scanf("%*c");
409         printf("输入学生的班级:");
410         scanf("%29s", stu[nCount].class);
411         scanf("%*[^\n]");
412         scanf("%*c");
413         while(1)
414         {
415             int nID = 0;
416             int flag = 0;
417             printf("输入学生的ID:");
418             scanf("%d", &nID);
419             for(i = 0; i < nCount; i++)
420             {
421                 if(stu[i].nId == nID)
422                 {
423                     break;
424                 }
425             }
426             if(i == nCount)
427             {
428                 stu[nCount].nId = nID;
429                 break;
430             }
431             printf("当前学号已存在,");
432             continue;
433 
434         }
435         printf("请输入英语成绩:");
436         scanf("%lf", &stu[nCount].EnScore);
437         printf("请输入语文成绩:");
438         scanf("%lf", &stu[nCount].ChScore);
439         printf("请输入数学成绩:");
440         scanf("%lf", &stu[nCount].MathScore);
441     }
442 
443     fwrite(&stu[nCount], sizeof(stu[nCount]), 1, fp);
444 
445     if(fp != NULL)
446     {
447         fclose(fp);
448         fp = NULL;
449     }
450 }
451 
452 //删除学生信息
453 void Delete()
454 {
455 
456     int i = 0;
457     int nCount = 0;
458     int nPtr = 0;
459     FILE* fp = NULL;
460     fp = fopen("stu.txt", "r");
461     if(fp == NULL)
462     {
463         puts("文件打开失败!!!");
464         return ;
465     }
466     fseek(fp, 0, SEEK_END);
467     nPtr = ftell(fp);
468     nCount = nPtr / sizeof(stu[0]);
469     fseek(fp, 0, SEEK_SET);
470     for(i = 0; i < nCount; i++)
471     {
472         fread(&stu[i], sizeof(stu[i]), 1, fp);
473     }
474     if(fp != NULL)
475     {
476         fclose(fp);
477         fp = NULL;
478     }
479 
480     int nID = 0;
481     printf("请输入你要删除学生的学号:");
482     scanf("%d", &nID);
483     int nRet = FindBiaohao(nID, nCount);
484     if(nRet == -1)
485     {
486         puts("查无此人!!!");
487         return;
488     }
489     else
490     {
491         char c = 0;
492         printf("姓名:%s  班级:%s  学号:%d  英语:%6.2lf  语文:%6.2lf  数学:%6.2lf\n",
493                 stu[nRet].name, stu[nRet].class, stu[nRet].nId, 
494                 stu[nRet].EnScore, stu[nRet].ChScore, stu[nRet].MathScore);
495         puts("确认删除吗?<y/n>");
496         getchar();
497         scanf("%c", &c);
498         getchar();
499         if(c != 'n')
500         {
501             int i = 0;
502             for(i = nRet; i < nCount-1; i++)
503             {
504                 stu[i] = stu[i+1];
505             }
506             nCount--;
507         }
508         else
509         {
510             return;
511         }
512     }
513 
514 
515     //FILE* fp = NULL;
516     fp = fopen("stu.txt", "w");
517     if(fp == NULL)
518     {
519         puts("打开文件失败!!!");
520         return ;
521     }
522 
523     for(i = 0; i < nCount; i++)
524     {
525         fwrite(&stu[i], sizeof(stu[i]), 1, fp);
526     }
527 
528     if(fp != NULL)
529     {
530         fclose(fp);
531         fp = NULL;
532     }
533 }
534 //修改学生信息
535 void Modify()
536 {
537     int i = 0;
538     int nCount = 0;
539     int nPtr = 0;
540     FILE* fp = NULL;
541     fp = fopen("stu.txt", "r");
542     if(fp == NULL)
543     {
544         puts("文件打开失败!!!");
545         return ;
546     }
547     fseek(fp, 0, SEEK_END);
548     nPtr = ftell(fp);
549     nCount = nPtr / sizeof(stu[0]);
550     fseek(fp, 0, SEEK_SET);
551     for(i = 0; i < nCount; i++)
552     {
553         fread(&stu[i], sizeof(stu[i]), 1, fp);
554     }
555     if(fp != NULL)
556     {
557         fclose(fp);
558         fp = NULL;
559     }
560 
561     int nID = 0;
562     printf("请输入你要修改学生的学号:");
563     scanf("%d", &nID);
564     int nRet = FindBiaohao(nID, nCount);
565     if(nRet == -1)
566     {
567         puts("查无此人!!!");
568         return;
569     }
570     else
571     {
572         char c = 0;
573         printf("姓名:%s  班级:%s  学号:%d  英语:%6.2lf  语文:%6.2lf  数学:%6.2lf\n",
574                 stu[nRet].name, stu[nRet].class, stu[nRet].nId, 
575                 stu[nRet].EnScore, stu[nRet].ChScore, stu[nRet].MathScore);
576         puts("确认修改吗?<y/n>");
577         getchar();
578         scanf("%c", &c);
579         getchar();
580         if(c != 'n')
581         {
582             int i = 0;
583             for(i = nRet; i < nCount-1; i++)
584             {
585                 stu[i] = stu[i+1];
586             }
587             nCount--;
588         }
589         else
590         {
591             return;
592         }
593     }
594 
595 
596     //FILE* fp = NULL;
597     fp = fopen("stu.txt", "w");
598     if(fp == NULL)
599     {
600         puts("打开文件失败!!!");
601         return ;
602     }
603 
604     for(i = 0; i < nCount; i++)
605     {
606         fwrite(&stu[i], sizeof(stu[i]), 1, fp);
607     }
608 
609     if(fp != NULL)
610     {
611         fclose(fp);
612         fp = NULL;
613     }
614 
615     Insert();
616 }
617 
618 //查询学生信息
619 void Query()
620 {
621     int i = 0;
622     int nCount = 0;
623     int nPtr = 0;
624     FILE* fp = NULL;
625     fp = fopen("stu.txt", "r");
626     if(fp == NULL)
627     {
628         puts("文件打开失败!!!");
629         return ;
630     }
631     fseek(fp, 0, SEEK_END);
632     nPtr = ftell(fp);
633     nCount = nPtr / sizeof(stu[0]);
634     fseek(fp, 0, SEEK_SET);
635     for(i = 0; i < nCount; i++)
636     {
637         fread(&stu[i], sizeof(stu[i]), 1, fp);
638     }
639     if(fp != NULL)
640     {
641         fclose(fp);
642         fp = NULL;
643     }
644 
645     int nID = 0;
646     printf("请输入你要查询学生的学号:");
647     scanf("%d", &nID);
648     int nRet = FindBiaohao(nID, nCount);
649     if(nRet == -1)
650     {
651         puts("查无此人!!!");
652     }
653     else
654     {
655         char c = 0;
656         printf("姓名:%s  班级:%s  学号:%d  英语:%6.2lf  语文:%6.2lf  数学:%6.2lf\n",
657                 stu[nRet].name, stu[nRet].class, stu[nRet].nId, 
658                 stu[nRet].EnScore, stu[nRet].ChScore, stu[nRet].MathScore);
659     }
660 }
661 
662 
663 //显示学生信息
664 void ShowStu()
665 {
666     int i = 0;
667     int nCount = 0;
668     int nPtr = 0;
669     FILE* fp = NULL;
670     fp = fopen("stu.txt", "r");
671     if(fp == NULL)
672     {
673         puts("文件打开失败!!!");
674         return ;
675     }
676     fseek(fp, 0, SEEK_END);
677     nPtr = ftell(fp);
678     nCount = nPtr / sizeof(stu[0]);
679     fseek(fp, 0, SEEK_SET);
680     for(i = 0; i < nCount; i++)
681     {
682         fread(&stu[i], sizeof(stu[i]), 1, fp);
683         printf("姓名:%s  班级:%s  学号:%d  英语:%6.2lf  语文:%6.2lf  数学:%6.2lf\n",
684                 stu[i].name, stu[i].class, stu[i].nId, 
685                 stu[i].EnScore, stu[i].ChScore, stu[i].MathScore);
686     }
687     //puts("");
688 
689     if(fp != NULL)
690     {
691         fclose(fp);
692         fp = NULL;
693     }
694 }
695 
696 void SmallMenu()
697 {
698     int n = 0;
699     while(1)
700     {
701         //AdminMenu();
702         puts("");
703         printf("*******************************\r\n");
704         puts("");
705         printf("******  1.管理员登录  *********\r\n");
706         puts("");
707         printf("******  2.学生登录    *********\r\n");
708         puts("");
709         printf("******  3.退出系统    *********\r\n");
710         puts("");
711         printf("*******************************\r\n");
712         
713         puts("请选择你要执行操作对应的菜单编号:");
714         scanf("%d", &n);
715         switch(n)
716         {
717             case 1:
718                 printf("管理员登录\r\n");
719                 int nRet = Login();
720                 if(nRet)
721                 {
722                     while(1)
723                     {
724                         //AdminMenu();
725 
726                         //puts("请选择你要执行操作对应的菜单编号:");
727                         printf("欢迎管理员admin登录系统!\r\n");
728                         Select();
729                     }
730                 }
731                 else
732                 {
733                     printf("Fuck You!\r\n");
734                     puts("你的帐号和密码错误,请联系管理员");
735                 }
736             break;
737 
738             case 2:
739             printf("学生登录:\r\n");
740             StuLogin();
741             break;
742 
743             case 3:
744             puts("退出系统");
745             exit(0);
746             break;
747                 
748         }
749         if( n < 1 && n > 3)
750         {
751             continue;
752         }
753     }
754 }
755 
756 int main()
757 {
758     Welcome();
759     sleep(2);
760     SmallMenu();
761     return 0;
762 }

 

posted on 2018-07-29 14:58  秋雨丶梧桐  阅读(2130)  评论(0编辑  收藏  举报

导航