About

Salve

Programmi

    #include <stdio.h>
    #include <stdlib.h>
    
    struct Tree {
    	int info;
    
    	struct Tree* right;
    	struct Tree* left;
    };
    
    struct Tree*
    createTree( int info )
    {
    	struct Tree* tree;
    
    	tree = malloc(sizeof(struct Tree));
    
    	tree->info = info;
    	tree->right = NULL;
    	tree->left = NULL;
    
    	return tree;
    }
    
    void
    insertTree( struct Tree** root, int info )
    {
    	if( root && *root )
    	{
    		if( info > (*root)->info )
    		{
    			if( (*root)->left )
    				insertTree( &(*root)->left, info );
    			else
    				(*root)->left = createTree( info );
    		} else {
    			if( (*root)->right )
    				insertTree( &(*root)->right, info );
    			else
    				(*root)->right = createTree( info );
    		}
    	}
    }
    
    void
    printTree( struct Tree* root, int i )
    {
    	int j;
    
    	for(j=0;j<i;j++)
    		putchar('\t');
    
    	printf("- %d\n", root->info);
    
    	if( root->left )
    		printTree( root->left, i+1 );
    
    	if( root->right )
    		printTree( root->right, i+1 );
    }
    
    int
    main()
    {
    /*	struct Tree* root = createTree( 3 );
    
    	insertTree( &root, 4 );
    	insertTree( &root, 2 );
    	insertTree( &root, 5 );
    	insertTree( &(root->left), 3 );
    	insertTree( &root, 3 );
    	insertTree( &root, 1 );	
    
    	printTree( root, 0 );*/
    
    	struct Tree* root = createTree( 5 );
    	int i;
    
    	for(i = 1;i < 11;i++)
    		if( i != 5 )
    			insertTree( &root, i);
    
    	printTree( root, 0 );
    
    	return 0;
    }
    
    #include <stdio.h>
    #include <assert.h>
    
    #define EPOCH 50000
    #define CH_BLOCK 10
    
    enum CycleMode {
    	RIGHT,
    	LEFT,
    	UP,
    	DOWN
    };
    
    struct Cell {
    	int state, nstate;
    
    	struct Cell* up;
    	struct Cell* down;
    	struct Cell* left;
    	struct Cell* right;
    };
    
    typedef struct Cell Cell;
    
    unsigned char*
    rle_dec( unsigned char* data, int* len )
    {
    	unsigned char* result;
    	int size = strlen(data);
    	int i, j, k = 0;
    	unsigned char byte, count;
    
    	for(i = 0;i < size;i++)
    	{
    		if( k == 0 )
    			result = malloc( CH_BLOCK * sizeof(unsigned char*));
    
    		if( data[i] >= '0' && data[i] <= '9' )
    		{
    			count = atoi( data+i );
    
    			while(data[i] >= '0' && data[i] <= '9')
    				i++;
    		} else
    			count = 1;
    
    		byte = data[i];
    
    		for(j = 0;j < count;j++)
    		{
    			if( k % CH_BLOCK )
    				result = realloc( result, (CH_BLOCK + k) * sizeof(unsigned char*));
    
    			result[ k++ ] = byte;
    		}
    	}
    
    	*len = k;
    
    	return result;
    }
    
    Cell* 
    newCell(int state)
    {
    	Cell* new = malloc(sizeof(struct Cell));
    
    	assert( new );
    
    	new->state = state;
    
    	new->up = new->down = new->left = new->right = NULL;
    
    	return new;
    }
    
    Cell*
    cycleCell(Cell* head, int h, int mode)
    {
    	int i;
    	Cell* tmp = head;
    
    	assert( tmp );
    
    	switch( mode )
    	{
    		case RIGHT:
    			for(i = 0;i < h;i++, tmp = tmp->right);
    			break;
    		case LEFT:
    			for(i = 0;i < h;i++, tmp = tmp->left);
    			break;
    		case UP:
    			for(i = 0;i < h;i++, tmp = tmp->up);
    			break;
    		case DOWN:
    			for(i = 0;i < h;i++, tmp = tmp->down);
    			break;
    		default:
    			break;
    	}
    
    	return tmp;
    }
    
    void
    setCell(Cell* head, int i, int j, int val)
    {
    	Cell *t1, *t2;
    
    	t1 = cycleCell(head, i, DOWN);
    	t2 = cycleCell(t1, j, RIGHT);
    
    	t2->state = val;
    }
    
    Cell*
    getCell(Cell* head, int i, int j)
    {
    	Cell *t1, *t2;
    
    	t1 = cycleCell(head, i, DOWN);
    	t2 = cycleCell(t1, j, RIGHT);
    
    	return t2;
    }
    
    int
    countNeighbours(Cell* head)
    {
    	return 
    		head->up->state + 
    		head->up->left->state + head->up->right->state +
    		head->left->state + head->right->state +
    		head->down->state +
    		head->down->left->state + head->down->right->state;
    }
    
    void
    applyRules(Cell* head)
    {
    	int c = countNeighbours( head );
    
    	if(head->state == 0 && c == 3)
    		head->nstate = 1; /* return 1 */
    	else if(head->state == 1 && (c == 2 || c == 3))
    		head->nstate = 1;
    	else
    		head->nstate = 0;
    }
    
    char**
    readFile(char* file, int h, int w)
    {
    	FILE* fp;
    	char** matrix, *line;
    	int i, len, c = 5;
    
    	char buf[BUFSIZ];
    
    	assert( (fp = fopen(file,"r")) );
    
    	matrix = malloc( sizeof(char*) * h );
    
    	for(i = 0;i < h;i++)
    		matrix[i] = malloc( w );
    
    	memset( matrix, h*w, 0 );
    
    	while(fgets(buf,BUFSIZ,fp))
    	{
    		buf[strlen(buf)-1] = 0;
    		line = rle_dec(buf, &len);
    
    		for(i = 0;i < len;i++)
    			matrix[c][i+5] = (line[i] == 'b') ? 0 : 1;
    
    		c++;
    	}
    /*
    	int j;
    
    	for(i = 0;i < h;i++)
    	{
    		for(j = 0;j < w;j++)
    			printf("%d ", matrix[i][j] );
    
    		putchar('\n');
    	}
    */
    	return matrix;
    }
    
    int
    main(int argc,char** argv)
    {
    	int i, j, h, w;
    	Cell *polo, *Ci, *Cj;
    
    	char** matrix;
    
    	if(argc < 4)
    		return 1;
    
    	h = atoi(argv[1]);
    	w = atoi(argv[2]);
    
    	polo = newCell(0);
    	Ci = polo;
    
    	for(i = 0;i < h;i++, Ci = Ci->down)
    	{
    		Cell* ti = newCell(0);
    
    		ti->up = Ci;
    		Ci->down = ti;
    
    		if(i == h-1)
    			Ci->down = polo;
    
    		for(j = 0, Cj = Ci;j < w-1;j++, Cj = Cj->right)
    		{
    			Cell* tj = newCell(0);
    
    			if(i > 0)
    			{
    				tj->up = Cj->up->right;
    				Cj->up->right->down = tj;
    
    				if(i == h-1)
    					tj->down = cycleCell(tj, h-1, UP );
    			}
    			
    			tj->left = Cj;
    			Cj->right = tj;
    
    			if(j == w-2)
    				tj->right = Ci;
    		}
    	}
    
    	for(i = 0, Ci = polo;i < h;i++, Ci = Ci->down)
    		Ci->left = cycleCell(Ci, w-1, RIGHT);
    
    	for(j = 0, Cj = polo;j < w;j++, Cj = Cj->right)
    		Cj->up = cycleCell(Cj, h-1, DOWN );
    
    	if( atoi(argv[3]) == 0 )
    	{
    		matrix = readFile( argv[3], h, w );
    
    		for(i = 0;i < h;i++)
    		{
    			for(j = 0;j < w;j++)
    				printf("%d ", matrix[i][j] );
    
    			putchar('\n');
    		}
    
    	} else {
    		matrix = malloc(sizeof(char*)*h);
    
    		for(i = 0;i < h;i++)
    			matrix[i] = malloc(sizeof(char)*w);
    
    		srand(time(NULL));
    
    		for(i = 0;i < atoi(argv[3]);i++)
    			matrix[rand()%h][rand()%w] = 1;
    	}
    
    	for(i = 0;i < h;i++)
    		for(j = 0;j < w;j++)
    			setCell(polo, i, j, matrix[i][j]);
    
    	while(1)
    	{
    
    		for(i = 0;i < h;i++,polo = polo->down)
    		{
    			Cell* tmp = polo;
    
    			for(j = 0;j < w;j++,tmp = tmp->right)
    			{
    				applyRules(tmp);
    
    				printf("%c ", tmp->nstate ? 'O' : '.' );
    			}
    
    			putchar('\n');
    		}
    
    		putchar('\n');
    
    		for(i = 0;i < h;i++)
    			for(j = 0;j < w;j++)
    				getCell(polo, i, j)->state = getCell(polo, i, j)->nstate; /* setCell(polo, i, j, matrix[i][j]); */
    
    		usleep(EPOCH);
    	}
    
    	return 0;
    }