C语言:随机抽奖

#include <stdio.h>
#include <stdlib.h> //<stdlib.h>用于调用 rand(),
#include <time.h> //声明time 时间不可逆转一直在变
 #include <Windows.h>  //<Windows.h> 用于清屏
#include <conio.h>  //<conio.h>  用按键用的
#define MAX_NUM 9999
int main()
{
 FILE *fp = fopen("data.txt", "rb");

    char numa[20];
    
    
    while(!feof(fp))
   {
       fscanf(fp,"%d",&numa);
       printf("%d ",numa); 
   }
   fclose(fp);
int num;
srand((unsigned)time(0)); //rand是伪随机,所以先弄srand,才能是真的随机数
    while (1)
    {
        if (!_kbhit())
    { num = rand()%(999-100+1)+100; //rand()用法:rand()%(上限-下限+1)+下限

    printf("抽奖中....%d\n",num);
    Sleep(10);              //以毫秒计时
    system("cls");    }                           //system("cls")作用:清屏
    
    else break;        
    }
    printf("抽奖结果是:%d\n",num);
    return 0;
}

 

#include <stdio.h>
#include <stdlib.h> //<stdlib.h>用于调用 rand(),
#include <time.h> //声明time 时间不可逆转一直在变
 #include <Windows.h>  //<Windows.h> 用于清屏
#include <conio.h>  //<conio.h>  用按键用的
#define MAX_NUM 9999
int main()
{
    clock_t start,stop;
    typedef struct 
{
    int ida;
    char name[20];
    
}student;

    student st[MAX_NUM];

 FILE *fp = fopen("data1.txt", "rb");

    char numa[20];
    int aa=0;
    
    while(!feof(fp))
   {
       fscanf(fp,"%s",st[aa].name);
       st[aa].ida=aa;
       printf("%s %d\n",st[aa].name,st[aa].ida); 
       aa++;
       Sleep(10); 
   }
   aa=aa-2;
   Sleep(4000); 
   fclose(fp);
int num;
srand((unsigned)time(0)); //rand是伪随机,所以先弄srand,才能是真的随机数
start=clock();
    while (1)
    {
        
        stop=clock();
        printf("\n%d\n",stop-start);
        if (stop-start>5000) 
           break;
        if (!_kbhit())
        { num = rand()%(aa-0+1)+0; //rand()用法:rand()%(上限-下限+1)+下限
    
        printf("抽奖中....%s,%d\n",st[num].name,st[num].ida);
        Sleep(10);              //以毫秒计时
        //system("cls");   
         }                           //system("cls")作用:清屏
        
        else break;        
        }
    printf("抽奖结果是:%s,%d\n",st[num].name,st[num].ida);
    return 0;
}

 

posted @ 2021-06-16 07:21  myrj  阅读(468)  评论(0编辑  收藏  举报