Sigrid Haflínudóttirupdate to more sensible limitsf28a9636 years agoHistory
65 lines · 1.5 KB · C
RawDownload
#include <assert.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include "shuffle.h" static intrndrange(int max_excluding){	assert(max_excluding > 0 && max_excluding < RAND_MAX);	return rand() % max_excluding;} intmain(){	Shuffle s;	char c[64], *p, *argv[4];	int argc, i;	int first, total, index, count; 	printf("> init TOTAL [FIRST]\n");	printf("> resize TOTAL\n");	printf("> get [INDEX] [COUNT]\n");	total = 0; 	for(;;){		c[sizeof(c)-1] = 0;		printf("> ");		if(fgets(c, sizeof(c), stdin) == NULL)			break;		if(c[sizeof(c)-1] != 0 || c[0] < 'a' || c[0] > 'z')			continue; 		c[strlen(c)-1] = 0;		argv[0] = p = c;		for(argc = 1; argc < 4; argv[argc++] = p){			if((p = strchr(p, ' ')) == NULL)				break;			for(*p++ = 0; *p == ' '; p++);		} 		if(strcmp(argv[0], "init") == 0 && argc >= 2 && argc <= 3){			if((total = atoi(argv[1])) < 1 || total >= RAND_MAX)				printf("total is out of range [%d, %d]\n", 1, RAND_MAX-1);			else{				first = argc > 2 ? atoi(argv[2]) : rndrange(total);				shuffle_init(&s, rndrange, total, first);			}		}else if(strcmp(argv[0], "resize") == 0 && argc == 2){			total = atoi(argv[1]);			shuffle_resize(&s, total);		}else if(strcmp(argv[0], "get") == 0 && argc >= 1 && argc <= 3){			index = argc > 1 ? atoi(argv[1]) : 0;			count = argc == 2 ? 1 : (argc == 3 ? atoi(argv[2]) : total);			for(i = index; i < index+count; i++)				printf("%d ", shuffle_one(&s, i));			printf("\n");		}else{			printf("?\n");		}	} 	return 0;}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t