鱼C论坛

 找回密码
 立即注册
查看: 2939|回复: 8

[技术交流] 学生成绩管理系统

[复制链接]
发表于 2016-6-11 22:19:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
花了两天时间终于把系统完善啦~~~至今写过的最长代码
感谢小甲鱼老师的<C语言程序设计>:057第十章 结构体与共用体05(新版).mp4
努力学习! 热爱鱼C^_^~~~如有BUG,还望大牛指正!

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include<windows.h>                // Sleep函数

  5. #define LEN sizeof(struct student)                // student结构的大小

  6. void Commands_menu(void);
  7. struct student *Create(struct student *head);
  8. struct student *Insert(struct student *head, struct student *stu_2);
  9. struct student *Del(struct student *head);
  10. void Print(struct student *head);
  11. void Quit(void);
  12. void SlowDisplay(char *p);

  13. struct student
  14. {
  15.         int num;
  16.         float score;
  17.         struct student *next;
  18. };

  19. int n=0;                // 链表节点数
  20. int choose_number;        // 命令选择指向默认
  21. int is_num;                // 检查输入是否为数字
  22. char proof;                // 判断 Y/N

  23. int main()
  24. {

  25.         SlowDisplay("\n\t学 生 成 绩 管 理 系 统\n");

  26.         Commands_menu();

  27.         return 0;
  28. }

  29. //*******************************命令菜单*******************************
  30. void Commands_menu(void)
  31. {
  32.         struct student *head=NULL, stu_2;

  33.         while (1)
  34.         {
  35.                 puts("\n************************************************************************");
  36.                 printf("****\t1 : 创建数据\t< 友情提醒: 执行'创建数据'命令会覆盖旧数据库! >\n");
  37.                 printf("****\t2 : 插入数据\n");
  38.                 printf("****\t3 : 删除数据\n");
  39.                 printf("****\t4 : 打印数据\n");
  40.                 printf("****\t0 : 退出!\n");
  41.                 puts("************************************************************************");

  42.                 SlowDisplay("\n请输入执行命令 : ");

  43.                 choose_number = 9;
  44.                 switch(scanf("%d", &choose_number), choose_number)
  45.                 {
  46.                 case 1:
  47.                         // 一次过滤所有非法字符输入
  48.                         while(getchar() != '\n')
  49.                         {
  50.                                 ;
  51.                         }
  52.                         SlowDisplay("\n确定创建新数据库, 覆盖旧数据库? < Y / N > : ");
  53.             proof = getchar();
  54.             if(proof == 'y' || proof == 'Y')
  55.             {
  56.                 SlowDisplay("\n数据库重建中...\t< 学号、成绩输入 '0' 则退出! >\n");
  57.                                 head = Create(head);
  58.             }
  59.             else if(proof == 'n' || proof == 'N')
  60.                         {
  61.                                 SlowDisplay("\n取消创建! 继续!\n");
  62.             }
  63.                         else
  64.                         {
  65.                                 SlowDisplay("\n创建失败! 输入非法!\n");
  66.                         }
  67.                         Print(head);
  68.                         break;

  69.                 case 2:
  70.                         head = Insert(head, &stu_2);
  71.                         Print(head);
  72.                         break;

  73.                 case 3:
  74.                         head = Del(head);
  75.                         Print(head);
  76.                         break;

  77.                 case 4:
  78.                         Print(head);
  79.                         break;

  80.                 case 0:
  81.                         Quit();
  82.                         break;

  83.                 default:
  84.                         SlowDisplay("\n输入非法!\n\n");
  85.                 }

  86.                 // 一次过滤所有非法字符输入
  87.                 while(getchar() != '\n')
  88.                 {
  89.                         ;
  90.                 }
  91.         }
  92. }

  93. //*******************************创建数据*******************************

  94. struct student *Create(struct student *head)
  95. {
  96.         struct student *p1, *p2;

  97.         n = 0;
  98.         head = NULL;
  99.         p1 = p2 = (struct student *)malloc(LEN);

  100.         printf("\n\t录入第 %d 位学生学号 : ", n+1);
  101.         // 检查非法输入
  102.         while(!(is_num = scanf("%d", &p1->num)))
  103.         {
  104.                 printf("\n\t请重新输入学生学号(纯数字) : ");
  105.                 while(getchar() != '\n')
  106.                 {
  107.                         ;
  108.                 }
  109.         }
  110.         if(p1->num == 0)
  111.         {
  112.                 SlowDisplay("\n结束创建!\n");
  113.                 return head;
  114.         }
  115.         printf("\n\t录入第 %d 位学生成绩 : ", n+1);
  116.         // 检查非法输入
  117.         while(!(is_num = scanf("%f", &p1->score)) || p1->score < 0 || p1->score >100)
  118.         {
  119.                 printf("\n\t请重新输入学生成绩(0~100的数字) : ");
  120.                 while(getchar() != '\n')
  121.                 {
  122.                         ;
  123.                 }
  124.         }
  125.        
  126.         while (p1->num)
  127.         {
  128.                 n++;
  129.                 if (1 == n)
  130.                 {
  131.                         head = p1;
  132.                 }
  133.                 else
  134.                 {
  135.                         p2->next = p1;
  136.                 }

  137.                 p2 = p1;

  138.                 p1 = (struct student *)malloc(LEN);

  139.                 getchar();
  140.                 printf("\n\t请继续录入第 %d 位学生学号 : ", n+1);
  141.                 // 检查非法输入
  142.                 while(!(is_num = scanf("%d", &p1->num)))
  143.                 {
  144.                         printf("\n\t请重新输入学生学号(纯数字) : ");
  145.                         while(getchar() != '\n')
  146.                         {
  147.                                 ;
  148.                         }
  149.                 }
  150.                 if(p1->num == 0)
  151.                 {
  152.                         SlowDisplay("\n结束创建!\n");
  153.                
  154.                         p2->next = NULL;

  155.                         return head;
  156.                 }
  157.                 printf("\n\t请继续录入第 %d 位学生成绩 : ", n+1);
  158.                 // 检查非法输入
  159.                 while(!(is_num = scanf("%f", &p1->score)) || p1->score < 0 || p1->score >100)
  160.                 {
  161.                         printf("\n\t请重新输入学生成绩(0~100的数字) : ");
  162.                         while(getchar() != '\n')
  163.                         {
  164.                                 ;
  165.                         }
  166.                 }
  167.         }

  168.         p2->next = NULL;

  169.         SlowDisplay("\n创建成功!\n");

  170.         return head;
  171. }

  172. //*******************************插入数据*******************************
  173. struct student *Insert(struct student *head, struct student *stu_2)
  174. {
  175.         struct student *p1, *p2;

  176.         stu_2 = (struct student *)malloc(LEN);

  177.         printf("\n请录入要插入学生学号(输入'0'取消) : ");
  178.         // 检查非法输入
  179.         while(!(is_num = scanf("%d", &stu_2->num)))
  180.         {
  181.                 printf("\n\t请重新输入学生学号(纯数字) : ");
  182.                 while(getchar() != '\n')
  183.                 {
  184.                         ;
  185.                 }
  186.         }
  187.         if(stu_2->num == 0)
  188.         {
  189.                 SlowDisplay("\n取消插入!\n");
  190.                 return head;
  191.         }
  192.         printf("\n请录入要插入学生成绩 : ");
  193.         // 检查非法输入
  194.         while(!(is_num = scanf("%f", &stu_2->score)) || stu_2->score < 0 || stu_2->score > 100)
  195.         {
  196.                 printf("\n\t请重新输入学生成绩(0~100的数字) : ");
  197.                 while(getchar() != '\n')
  198.                 {
  199.                         ;
  200.                 }
  201.         }

  202.         p1 = head;

  203.         if(head == NULL)
  204.         {
  205.                 head = stu_2;
  206.                 stu_2->next = NULL;
  207.         }
  208.         else
  209.         {
  210.                 while (p1->next != NULL && stu_2->num > p1->num)
  211.                 {
  212.                         p2 = p1;
  213.                         p1 = p1->next;
  214.                 }

  215.                 if (stu_2->num <= p1->num)
  216.                 {
  217.                         if (head == p1)
  218.                         {
  219.                                 head = stu_2;
  220.                         }
  221.                         else
  222.                         {
  223.                                 p2->next = stu_2;
  224.                         }                       
  225.                         stu_2->next = p1;
  226.                 }
  227.                 else
  228.                 {
  229.                         p1->next = stu_2;
  230.                         stu_2->next = NULL;
  231.                 }
  232.         }

  233.         n++;

  234.         SlowDisplay("\n插入成功!");
  235.         printf("\n插入学号 : %d\n", stu_2->num);

  236.         return head;
  237. }

  238. //*******************************删除数据*******************************
  239. struct student *Del(struct student *head)
  240. {
  241.         struct student *p1, *p2;
  242.         int num;

  243.         getchar();
  244.         printf("\n请输入您想删除的学生学号(输入'0'取消) : ");
  245.         // 检查非法输入
  246.         while(!(is_num = scanf("%d", &num)))
  247.         {
  248.                 printf("\n\t请重新输入学生学号(纯数字) : ");
  249.                 while(getchar() != '\n')
  250.                 {
  251.                         ;
  252.                 }
  253.         }
  254.         if(num == 0)
  255.         {
  256.                 SlowDisplay("\n取消删除!\n");
  257.                 return head;
  258.         }

  259.         p1 = head;
  260.         if(head == NULL)
  261.         {
  262.                 SlowDisplay("\n删除失败!");
  263.                 printf("\n数据库为空!\n");
  264.         }
  265.         else
  266.         {
  267.                 while (p1->next != NULL && num != p1->num)
  268.                 {
  269.                         p2 = p1;
  270.                         p1 = p1->next;
  271.                 }
  272.                 if (num == p1->num)
  273.                 {
  274.                         if (p1 == head)
  275.                         {
  276.                                 head = p1->next;
  277.                         }
  278.                         else
  279.                         {
  280.                                 p2->next = p1->next;
  281.                         }

  282.                         SlowDisplay("\n删除成功!");
  283.                         printf("\n删除学号 : %d\n", num);
  284.                         n--;
  285.                 }
  286.                 else
  287.                 {
  288.                         SlowDisplay("\n删除失败!");
  289.                         printf("\n无匹配学号 : %d\n", num);
  290.                 }
  291.         }

  292.         return head;

  293. }

  294. //*******************************打印数据*******************************
  295. void Print(struct student *head)
  296. {
  297.         struct student *p;
  298.         SlowDisplay("\n数据库打印中...");
  299.         puts("\n************************************************************************");
  300.         printf("\n****\t数据库中共计有 %d 条记录!\n", n);

  301.         p = head;
  302.         while (p)
  303.         {
  304.                 printf("\n\t学号 : %d , 成绩 : %5.2f", p->num, p->score);
  305.                 p = p->next;
  306.         }
  307.         putchar('\n');
  308. }

  309. //*******************************退出*******************************
  310. void Quit(void)
  311. {
  312.         // 一次过滤所有非法字符输入
  313.         while(getchar() != '\n')
  314.         {
  315.                 ;
  316.         }
  317.         SlowDisplay("\n确定退出? < Y / N > : ");
  318.         proof = getchar();
  319.         if(proof == 'y' || proof == 'Y')
  320.         {
  321.                 SlowDisplay("\n数据库保存中...\n");
  322.                 // 执行数据库保存...
  323.                 SlowDisplay("\n * * * *\n");
  324.                 SlowDisplay("\n * * * *\n");
  325.                 SlowDisplay("\n * * * *\n");
  326.                 SlowDisplay("\n数据库已保存, 系统退出!\t");
  327.                 exit(0);
  328.         }
  329.         else if(proof == 'n' || proof == 'N')
  330.         {
  331.                 SlowDisplay("\n取消退出! 继续!\n");
  332.         }
  333.         else
  334.         {
  335.                 SlowDisplay("\n输入非法! 继续!\n");
  336.         }
  337. }

  338. //*******************************慢显*******************************
  339. void SlowDisplay(char *p)
  340. {
  341.     while(1)
  342.     {
  343.         if(*p!=0)
  344.             printf("%c",*p++);
  345.         else
  346.             break;
  347.         Sleep(50);
  348.     }
  349. }
复制代码

评分

参与人数 1荣誉 +5 鱼币 +10 贡献 +5 收起 理由
~风介~ + 5 + 10 + 5 感谢楼主无私奉献!

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-6-12 20:29:45 | 显示全部楼层
写得挺好的,代码清晰,赞一个!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-6-12 21:02:53 | 显示全部楼层
~风介~ 发表于 2016-6-12 20:29
写得挺好的,代码清晰,赞一个!

谢谢鼓励,向着大牛努力!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-6-12 21:07:27 | 显示全部楼层
~风介~ 发表于 2016-6-12 20:29
写得挺好的,代码清晰,赞一个!

谢谢鼓励,向着大牛努力!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-6-12 21:08:04 | 显示全部楼层
~风介~ 发表于 2016-6-12 20:29
写得挺好的,代码清晰,赞一个!

谢谢鼓励,向着大牛努力!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-6-16 12:38:14 | 显示全部楼层
看看,学习下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-6-29 18:58:02 | 显示全部楼层
代码简洁明了,谢谢你的分享
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-15 23:20:49 | 显示全部楼层
代码很酷酷~~!~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-12-30 17:26:14 | 显示全部楼层
哥们 厉害拉
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 05:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表