鱼C论坛

 找回密码
 立即注册
查看: 2478|回复: 2

用C语言编写一个随机点名系统

[复制链接]
发表于 2021-11-11 22:07:39 | 显示全部楼层 |阅读模式

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

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

x
用C语言编写一个随机点名系统,运行该系统后,按空格键可以显示出全班任意一位同学。若该系统花名册没有该同学名字,也可以加入进系统花名册。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-11-12 12:17:31 | 显示全部楼层
本帖最后由 jhq999 于 2021-11-12 12:28 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <conio.h>


  5. typedef struct STUDENT
  6. {
  7.     int ID;
  8.     char stname[16];
  9.     STUDENT *next;
  10. }Student,*pStudent;



  11. int OutrndID(int stsum);
  12. int CallID(int stsum);
  13. pStudent Outst(int ID);


  14. pStudent Outst(int ID)
  15. {
  16.         pStudent tmp=firstST.next;
  17.         while ((ID!=tmp->ID))
  18.         {
  19.                 tmp=tmp->next;
  20.                 if (NULL==tmp)break;
  21.         }
  22.        
  23.         return tmp;
  24. }
  25. int OutrndID(int stsum)
  26. {
  27.       return (rand()%stsum)+1;
  28. }
  29. int CallID(int stsum)
  30. {
  31.     STUDENT firstST={0};
  32.     int i=0,j=0;
  33.         pStudent tmp=&firstST;
  34.     for(i=1;i<=stsum;i++)
  35.     {
  36.       pStudent pst=(pStudent)malloc(sizeof(STUDENT));
  37.           tmp->next=pst;
  38.           pst->ID=i;
  39.           for (j = 0; j < sizeof(pst->stname); j++)
  40.           {
  41.                   pst->stname[j]='\0';
  42.           }
  43.           pst->next=NULL;
  44.           tmp=pst;
  45.      }
  46.      srand((unsigned int)(time(NULL)));
  47.          char inputch=0;
  48.          
  49.          while (1)
  50.          {
  51.                  printf("输入空格:");
  52.                  //scanf("%c",&inputch);
  53.                  inputch=_getch();
  54.                  printf("\n");
  55.                  if (0x20!=inputch)
  56.                  {
  57.                          printf("是否退出(Y/N)?");
  58.                          inputch=getchar();
  59.                          if (('Y'==inputch)||('y'==inputch))
  60.                          {
  61.                                  break;
  62.                          }
  63.                          continue;
  64.                  }
  65.                  if (NULL==(tmp=Outst(OutrndID(stsum))))
  66.                  {
  67.                          printf("没有找到!");
  68.                  }
  69.                  if (!tmp->stname[0])
  70.                  {
  71.                          printf("(学号:%d)没有录入名字,请录入:\n",tmp->ID);
  72.                          scanf("%s",tmp->stname);
  73.                          getchar();
  74.                  }
  75.                  else
  76.                  {
  77.                          printf("(学号:%d)姓名:%s\n",tmp->ID,tmp->stname);
  78.                  }
  79.          }
  80.          if (NULL!=firstST.next)
  81.         {
  82.                 tmp=firstST.next->next;
  83.                 free(firstST.next);
  84.                 firstST.next=tmp;
  85.         }
  86.          return 0;
  87. }
  88. int main ()
  89. {
  90.         CallID(5);
  91.        
  92.         return 0;
  93. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-12 12:49:49 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<time.h>
  3. #include <stdlib.h>

  4. #define Students_init   5 //学生人数

  5. int  Students_num = Students_init;
  6. char *name[1024] = {"学生1","学生2","学生3","学生4","学生5"};
  7. void check_name(void);
  8. void add_name(void);

  9. int mian(void)
  10. {
  11.         int com = 0;
  12.         while(1)
  13.         {
  14.                 printf("请选择功能:“1”为点名,“2”为添加学生姓名,“3”为退出系统");
  15.                 scanf("%d",&com) ;
  16.                
  17.                 if(com == 1)
  18.                         check_name();
  19.                 else if(com == 2)
  20.                         add_name();
  21.                 else
  22.                         break;
  23.         }
  24.        
  25.         return 0;
  26. }

  27. void check_name(void)
  28. {
  29.         time_t t;
  30.         unsigned int i;
  31.        
  32.         srand((unsigned)time(&t));    //// 使用当前时间值初始化伪随机数种子序列
  33.         i = rand() %  Students_num;
  34.        
  35.         printf("%s",name[i]);
  36. }

  37. void add_name(void)
  38. {       
  39.         printf("请输入需要添加的姓名:");
  40.         scanf("%s",name[Students_num]) ;
  41.         Students_num++;
  42.         printf("添加成功");
  43. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 17:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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