Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Jonas Raoni Soares Silva http://jsfromhell.com

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

change integer variables content

   1  
   2  //+ Jonas Raoni Soares Silva
   3  //@ http://jsfromhell.com
   4  
   5  #include<stdio.h>
   6  #include<conio.h>
   7  void main(){
   8  	int a, b;
   9  	clrscr();
  10  
  11  	printf( "Digite a= " );
  12  	scanf( "%d", &a );
  13  
  14  	printf( "Digite b= " );
  15  	scanf( "%d", &b );
  16  
  17  	a = b - a + ( b = a );
  18  
  19  	printf( "a= %d\nb= %d", a, b);
  20  	getch();
  21  }
  22  

pascal triangle plotter XD

   1  
   2  //+ Jonas Raoni Soares Silva
   3  //@ http://jsfromhell.com
   4  
   5  #include <stdio.h>
   6  #include <conio.h>
   7  
   8  #define MAX 16
   9  
  10  int main () {
  11  	int unsigned vetor[MAX], grau, i = 0, j, top, left;
  12  
  13  	clrscr();
  14  
  15  	printf( "Dado o grau, printar o triangulo de pascal\n" );
  16  
  17  	while( grau > MAX && printf( "Digite um numero entre 0 e %d para o grau: ", MAX ) && scanf( "%u", &grau ) );
  18  	while( i++ < grau && !( j = 0 ) && printf( "\n" ) )
  19  		while( j < i && ( j == 0 && ( top = left = vetor[j] = 1 ) ? 1 : j > 0 && j < i-1 && ( top = vetor[j] ) && ( vetor[j] = left + top ) && ( left = top ) ? 1 : ( vetor[j] = 1 ) ) && printf( "%-5d", vetor[j] ) && ++j );
  20  	getch();
  21  	return 0;
  22  }
  23  

palindrome :D

   1  
   2  //+ Jonas Raoni Soares Silva
   3  //@ http://jsfromhell.com
   4  
   5  #include <stdio.h>
   6  
   7  int isPalindromo( char *s ){
   8  	char *s2 = s + strlen( s ) - 1;
   9  	if( !*s )
  10  		return 1;
  11  	while( *s++ == *s2-- && *s );
  12  	return !*s && *( --s ) == *( ++s2 );
  13  }
  14  
  15  int main( int argc, char *argv[] ){
  16  	char s[255];
  17  
  18  	printf( "Texto:" );
  19  	gets( s );
  20  	printf( "%s \n", isPalindromo( s ) ? "YES" : "NO" );
  21  
  22  	system( "pause" );
  23  
  24  	return 0;
  25  }
  26  

intersecction of two lines in c

   1  
   2  //+ Jonas Raoni Soares Silva
   3  //@ http://jsfromhell.com
   4  
   5  #include <stdio.h>
   6  
   7  typedef struct {
   8          float x1, x2, y1, y2;
   9  } Segmento;
  10  
  11  typedef struct {
  12  	float x, coefLinear;
  13  } EquacaoDaReta;
  14  
  15  void preencherSegmento( Segmento *s ){
  16      printf( "Entre com as coordenadas do segmento A no formato: x1,y1,x2,y2:" );
  17      scanf( "%f,%f,%f,%f", &s->x1, &s->y1, &s->x2, &s->y2 );
  18  }
  19  
  20  float calcularTangente( Segmento *s ){
  21  	return tangenteInvalida( s ) ? 0 : ( s->y1 - s->y2 ) / ( s->x1 - s->x2 );
  22  }
  23  
  24  int tangenteInvalida( Segmento *s ){
  25  	return !( s->x1 - s->x2 );
  26  }
  27  
  28  int main( int argc, char *argv[] ){
  29      Segmento segA, segB;
  30  	EquacaoDaReta eqA, eqB;
  31   	float tangenteA, tangenteB;
  32  	float interseccaoX, interseccaoY;
  33  
  34  	preencherSegmento( &segA );
  35  	preencherSegmento( &segB );
  36  
  37      tangenteA = calcularTangente( &segA );
  38  	tangenteB = calcularTangente( &segB );
  39  
  40  	if( tangenteInvalida( &segA ) && tangenteInvalida( &segB ) ){
  41  		printf( "As retas sao paralelas, portanto nunca se cruzam.");
  42  		system( "pause" );
  43  		return 0;
  44  	}
  45  	else if( tangenteInvalida( &segA ) ){
  46    		interseccaoX = segA.x1;
  47  		interseccaoY = segB.y1 + tangenteB * ( segA.x1 - segB.x1 );
  48  	}
  49  	else if( tangenteInvalida( &segB ) ){
  50  		interseccaoX = segB.x1;
  51  		interseccaoY = segA.y1 + tangenteA * ( segB.x1 - segA.x1 );
  52  	}
  53  	else if( tangenteA == tangenteB ){
  54  		printf( "As retas sao paralelas, portanto nunca se cruzam.");
  55  		system( "pause" );
  56  		return 0;
  57  	} else {
  58  		eqA.x = tangenteA;
  59  		eqA.coefLinear = tangenteA * -segA.x1 + segA.y1;
  60  
  61  		eqB.x = tangenteB;
  62  		eqB.coefLinear = tangenteB * -segB.x1 + segB.y1;
  63  
  64  	    interseccaoX = ( eqB.coefLinear - eqA.coefLinear ) / ( eqA.x - eqB.x );
  65  		interseccaoY = eqA.x * interseccaoX + eqA.coefLinear;
  66  	}
  67  
  68  	printf( "\nO ponto de interseccao eh: (%f, %f )", interseccaoX, interseccaoY );
  69  	system( "pause" );
  70  	return 0;
  71  
  72  }

C memory manager "MemoryBlock "

   1  
   2  //+ Jonas Raoni Soares Silva
   3  //@ http://jsfromhell.com
   4  
   5  #ifndef __MEMORYBLOCK__
   6  #define __MEMORYBLOCK__
   7  
   8  
   9  //-- includes -----------------------------------------------
  10  #include <stdio.h>
  11  #include <stdlib.h>
  12  
  13  
  14  
  15  //-- data types ---------------------------------------------
  16  typedef struct _MemoryBlock {
  17  	void *data;
  18  	size_t capacity, used, dataSize;
  19  }* MemoryBlock;
  20  
  21  
  22  
  23  //-- functions ----------------------------------------------
  24  MemoryBlock getMemoryBlock( register const size_t dataSize, register const size_t initialSize ){
  25  
  26  	MemoryBlock mb = (MemoryBlock)malloc( sizeof( struct _MemoryBlock ) );
  27  	
  28  	if( mb ) {
  29  		mb->used = 0;
  30  		mb->data = NULL;
  31  		mb->dataSize = dataSize;
  32  		mb->capacity = initialSize && ( mb->data = (void *)malloc( initialSize * dataSize ) ) ? initialSize : 0;
  33  	}
  34  	return mb;
  35  }
  36  
  37  void freeMemoryBlock( register const MemoryBlock mb ){
  38  	free( mb->data );
  39  	free( mb );
  40  }
  41  
  42  int resizeMemoryBlock( register const size_t newSize, register const MemoryBlock mb ){
  43  
  44  	void *newData = (void *)realloc( mb->data, newSize * mb->dataSize );
  45  
  46  	if( newData ){
  47  		mb->data = newData;
  48  		return mb->capacity = newSize;
  49  	}
  50  	return 0;
  51  }
  52  
  53  int growMemoryBlock( register const MemoryBlock mb ){
  54  
  55  	size_t delta = mb->capacity > 64 ? mb->capacity / 4 : mb->capacity > 8 ? 16 : 4;
  56  	void *newData = (void *)realloc( mb->data, ( mb->capacity + delta ) * mb->dataSize );
  57  
  58  	if( newData ){
  59  		mb->data = newData;
  60  		return mb->capacity += delta;
  61  	}
  62  	return 0;
  63  }
  64  
  65  int growExMemoryBlock( register const size_t neededBlocks, register const MemoryBlock mb ){
  66  	while( mb->used+neededBlocks > mb->capacity )
  67  		if( !growMemoryBlock( mb ) )
  68  			return 0;
  69  	return 1;
  70  }
  71  
  72  int incMemoryBlock( register const MemoryBlock mb ){
  73  	return  mb->used+1 <= mb->capacity || growMemoryBlock( mb );
  74  }
  75  
  76  #endif
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS