抽簽小程序(C語言隨機數)


最近班級里需要人員抽簽參加活動,閑來無事用java的(Math.random()方法||java.util.Random())寫了一個隨機抽簽的,所以我又了解了一下C語言的隨機數獲取。

C語言的隨機數獲取:
參考:http://c.biancheng.net/view/2043.html
程序的抽簽信息源文件可以自己通過程序去做出來。。。咳咳,雖然方法很笨…
程序中有些函數注釋掉了,還有的隱藏了,就是制作源信息文件的。
你們可以改進改進,我這個寫的比較亂。。。
還有就是因為我不太喜歡參加活動,所以在隨機數函數里是輸出不出來46號的。。
具體程序代碼

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<windows.h>
#include<string.h>
#define COUNT 48 //總人數
//#define STU 20 //抽取學生人數 -為用戶自定義人數,所以不啟用 
//#define INITCOUNT 100 順序表初始化量 
//#define INCREMENT 10 順序表增量 
typedef struct
{
	char no[15];//學號
	char name[10];//姓名 
}student;
//-----------------------------------
FILE *stu,*lu;//定義學生信息源文件指針,幸運兒文件指針 
char *pno="學號",*pname="姓名",*pflag="編號";//字符串指針
int n;//抽取幸運兒數量 
//-----------------------------------
/*typedef struct { student *data;//基地址 int length;//長度 }StuList; 順序表-暫不啟用 */ 
//----函數聲明---
void MENU();//菜單 
unsigned int RandomNum();//隨機數,Seed為time,單位為秒 
void DivCount(int *n);//用戶自定義抽取人數
void TestInput(student *p);//測試輸入
int WriteLuckyVisible(student *p,int *Lucky);//寫入幸運兒txt文件,用戶可視化的
int ReadStu(student *p); //讀取信息源文件 
int WriteStuInvisible(student *p);//制作信息源文件時使用
int ReadStuVisible(student *p);//制作讀取數據源信息時使用 
//--------------
void main()
{
	//-----變量定義----------
	 
	int choose,i,j;
	int Lucky[COUNT];//幸運兒 
	student data[COUNT];
	//----------------------
	DivCount(&n); 
	//ReadStuVisible(data);制作完畢-暫不啟用 
	ReadStu(data);
	for(;;)
	{
	MENU();
	scanf("%d",&choose); 
	switch(choose)
	{
		case 1:
		do
		{
			printf("--抽取中--\n");
			printf("計數 丨%s丨%s\t %s\n",pflag,pno,pname);
			for(i=0;i<n;i++)
			{
				 
				Lucky[i]=RandomNum();
				for(j=0;j<i;j++)//---尋找重復項 
				{
					
					if(Lucky[j]==Lucky[i])
						while(Lucky[i]==Lucky[j])
						{
							Lucky[i]=RandomNum();
						}
							 
					else
						continue;
				}
				
			
				//printf("%d\n",Lucky[i]);隨機數重復測試 
				printf("第%-3d. %-4d% -13s %-5s\n",i+1,Lucky[i],data[Lucky[i]-1].no,data[Lucky[i]-1].name);//下標從0開始 
			}
			
				
			//------------------------------
			printf("【1.重新抽取丨2.輸出至TXT文件丨】:");
			scanf("%d",&choose); 
			switch(choose)
			{
				case 1:;break;
				case 2:
				if((WriteLuckyVisible(data,Lucky)==1))
					printf("輸出成功!\n");
				else
					printf("錯誤!\n"); 
				;break;
				
				default:printf("輸入錯誤!\n");
			} 
			
		}while(choose==1);
		;break;
		case 2:exit(0);break;
		case 8:printf("%s\t%s",data[0].no,data[0].name);break; 
		case 9:
		//TestInput(data);
		WriteStuInvisible(data);break;
		default:printf("\t\t\t\t\t輸入錯誤!");break;
	}
	system("pause"); 
	}
	
	
}
int ReadStuVisible(student *p)
{
	int i;
	if((stu=fopen("002.txt","r"))==NULL)
	return 0;
	for(i=0;i<COUNT;i++)
	fscanf(stu,"%s %s",(p+i)->name,(p+i)->no);
	fclose(stu);
	return 1;
}
int ReadStu(student *p)
{
	if((stu=fopen("Source.data","r"))==NULL)
		return 0;
	fread(p,sizeof(student),COUNT,stu);
	fclose(stu);
	return 1;
}
int WriteStuInvisible(student *p)
{
	if((stu=fopen("Source.data","wt+"))==NULL)
		return 0;
		fwrite(p,sizeof(student),COUNT,stu);
	fclose(stu);
		return 1;
}
int WriteLuckyVisible(student *p,int *Lucky)
{
	int i;
	if((lu=fopen("LuckyDog.txt","wt+"))==NULL)
		return 0;

	fprintf(lu,"計數 丨%s丨%s\t %s\n",pflag,pno,pname);
	for(i=0;i<n;i++)
	{
		fprintf(lu,"第%-4d. %-4d% -13s %-5s\n",i+1,Lucky[i],p[Lucky[i]-1].no,p[Lucky[i]-1].name);
	}
	fclose(lu);
	return 1;
}
void TestInput(student *p)
{
	int i;
	for(i=0;i<COUNT;i++)
	{
		system("cls");
		printf("----No.%d----\n",i+1);
		printf("學號:");
		scanf("%s",(p+i)->no);
		printf("姓名;");
		scanf("%s",(p+i)->name);
		
	}
}
void DivCount(int *n)
{
	for(;;)
	{
	system("cls");
	printf("\t\t\t\t請輸入本次准備抽取人數:");
	scanf("%d",n);
	if(*n<1||*n>COUNT)
	{
		printf("\t\t\t\t抽取人數不合法!");
		system("pause");
		continue; 
	}
	else
		break; 
		
	}

		
}
unsigned int RandomNum()
{
	unsigned int x;
	srand(time(NULL));
	for(;;)
	{
		Sleep(1000);
		x=rand()%COUNT+1;
		if(x!=46)
			break;
		else
			continue;
	}
	
	return x;
}
void MENU()
{
	system("cls");
	printf("\t\t\t\t --------------------\n");
	printf("\t\t\t\t丨 1.抽取 2.結束 丨\n");
	printf("\t\t\t\t --------------------\n");
	printf("\t\t\t\t 請輸入:");
} 

呃呃,自己預想的好多功能還沒有實現,而且抽簽的實現方法還有點復雜。。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM