乱数プログラム 使用例4



[ 簡単な説明 ]

ランダム・データ配列生成プログラム使用例です。
int 型、char 型、double 型、文字列型のランダム・データを生成し、表示します。
文字列型に関しては、重複データを排除する関数も使用します。

プログラム・ソース("test4.c")           top (先頭に戻る)
/*		test4.c		*/
#include <stdio.h>
#include <stdlib.h>
#include "random.h"

int dbldel_c(unsigned char *a[], int n, int flag);
void qsort_c(unsigned char *a[], int n);
void inssort_c(unsigned char *a[], int n);
int strcomp(unsigned char *a, unsigned char *b);

main()
{
	int *a, *ip;
	unsigned char *c, *cp;
	double *d, *dp;
	unsigned char **s, **sp, **sw, **swp;
	int i, n = 100, nn;

	init_mkdata(init_ornd0, ornd0, 1L);

	a = mkdata_i(n, 1, 99);
	printf("a = mkdata_i(n, 1, 99);\n");
	for(i = 0, ip = a; i < n; i++)	printf("%d ", *ip++);
	putchar('\n');

	c = mkdata_c(n, 'a', 'z');
	printf("\nc = mkdata_c(n, 'a', 'z');\n");
	for(cp = c; *cp; )	printf("%c", *cp++);
	putchar('\n');

	d = mkdata_d(n, -99., 99.);
	printf("\nd = mkdata_d(m, -99., 99.);\n");
	for(i = 0, dp = d; i < n; i++)	printf("%f ", *dp++);
	putchar('\n');

	s = mkdata_s(n, 1, 3, 'A', 'Z');
	printf("\ns = mkdata_s(n, 1, 3, 'A', 'Z');\n");
	for(i = 0, sp = s; i < n; i++)	printf("%3s ", *sp++);
	putchar('\n');
	sw = (unsigned char **)malloc(n * sizeof(unsigned char *));
	for(i = 0, swp = sw, sp = s; i < n; i++)	*swp++ = *sp++;
	qsort_c(sw, n);
	nn = dbldel_c(sw, n, 0);
	printf("重複数 = %d\n\n", n - nn);

	s = mkdata_sd(n, 1, 3, 'A', 'Z');
	printf("s = mkdata_sd(n, 1, 3, 'A', 'Z');\n");
	for(i = 0, sp = s; i < n; i++)	printf("%3s ", *sp++);
	putchar('\n');
	for(i = 0, swp = sw, sp = s; i < n; i++)	*swp++ = *sp++;
	qsort_c(sw, n);
	nn = dbldel_c(sw, n, 0);
	printf("重複数 = %d\n", n - nn);

	return 1;
}

int dbldel_c(unsigned char *a[], int n, int flag)
{
	int i, nn;
	unsigned char **dp, **wp;

	nn = 1;
	wp = dp = a + 1;
	for(i = 1; i < n; i++, dp++)	if(strcmp(*(dp - 1), *dp))	nn++;
	if(nn != n && flag)
	{
		wp = a;
		dp = a + 1;
		i = n - nn;
		while(i > 0)
		{
			if(strcmp(*wp, *dp) == 0)
			{
				**dp = '\0';
				i--;
			}
			else	wp = dp;
			dp++;
		}
	}
	return nn;
}

void qsort_c(unsigned char *a[], int n)
{
	unsigned char **i, **j, **l, **r;
	unsigned char **st1[32], **st2[32], ***s1, ***s2;
	unsigned char *x, *w;

	if(n <= 1)
	{
		fprintf(stderr, "Error : n <= 1  in qsort_c()\n");
		return;
	}

	s1 = st1;
	s2 = st2;
	*s1 = a;
	*s2 = a + n - 1;
	do
	{
		l = *s1--;
		r = *s2--;
		if(r - l < 11)	inssort_c(l, r - l + 1);
		else
		{
			do
			{
				i = l;
				j = r;
				x = *(l + (int)((r - l) / 2));
				do
				{
					while(strcomp(x, *i) > 0)	i++;
					while(strcomp(*j, x) > 0)	j--;
					if(i > j)	break;
					w = *i;
					*i++ = *j;
					*j-- = w;
				} while(i <= j);
				if(j - l < r - i)
				{
					if(i < r)
					{
						*(++s1) = i;
						*(++s2) = r;
					}
					r = j;
				}
				else
				{
					if(l < j)
					{
						*(++s1) = l;
						*(++s2) = j;
					}
					l = i;
				}
			} while(l < r);
		}
	}while (s1 >= st1);
	return;
}

void inssort_c(unsigned char *a[], int n)
{
	unsigned char *x;
	int i, j;

	if(n <= 1)
	{
		fprintf(stderr, "Error : n <= 1  in inssort_c()\n");
		return;
	}
	for(i = 1; i < n; i++)
	{
		x = a[i];
		j = i - 1;
		while(j >= 0 && strcomp(a[j], x) > 0)
		{
			a[j + 1] = a[j];
			j--;
		}
		a[j + 1] = x;
	}
}

int strcomp(unsigned char *a, unsigned char *b)
{
	unsigned char *p, *q;

	p = a;
	q = b;
	while(*p)
	{
		if(*p > *q)	return 1;
		if(*p++ < *q++)	return -1;
	}
	if(*q)	return -1;
	return 0;
}

出力結果           top (先頭に戻る)
a = mkdata_i(n, 1, 99);
25 15 14 87 60 21 53 44 71 96 32 41 42 51 88 71 41 13 9 42 6 59 88 20 23 7 11 65 43 54 76 23 32 3 93 19 77 56 72 35 43 48 20 72 11 17 60 28 95 46 99 94 34 69 16 48 27 72 40 43 51 53 33 67 79 77 13 5 72 75 34 35 98 98 7 42 36 47 15 61 7 85 45 80 10 93 98 11 80 21 1 59 35 15 20 36 25 5 31 15

c = mkdata_c(n, 'a', 'z');
ttbkmqjlqexqrxqzrwssuyjioqflqdbeztwlklhxkfwqisoqdezwvgmqovjhzunikheeixyzahaozzxh xgfwvacecldccdnhkbiy

d = mkdata_d(m, -99., 99.);
57.018898 -3.375381 95.966375 -1.142148 9.923569 69.417481 83.597857 21.188166 -92.501629 21.122018 -16.241655 68.496649 51.180034 70.833951 -67.785276 24.864330 -83.204904 49.173050 -0.549833 64.962334 49.950598 -0.291258 54.828258 6.531944 90.380130 -37.147622 -46.075606 -14.702125 5.389716 98.962502 62.770643 42.193793 -84.928417 -9.900621 -79.732282 3.540151 -98.677223 -20.094154 65.546446 -32.885075 -81.459784 75.417474 -54.509113 11.329677 -58.115173 -7.707985 -56.104256 -68.237510 -51.825780 -33.888999 73.591449 -54.518172 57.088701 -18.209923 53.815817 18.430727 93.236971 61.766500 -4.441524 -2.686865 -14.144984 63.252676 25.726096 -53.511557 -52.743038 -6.243508 5.361583 22.131261 -81.904544 -73.664997 6.388509 55.667106 47.057665 86.183039 -89.661072 44.369951 57.773018 -0.887928 -73.413059 82.711155 -31.614158 92.843064 -24.631745 32.265178 -41.148138 37.243448 72.635569 -81.988820 93.905374 9.613043 -1.584009 -90.440365 14.786347 24.128735 27.657517 -64.113559 -40.578721 -94.567739 -53.993495 -34.677682

s = mkdata_s(n, 1, 3, 'A', 'Z');
TSB ANZ V EK Z WZ BY FSM JEE CG MO E LV UUK DGX K M KM YN AX PSJ H ZV S XDA D C SW HPH HX J HT N D G JS YN E BHH PL GFB X Z NNP UE H DFW S H CK Z T N SR ZP J Y QO GLU AMQ SLZ JF XJU Y ZX AT LEX BN XTX Q RQ X EJA NGE VMA XQU T B QL VD N YV OY QXI DB MLU L HBB D P S LZ W P VWN J YYJ Y Q T
重複数 = 21

s = mkdata_sd(n, 1, 3, 'A', 'Z');
N ZNF FI VBR NUU BVM BI IE NZR FM GPX NV VLO WU AE KRF DJO XD X CN IQD LJZ UZ ONP G OMU C OL JYZ DA M T Z JI GXG GBB IV PUB XS ZA WN KC IC RWB BM RFU FMR ZM B V Q WLK P YI U D RYY LI SLX BP ZRK WIV J LY WD ZJX WDE IY JMF EZ PT I LEI L WKE EBY DMO S IW Y VJG O WPS ZL RDN ATH FT R F JXC OMC NM XFP SP QY KA KHQ BT VB EA
重複数 = 0