鱼C论坛

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

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

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

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

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

x
用C语言编写一个随机点名系统,运行该系统后,按空格键可以显示出全班任意一位同学。若该系统花名册没有该同学名字,也可以加入进系统花名册。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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


typedef struct STUDENT
{
    int ID;
    char stname[16];
    STUDENT *next;
}Student,*pStudent;



int OutrndID(int stsum);
int CallID(int stsum);
pStudent Outst(int ID);


pStudent Outst(int ID)
{
        pStudent tmp=firstST.next;
        while ((ID!=tmp->ID))
        {
                tmp=tmp->next;
                if (NULL==tmp)break;
        }
        
        return tmp;
}
int OutrndID(int stsum)
{
      return (rand()%stsum)+1;
}
int CallID(int stsum)
{
    STUDENT firstST={0}; 
    int i=0,j=0;
        pStudent tmp=&firstST;
    for(i=1;i<=stsum;i++)
    {
      pStudent pst=(pStudent)malloc(sizeof(STUDENT));
          tmp->next=pst;
          pst->ID=i;
          for (j = 0; j < sizeof(pst->stname); j++)
          {
                  pst->stname[j]='\0';
          }
          pst->next=NULL;
          tmp=pst;
     }
     srand((unsigned int)(time(NULL)));
         char inputch=0;
         
         while (1)
         {
                 printf("输入空格:");
                 //scanf("%c",&inputch);
                 inputch=_getch();
                 printf("\n");
                 if (0x20!=inputch)
                 {
                         printf("是否退出(Y/N)?");
                         inputch=getchar();
                         if (('Y'==inputch)||('y'==inputch))
                         {
                                 break;
                         }
                         continue;
                 }
                 if (NULL==(tmp=Outst(OutrndID(stsum))))
                 {
                         printf("没有找到!");
                 }
                 if (!tmp->stname[0])
                 {
                         printf("(学号:%d)没有录入名字,请录入:\n",tmp->ID);
                         scanf("%s",tmp->stname);
                         getchar();
                 }
                 else
                 {
                         printf("(学号:%d)姓名:%s\n",tmp->ID,tmp->stname);
                 }
         }
         if (NULL!=firstST.next)
        {
                tmp=firstST.next->next;
                free(firstST.next);
                firstST.next=tmp;
        }
         return 0;
}
int main ()
{
        CallID(5);
        
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

#define Students_init   5 //学生人数 

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

int mian(void)
{
        int com = 0;
        while(1)
        { 
                printf("请选择功能:“1”为点名,“2”为添加学生姓名,“3”为退出系统");
                scanf("%d",&com) ;
                
                if(com == 1)
                        check_name();
                else if(com == 2)
                        add_name();
                else
                        break; 
        }
        
        return 0;
} 

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

void add_name(void)
{        
        printf("请输入需要添加的姓名:");
        scanf("%s",name[Students_num]) ;
        Students_num++;
        printf("添加成功"); 
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-22 23:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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