日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

抽签小程序(C语言随机数)

發(fā)布時(shí)間:2024/5/24 综合教程 46 生活家
生活随笔 收集整理的這篇文章主要介紹了 抽签小程序(C语言随机数) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

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

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<windows.h>
#include<string.h>
#define COUNT 48 //總?cè)藬?shù)
//#define STU 20   //抽取學(xué)生人數(shù) -為用戶自定義人數(shù),所以不啟用 
//#define INITCOUNT 100 順序表初始化量 
//#define INCREMENT 10  順序表增量 
typedef struct
{
	char no[15];//學(xué)號
	char name[10];//姓名 
}student;
//-----------------------------------
FILE *stu,*lu;//定義學(xué)生信息源文件指針,幸運(yùn)兒文件指針 
char *pno="學(xué)號",*pname="姓名",*pflag="編號";//字符串指針
int n;//抽取幸運(yùn)兒數(shù)量 
//-----------------------------------
/*typedef struct
{
	student *data;//基地址
	int length;//長度 
}StuList; 順序表-暫不啟用 
*/ 
//----函數(shù)聲明---
void MENU();//菜單 
unsigned int RandomNum();//隨機(jī)數(shù),Seed為time,單位為秒 
void DivCount(int *n);//用戶自定義抽取人數(shù)
void TestInput(student *p);//測試輸入
int WriteLuckyVisible(student *p,int *Lucky);//寫入幸運(yùn)兒txt文件,用戶可視化的
int ReadStu(student *p); //讀取信息源文件 
int WriteStuInvisible(student *p);//制作信息源文件時(shí)使用
int ReadStuVisible(student *p);//制作讀取數(shù)據(jù)源信息時(shí)使用 
//--------------
void main()
{
	//-----變量定義----------
	 
	int choose,i,j;
	int Lucky[COUNT];//幸運(yùn)兒 
	student data[COUNT];
	//----------------------
	DivCount(&n); 
	//ReadStuVisible(data);制作完畢-暫不啟用 
	ReadStu(data);
	for(;;)
	{
	MENU();
	scanf("%d",&choose); 
	switch(choose)
	{
		case 1:
		do
		{
			printf("--抽取中--
");
			printf("計(jì)數(shù) 丨%s丨%s	  %s
",pflag,pno,pname);
			for(i=0;i<n;i++)
			{
				 
				Lucky[i]=RandomNum();
				for(j=0;j<i;j++)//---尋找重復(fù)項(xiàng) 
				{
					
					if(Lucky[j]==Lucky[i])
						while(Lucky[i]==Lucky[j])
						{
							Lucky[i]=RandomNum();
						}
							 
					else
						continue;
				}
				
			
				//printf("%d
",Lucky[i]);隨機(jī)數(shù)重復(fù)測試 
				printf("第%-3d. %-4d% -13s %-5s
",i+1,Lucky[i],data[Lucky[i]-1].no,data[Lucky[i]-1].name);//下標(biāo)從0開始 
			}
			
				
			//------------------------------
			printf("【1.重新抽取丨2.輸出至TXT文件丨】:");
			scanf("%d",&choose); 
			switch(choose)
			{
				case 1:;break;
				case 2:
				if((WriteLuckyVisible(data,Lucky)==1))
					printf("輸出成功!
");
				else
					printf("錯(cuò)誤!
"); 
				;break;
				
				default:printf("輸入錯(cuò)誤!
");
			} 
			
		}while(choose==1);
		;break;
		case 2:exit(0);break;
		case 8:printf("%s	%s",data[0].no,data[0].name);break; 
		case 9:
		//TestInput(data);
		WriteStuInvisible(data);break;
		default:printf("					輸入錯(cuò)誤!");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,"計(jì)數(shù) 丨%s丨%s	           %s
",pflag,pno,pname);
	for(i=0;i<n;i++)
	{
		fprintf(lu,"第%-4d.    %-4d% -13s %-5s
",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----
",i+1);
		printf("學(xué)號:");
		scanf("%s",(p+i)->no);
		printf("姓名;");
		scanf("%s",(p+i)->name);
		
	}
}
void DivCount(int *n)
{
	for(;;)
	{
	system("cls");
	printf("				請輸入本次準(zhǔn)備抽取人數(shù):");
	scanf("%d",n);
	if(*n<1||*n>COUNT)
	{
		printf("				抽取人數(shù)不合法!");
		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("				 --------------------
");
	printf("				丨 1.抽取    2.結(jié)束 丨
");
	printf("				 --------------------
");
	printf("				 請輸入:");
} 

呃呃,自己預(yù)想的好多功能還沒有實(shí)現(xiàn),而且抽簽的實(shí)現(xiàn)方法還有點(diǎn)復(fù)雜。。。

總結(jié)

以上是生活随笔為你收集整理的抽签小程序(C语言随机数)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。