逍遥游wuzaijun.blog.chinaunix.net
freeandeasywu
全部博文(40)
2011年(11)
2010年(29)
格伯纳
tenya
grantfee
reb00t
wobenbud
零self
sysok8
儒释道
yousuozi
分类: LINUX
2010-07-22 00:18:30
#include<stdlib.h>#include<stdio.h>#include<time.h>#define TRUE 1#define FALSE 0#define N_CARDS 13void shuffle(int *deck, int n_cards){ int i; static int first_time=TRUE; if(first_time){ first_time=FALSE; srand((unsigned int) time(NULL)); } for(i = n_cards - 1; i > 0;i-=1) { int where; int temp; where = rand() % i; temp=deck[where]; deck[where] = deck[i]; deck[i] = temp; }}void print(int *deck, int n_cards, void (*fun)(int * , int )){ int i=0; printf("inital status .......\n"); for( ; i < n_cards ; i++ ) { printf("deck[%d]=%d\n",i,deck[i]); } fun(deck , n_cards); printf("change status .......\n"); for(i = 0 ; i < n_cards ; i++ ) { printf("deck[%d]=%d\n",i,deck[i]); }}void main(){ int *deck=(int *)malloc(N_CARDS * sizeof(int)); int *p=deck; int i = 0; for(;p < &deck[N_CARDS]; )//注意:赋值的最有效方式 *p++ = i++; print(deck, N_CARDS, shuffle); }
上一篇:BSS段 data段 text段 堆heap 和 栈stack
下一篇: linux中fork()函数详解(原创!!实例讲解)
登录 注册