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

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com

#include<stdio.h>
#include<conio.h>
void main(){
	int a, b;
	clrscr();

	printf( "Digite a= " );
	scanf( "%d", &a );

	printf( "Digite b= " );
	scanf( "%d", &b );

	a = b - a + ( b = a );

	printf( "a= %d\nb= %d", a, b);
	getch();
}


pascal triangle plotter XD

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com

#include <stdio.h>
#include <conio.h>

#define MAX 16

int main () {
	int unsigned vetor[MAX], grau, i = 0, j, top, left;

	clrscr();

	printf( "Dado o grau, printar o triangulo de pascal\n" );

	while( grau > MAX && printf( "Digite um numero entre 0 e %d para o grau: ", MAX ) && scanf( "%u", &grau ) );
	while( i++ < grau && !( j = 0 ) && printf( "\n" ) )
		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 );
	getch();
	return 0;
}

palindrome :D

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com

#include <stdio.h>

int isPalindromo( char *s ){
	char *s2 = s + strlen( s ) - 1;
	if( !*s )
		return 1;
	while( *s++ == *s2-- && *s );
	return !*s && *( --s ) == *( ++s2 );
}

int main( int argc, char *argv[] ){
	char s[255];

	printf( "Texto:" );
	gets( s );
	printf( "%s \n", isPalindromo( s ) ? "YES" : "NO" );

	system( "pause" );

	return 0;
}

intersecction of two lines in c

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com

#include <stdio.h>

typedef struct {
        float x1, x2, y1, y2;
} Segmento;

typedef struct {
	float x, coefLinear;
} EquacaoDaReta;

void preencherSegmento( Segmento *s ){
    printf( "Entre com as coordenadas do segmento A no formato: x1,y1,x2,y2:" );
    scanf( "%f,%f,%f,%f", &s->x1, &s->y1, &s->x2, &s->y2 );
}

float calcularTangente( Segmento *s ){
	return tangenteInvalida( s ) ? 0 : ( s->y1 - s->y2 ) / ( s->x1 - s->x2 );
}

int tangenteInvalida( Segmento *s ){
	return !( s->x1 - s->x2 );
}

int main( int argc, char *argv[] ){
    Segmento segA, segB;
	EquacaoDaReta eqA, eqB;
 	float tangenteA, tangenteB;
	float interseccaoX, interseccaoY;

	preencherSegmento( &segA );
	preencherSegmento( &segB );

    tangenteA = calcularTangente( &segA );
	tangenteB = calcularTangente( &segB );

	if( tangenteInvalida( &segA ) && tangenteInvalida( &segB ) ){
		printf( "As retas sao paralelas, portanto nunca se cruzam.");
		system( "pause" );
		return 0;
	}
	else if( tangenteInvalida( &segA ) ){
  		interseccaoX = segA.x1;
		interseccaoY = segB.y1 + tangenteB * ( segA.x1 - segB.x1 );
	}
	else if( tangenteInvalida( &segB ) ){
		interseccaoX = segB.x1;
		interseccaoY = segA.y1 + tangenteA * ( segB.x1 - segA.x1 );
	}
	else if( tangenteA == tangenteB ){
		printf( "As retas sao paralelas, portanto nunca se cruzam.");
		system( "pause" );
		return 0;
	} else {
		eqA.x = tangenteA;
		eqA.coefLinear = tangenteA * -segA.x1 + segA.y1;

		eqB.x = tangenteB;
		eqB.coefLinear = tangenteB * -segB.x1 + segB.y1;

	    interseccaoX = ( eqB.coefLinear - eqA.coefLinear ) / ( eqA.x - eqB.x );
		interseccaoY = eqA.x * interseccaoX + eqA.coefLinear;
	}

	printf( "\nO ponto de interseccao eh: (%f, %f )", interseccaoX, interseccaoY );
	system( "pause" );
	return 0;

}

C memory manager "MemoryBlock "

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com

#ifndef __MEMORYBLOCK__
#define __MEMORYBLOCK__


//-- includes -----------------------------------------------
#include <stdio.h>
#include <stdlib.h>



//-- data types ---------------------------------------------
typedef struct _MemoryBlock {
	void *data;
	size_t capacity, used, dataSize;
}* MemoryBlock;



//-- functions ----------------------------------------------
MemoryBlock getMemoryBlock( register const size_t dataSize, register const size_t initialSize ){

	MemoryBlock mb = (MemoryBlock)malloc( sizeof( struct _MemoryBlock ) );
	
	if( mb ) {
		mb->used = 0;
		mb->data = NULL;
		mb->dataSize = dataSize;
		mb->capacity = initialSize && ( mb->data = (void *)malloc( initialSize * dataSize ) ) ? initialSize : 0;
	}
	return mb;
}

void freeMemoryBlock( register const MemoryBlock mb ){
	free( mb->data );
	free( mb );
}

int resizeMemoryBlock( register const size_t newSize, register const MemoryBlock mb ){

	void *newData = (void *)realloc( mb->data, newSize * mb->dataSize );

	if( newData ){
		mb->data = newData;
		return mb->capacity = newSize;
	}
	return 0;
}

int growMemoryBlock( register const MemoryBlock mb ){

	size_t delta = mb->capacity > 64 ? mb->capacity / 4 : mb->capacity > 8 ? 16 : 4;
	void *newData = (void *)realloc( mb->data, ( mb->capacity + delta ) * mb->dataSize );

	if( newData ){
		mb->data = newData;
		return mb->capacity += delta;
	}
	return 0;
}

int growExMemoryBlock( register const size_t neededBlocks, register const MemoryBlock mb ){
	while( mb->used+neededBlocks > mb->capacity )
		if( !growMemoryBlock( mb ) )
			return 0;
	return 1;
}

int incMemoryBlock( register const MemoryBlock mb ){
	return  mb->used+1 <= mb->capacity || growMemoryBlock( mb );
}

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