/* くじ引きプログラム '96.1.16 Y.Yasuda. 入力ファイルを二つ用意する。 第一引数に景品ファイル(景品の羅列)の名前 第二引数に入力ファイル(人間の名前、番号札などの羅列)の名前 を与えて実行すると標準出力にDELIM文字で区切って景品ファイル順に 当選入力データをつないで出す。 */ #include #include #include #include #include #define MAXGOODS 100 #define MAXLINE 1000 #define MAXLEN 256 #define DELIM ";" main(argc, argv) int argc; char *argv[]; { struct timeval tp; char gfname[256],ifname[256],str[MAXLEN], gbuf[MAXGOODS][MAXLEN],ibuf[MAXLINE][MAXLEN]; int i,l,gmax,imax,rnd,gmap[MAXGOODS]; FILE *gfp,*ifp; /* 引数から景品ファイルと入力ファイルを得る */ if( argc != 3 ) { fprintf(stderr,"Usage: %s goods-file input-file\n",argv[0]); exit(1); }; strcpy(gfname,argv[1]); strcpy(ifname,argv[2]); /* 景品ファイルを読んで gbuf[0-gmax] に景品データを入れる */ gfp=fopen(gfname,"r"); if(gfp == NULL) { fprintf(stderr,"Goods file missing. Cannot open '%s'.\n",gfname); exit(1); }; l=0; while( fgets(str, sizeof(str), gfp) != NULL ) { if( strlen(str) == NULL ) { continue; } else { if( l == MAXGOODS ) break; if( str[strlen(str)-1] == '\n' ) str[strlen(str)-1]=NULL; strcpy(gbuf[l],str); l++; }; }; if( l == 0 ) { fprintf(stderr,"Goods file '%s' is empty.\n",gfname); exit(1); }; if( l == MAXGOODS ) { fprintf(stderr,"Goods file '%s' has more than %d line.\n",gfname,MAXGOODS); exit(1); }; fclose(gfp); gmax= l - 1; /* 対象ファイルを読んで ibuf[0-imax] に景品データを入れる */ ifp=fopen(ifname,"r"); if(ifp == NULL) { fprintf(stderr,"Input file missing. Cannot open '%s'.\n",ifname); exit(1); }; l=0; while( fgets(str, sizeof(str), ifp) != NULL ) { if( strlen(str) == NULL ) { continue; } else { if( l == MAXLINE ) break; if( str[strlen(str)-1] == '\n' ) str[strlen(str)-1]=NULL; strcpy(ibuf[l],str); l++; }; }; if( l == 0 ) { fprintf(stderr,"input file '%s' is empty.\n",ifname); exit(1); }; if( l == MAXLINE ) { fprintf(stderr,"Input file '%s' has more than %d line.\n",ifname,MAXLINE); exit(1); }; fclose(ifp); imax= l - 1; if( gmax > imax ) { fprintf(stderr,"'%s' file is longer than '%s' file.\n",gfname,ifname); exit(1); }; /* 乱数初期化 */ gettimeofday(&tp); srand((int)tp.tv_usec); for(l=0;l<=gmax;l++){ if(l==0) { rnd = (int)(rand() % (imax + 1)); } else { while(1) { rnd = (int)(rand() % (imax + 1)); for(i=0;i