二分探索木の形状

#include 
#include 
#include 

#define DATANUM 1000

struct BinarySearchTree{
    int data;
    struct BinarySearchTree *left;
    struct BinarySearchTree *right;
};

struct BinarySearchTree bst[DATANUM];

int makeBinarySearchTree(){
    struct BinarySearchTree *current;
    int i, depth, maxDepth;
    
    srand((unsigned int)time(NULL));
    bst[0].data = rand();
    bst[0].left = NULL;
    bst[0].right = NULL;
    maxDepth = 0;
    
    for (i = 1; i < DATANUM; i++){
        bst[i].data = rand();
        bst[i].left = NULL;
        bst[i].right = NULL;
        
        current = &bst[0];
        
        depth = 1;
        while (-1){
            if(bst[i].data < current->data){
                if (current->left == NULL){
                    current->left = &bst[i];
                    break;
                }
                else {
                    current = current->left;
                    depth++;
                }
            }
            else {
                if (current->right = NULL){
                    current->right = &bst[i];
                    break;
                }
                else {
                    current = current->right;
                    depth++;
                }
            }
        }
        
        if (depth > maxDepth) maxDepth = depth;
    }
    
    return maxDepth;
}

int main(){
    int i, sum;
    
    sum = 0;
    for (i = 1; i <= 10000; i++){
        sum += makeBinarySearchTree();
    }
    
    printf("DATANUM = %d\n", DATANUM);
    printf("max depth average is = %lf\n", sum / 10000.0);
    
    return 0;
}

ASCII文字コード表

ASCIIでは、半角英数記号の1文字を7ビットで表します。

#include 
int main(){
    int row, col;
    char data;
    printf("\t[20]\t[30]\t[40]\t[50]\t[60]\t[70]\n");
    for(row = 0x0; row <= 0xF; row++){
        printf("[%X]", row);
        for(col = 0x20; col <= 0x70; col += 0x10){
            data = col + row;
            if (data == 0x7F) break;
            else printf("\t%c", data);
        }
        printf("\n");
    }
    return 0;
}
	[20]	[30]	[40]	[50]	[60]	[70]
[0]	 	0	@	P	`	p
[1]	!	1	A	Q	a	q
[2]	"	2	B	R	b	r
[3]	#	3	C	S	c	s
[4]	$	4	D	T	d	t
[5]	%	5	E	U	e	u
[6]	&	6	F	V	f	v
[7]	'	7	G	W	g	w
[8]	(	8	H	X	h	x
[9]	)	9	I	Y	i	y
[A]	*	:	J	Z	j	z
[B]	+	;	K	[	k	{
[C]	,	<	L	\	l	|
[D]	-	=	M	]	m	}
[E]	.	>	N	^	n	~
[F]	/	?	O	_	o

ポインタ2

#include 
#include 
#include 

int main() {
    char buffer[1000] = "";
    int length;
    char *line = NULL;
    
    printf("type character and enter key");
    gets(buffer);
    length = strlen(buffer);
    if (length > 0) {
        line = (char *)malloc(length + 1);
        strcpy(line, buffer);
        printf("line = %s\n", line);
        free(line);
    }
    return 0;
}

ポインタ

ポインタはメモリーのアドレスを格納できる容器で、なおかつ、どの型のデータを指し示すかの情報を持っています。

下の例では、整数型のポインタ変数pと、整数型の変数iを宣言、iを1に初期化し、「*p」という表現で、変数iの中身を参照しています。

#include 

int main() {
    int *p, i = 1;
    p = &i;
    printf("%d\n", *p);
    return 0;
}

基本データ型

int, float, double, charの変数の大きさです。C言語には、文字列型がありません。C++はstringクラスがあります。

#include 

int main() {
    int i = 1;
    float f = 1;
    double d = 0.5;
    char c = 'a';
    
    printf("i = %d\n", i);
    printf("f = %f\n", f);
    printf("d = %f\n", d);
    printf("c = %c\n", c);
    
    printf("sizeof(i) = %d\n", sizeof(i));
    printf("sizeof(f) = %d\n", sizeof(f));
    printf("sizeof(d) = %d\n", sizeof(d));
    printf("sizeof(c) = %d\n", sizeof(c));
    
    return 0;
}
i = 1
f = 1.000000
d = 0.500000
c = a
sizeof(i) = 4
sizeof(f) = 4
sizeof(d) = 8
sizeof(c) = 1