All pastes #180686 Raw Edit

CIX

public c v1 · immutable
#180686 ·published 2006-09-23 12:59 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>struct mon_this{	char			*value;	unsigned int	value_size;};typedef struct mon_this this;void switch_elements(int *tab, long int i_1, long int i_2) {	int tmp = *(tab + i_1);	*(tab + i_1) = *(tab + i_2);	*(tab + i_2) = tmp;} void show(int *result, int occ) {	int i;	for(i = 0; i < occ; i++) 	{		printf("%d\n", *(result + i));	}}this	**this_new(void){	this	**ma_ref;	this	 *ma_struct;	/*	 */	ma_ref					= (this **)malloc(sizeof(this));	ma_struct				= (this  *)malloc(sizeof(this));	ma_struct->value		= NULL;	ma_struct->value_size	= 0;	*ma_ref					= ma_struct;	return(ma_ref);}void	this_free(this **ma_ref){	this	 *ma_struct	= *ma_ref;	if ( ma_struct->value_size > 0)		free(ma_struct->value); /* on pourrait utiliser (*ma_ref)->value.... */	free(ma_struct);	free(ma_ref);}void	this_size_value(this **ms, unsigned int size) /* ms == ma_struct */{	char	*papa;		papa	= realloc((*ms)->value, size);	if (papa != NULL)	{		(*ms)->value		= papa;		(*ms)->value_size	= size;		/* tu peux ajouter une gestion des actions via un FLAG  l'interrieur de this... */		/* genre ms->flag	= REALLOC_OK; ... */	}}long int fetch (this **result) {	int number;	long int i = 0;	while (scanf("%d\n", &number) == 1) 	{		if (i == 0)		this_size_value(result, sizeof(int));			/* *result = malloc(sizeof(int)); */		else if (i > 20000) 		{			printf("No, too much numbers.\n");			exit(EXIT_FAILURE);		} else			/* realloc(*result, sizeof(int) * (i+1));	*/			this_size_value(result, sizeof(int));		/* l tu devrais utiliser result->value[i] */		*((*result)->value + i) = number;		i++;	} 	return i;} void bubble_sort(int *tab, long int occ) {	int k, i;	for(k = occ; k > 1; k--) 		for(i = 0; i < k; i++)			if (*(tab + i) > *(tab + i + 1))				switch_elements(tab, i, i + 1);}void quick_sort(int *tab, long int occ) {	}void fusion(int *tab, int debut, int milieu, int fin){	int i, j = 0, k = 0;	int tmp[fin - debut];	/*	 * j go from 0 to milieu - 1	 * k go from milieu to fin - 1	 * */	for(i = 0; i < fin - debut; i++)	{		if (milieu - debut > j) {			if (fin - milieu > k) {				if ( *(tab + debut + j) < *(tab + milieu + k))					tmp[i] = *(tab + debut + j++);				else					tmp[i] = *(tab + milieu + k++);			} else {				tmp[i] = *(tab + debut + j++);			}		} else if (fin - milieu > k) {			tmp[i] = *(tab + milieu + k++);		} else			break;				}	for(i = 0; i < fin - debut; i++)		*(tab + debut + i) = tmp[i];}void fusion_sort(int *tab, long int occ) {	if (occ > 1) {		fusion_sort(tab, occ/2);		fusion_sort(tab + occ/2, (occ+1)/2);		fusion(tab, 0, occ/2, occ);	}	} int main(int argc, char **argv) {	int *result = NULL;	this	**data;	data	= this_new();	long int occ; 	//printf("%d\n%d\n--------", 13/2, 13-13/2);	if (argc > 2) 	{		printf("tri takes one arg or none.\n");		return EXIT_FAILURE;	} 	occ = fetch(data); 	if (occ > 0)	switch (argc) 	{		case 1:			//show(result->value, occ);			break;		case 2:			{			char *arg = argv[1];			clock_t c1, c2;			c1 = clock(); 			if (strcmp(arg, "-b") == 0)				bubble_sort(result, occ);			else if (strcmp(arg, "-q") == 0)				quick_sort(result, occ);			else if (strcmp(arg, "-f") == 0)				fusion_sort(result, occ); 			c2 = clock();			printf("Time : %lf\n", (double)(c2 - c1));			//show(result, occ);			}			break;	} 	this_free(data);	result = NULL; 	return EXIT_SUCCESS;}