[Linux C]双色球计算程序

开年会部门每人派发一张双色球彩票,算是年会的小礼品。

拿到手一看每张都连号连得很厉害,基本上都像这样1、2、3、4、5、8,10...购买彩票的人也太懒了吧,机选也好呀,手填还填成这样。

在一个满是程序员的部门里这种情况实在太让人感到无语了,于是本人自己突发奇想写了一个Linux环境下的C语言双色球的随机算号程序...随便玩玩。


/* 备注:恭喜发财!中大奖!

规则:
1. 6个红色球号码 + 1个蓝色球号码
2. 红色球号码范围 :1-33
   蓝色球号码范围:1-16
3. 红色球号码不会重复

算法:
1. 红球三连号或以上的概率极小,可以考虑剔除。例如:23、24、25等等...
2. 所有红球均在同一区段的概率极小,亦可考虑剔除。(暂不处理)
	红球的号码可分为三个区段:
	区段一:1-11
	区段二:12-22
	区段三:23-33
*/

#include 
#include 
#include 
#include 

#define MAX_SUCCESSIVE	3

#define RED_BALL_QUANTITY	6
#define BLUE_BALL_QUANTITY	1

#define RED_BALL_MAX	33
#define BLUE_BALL_MAX	16

#define INT_ARRAY_SIZE(array) (sizeof(array) / sizeof(int))

#define DEBUG 0

#define DEBUG_MSG(m) do{\
	if(DEBUG)			\
		printf(m);		\
}while(0)


typedef struct Str_DoubleBall_Group{
	int RedBall_IdGroup[RED_BALL_QUANTITY];
	int BlueBall_Id;
}Str_DoubleBall_Group;

#if 0
typedef struct Str_DoubleBall_Linklist{
	struct Str_DoubleBall_Group Str_DBG;
	struct Str_DoubleBall_Linklist *p_prev;
	struct Str_DoubleBall_Linklist *p_next;	
}Str_DoubleBall_Linklist;
#endif

/**** 函数声明 ****/

static inline void Print_Int_Array(int *P_array,int size);
static inline int Create_RandomId(void);
static inline int Create_RedBallId(void);
static inline int Create_BlueBallId(void);
int Create_RedBallGroup(int *P_array,int size);
int Sort_Bubble(int *P_array,int size);
int Check_Successive(int *P_array,int size);
int Init_DoubleBall_Group(Str_DoubleBall_Group* P_str);
int Print_DoubleBall_Group(Str_DoubleBall_Group* P_str);
int Destroy_DoubleBall_Group(Str_DoubleBall_Group* P_str);
Str_DoubleBall_Group* Create_DoubleBall_Group(void);

int Get_Amount_Of_Group(void);

/*****************/

/* 生成一个随机的号码 */
static inline int Create_RandomId(void)
{
	srand((unsigned int)time(NULL));		//设置随机数生成函数种子
	return rand();
}

/* 生成随机红球的号码 */
static inline int Create_RedBallId(void)
{
	return (Create_RandomId()%RED_BALL_MAX + 1);
}

/* 生成随机蓝球的号码 */
static inline int Create_BlueBallId(void)
{
	return (Create_RandomId()%BLUE_BALL_MAX + 1);
}

/* 生成一组红球的号码(不重复) */
int Create_RedBallGroup(int *P_array,int size)
{
	if((NULL == P_array)||(size <= 0)){
		printf("%d %s :Argument error!",__LINE__,__FUNCTION__);
		return -1;
	}

	int i,j,check;
	
	for(i=0; i *(P_array + j)){
				tmp = *(P_array + i);
				*(P_array + i) = *(P_array + j);
				*(P_array + j) = tmp;
			}
		}
	}
	
	return 0;
}

/* 查连号算法,输入必须为升序数组 */
int Check_Successive(int *P_array,int size)
{
	if((NULL == P_array)||(size <= 0)){
		printf("%d %s :Argument error!",__LINE__,__FUNCTION__);
		return -1;
	}

	int i,j;	
	int tmp = 0;
	int Max_Successive_Num = 0;
	
	for(i=0; i Max_Successive_Num){
			Max_Successive_Num = tmp;
		}

		tmp = 0;
	}
	
	return Max_Successive_Num;
}

/* 生成一组双色球号码(6红球 + 1蓝球) */
int Init_DoubleBall_Group(Str_DoubleBall_Group* P_str)
{
	if(NULL == P_str){
		printf("%d %s :Argument error!",__LINE__,__FUNCTION__);
		return -1;	
	}

	int ret = 0;
	
	do{
		Create_RedBallGroup(P_str->RedBall_IdGroup, 
							INT_ARRAY_SIZE(P_str->RedBall_IdGroup));	//生成红球号码数组

		Sort_Bubble(P_str->RedBall_IdGroup, 
						INT_ARRAY_SIZE(P_str->RedBall_IdGroup));		//排序
	
		ret = Check_Successive(P_str->RedBall_IdGroup, 
							INT_ARRAY_SIZE(P_str->RedBall_IdGroup));	//查连号
	}while(ret > MAX_SUCCESSIVE);
	
	P_str->BlueBall_Id=Create_BlueBallId();			//生成蓝球号码
	
	return 0;
}

/* 销毁单个双色球号码序列 */
int Destroy_DoubleBall_Group(Str_DoubleBall_Group* P_str)
{
	if(NULL == P_str){
		printf("%d %s :Argument error!",__LINE__,__FUNCTION__);
		return -1;	
	}

	free(P_str);

	return 0;
}

/* 生成单个双色球号码序列 */
Str_DoubleBall_Group* Create_DoubleBall_Group(void)
{
	Str_DoubleBall_Group* p = NULL;
	
	p = (Str_DoubleBall_Group*)malloc(sizeof(Str_DoubleBall_Group));
	
	if(NULL == p){
		printf("%d %s :malloc() error!",__LINE__,__FUNCTION__);
		return NULL;
	}
	
	memset(p,0,sizeof(Str_DoubleBall_Group));
	
	return p;
}

/* 打印整型数组 */
static inline void Print_Int_Array(int *P_array,int size)
{
	if((NULL == P_array)||(size <= 0)){
		printf("%d %s :Argument error!",__LINE__,__FUNCTION__);
		return -1;
	}

	int i;
	
	for(i=0; iRedBall_IdGroup, 
						INT_ARRAY_SIZE(P_str->RedBall_IdGroup));
	
	printf("\nThe blue ball number is:");
	printf(" %d\n\n",P_str->BlueBall_Id);

	return 0;
}

/* 获取需要生成多少组双色球号码 */
int Get_Amount_Of_Group(void)
{
	int amount = 0;
	
	while(amount == 0){
		printf("\n*** Good luck to you ***\n");
		printf("Please input the number of groups you want to create: ");	
		scanf("%ud",&amount);
		
		if(amount <= 0){
			printf("You have input an invalid number,please input a amount larger than 0\n");
			continue;
		}
	}
	
	return amount;	
}

int main(void)
{
	Str_DoubleBall_Group *p_DBG = NULL;
	
	int i,amount;
	
	amount = Get_Amount_Of_Group();

	printf("\nCreating the double ball result,please wait...\n");
	
	p_DBG = Create_DoubleBall_Group();
	
	for(i=0;i


程序的实际运行效率有待提高,2010年的电脑用虚拟机跑起来会卡,望各路大神指点一二。

你可能感兴趣的:(C代码案例)