1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void bubbleSort(char **tab, int (*cmp)(void *, void *))
{
char **p1, **p2;
for(p1=tab; *p1!=NULL; ++p1)
for(p2=p1+1; *p2!=NULL; ++p2)
if((*cmp)(p1,p2)>0)
{
char *tmp;
tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
}
void printTable(FILE *fdo, char **tab)
{
for(; *tab!=NULL; ++tab) fprintf(fdo,"%s\n", *tab);
}
int main(int argc, char *argv[])
{
char **t; unsigned int i,l;
if((t=(char**)malloc(sizeof(char*)*argc))==NULL)
{
fprintf(stderr,"Error in memory allocation\n");
return EXIT_FAILURE;
}
l=(unsigned int)argc-1U;
for(i=0U; i<l; ++i)
{
if((t[i]=(char*)malloc(strlen(argv[i+1])+1))==NULL)
{
fprintf(stderr,"Error in memory allocation\n");
return EXIT_FAILURE;
}
strcpy(t[i],argv[i+1]);
}
t[l]=NULL;
printTable(stdout,t);
bubbleSort(t, (int (*)(void *, void *))&strcmp);
printf("\n");
printTable(stdout, t);
return EXIT_SUCCESS;
} |
Partager