changeset 34:5f3bba7b355c

add loto6.c
author kent <kent@cr.ie.u-ryukyu.ac.jp>
date Tue, 22 Dec 2009 20:46:05 +0900
parents e62c90c8e699
children 3f5886e153cb
files CbC-examples/loto6.c
diffstat 1 files changed, 80 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CbC-examples/loto6.c	Tue Dec 22 20:46:05 2009 +0900
@@ -0,0 +1,80 @@
+/*
+ *  Nov 10, 2009
+ *  created by gongo.
+ *
+ *  Nov 10, 2009
+ *  modified by kent.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+(*ret)(int, void*);
+void *env;
+
+
+__code
+print(int *numbers)
+{
+	printf("%d-%d-%d-%d-%d-%d\n", numbers[0], numbers[1], numbers[2], numbers[3], numbers[4], numbers[5]);
+	free(numbers);
+	goto ret(0, env);
+}
+
+	__code
+take(int *array, int size, int length)
+{
+	int *taked = (int*)malloc(sizeof(int)*length);
+
+	memcpy(taked, array, sizeof(int)*length);
+	free(array);
+
+	goto print(taked);
+}
+
+__code
+shuffle(int *array, int size, int idx)
+{
+	int j = random() % size;
+	int tmp = array[idx];
+	array[idx] = array[j];
+	array[j] = tmp;
+
+	if (++idx < size) {
+		goto shuffle(array, size, idx);
+	} else {
+		goto take(array, size, 6);
+	}
+}
+
+__code
+range_loop(int *array, int idx, int from, int to, int step, int size)
+{
+	array[idx] = from;
+
+	if (from+step > to) {
+		goto shuffle(array, size, 0);
+	} else {
+		goto range_loop(array, idx+1, from+step, to, step, size);
+	}
+}
+
+__code
+range(int from, int to, int step)
+{
+	int size = (to-from+1)/step;
+	int *array = (int*)malloc(sizeof(int)*size);
+
+	goto range_loop(array, 0, from, to, step, size);
+}
+
+int
+main()
+{
+	srand(time(NULL));
+	ret = _CbC_return;
+	env = _CbC_environment;
+
+	goto range(1, 43, 1);
+}
+