funciones
Diferencias
Muestra las diferencias entre dos versiones de la página.
| Próxima revisión | Revisión previa | ||
| funciones [2012/08/27 00:09] – creado lmateu | funciones [2012/08/27 01:01] (actual) – lmateu | ||
|---|---|---|---|
| Línea 5: | Línea 5: | ||
| < | < | ||
| - | | + | |
| - | + | ||
| - | void swap(void *v[], int i, int j) | + | void swap(void *v[], int i, int j) { |
| - | | + | void *aux; |
| - | void *aux; | + | |
| - | + | aux= v[i]; | |
| - | aux=v[i]; | + | v[i]= v[j]; |
| - | v[i]=v[j]; | + | v[j]= aux; |
| - | v[j]=aux; | + | } |
| - | } | + | |
| - | + | void qsort(void *a[], int left, int right, | |
| - | void qsort(void *a[], int left, int right, | + | |
| - | | + | int i, last; |
| - | | + | |
| - | int i, last; | + | if (left> |
| - | + | return; | |
| - | if(left> | + | |
| - | return; | + | swap(a, left, (left+right)/ |
| - | + | last= left; | |
| - | swap(a, left, (left+right)/ | + | |
| - | last=left; | + | /* |
| - | + | +--+-----------+--------+--------------+ | |
| - | /* | + | | |/////////// |
| - | +--+-----------+--------+--------------+ | + | +--+-----------+--------+--------------+ |
| - | | |/////////// | + | left last |
| - | +--+-----------+--------+--------------+ | + | */ |
| - | left last | + | |
| - | */ | + | for (i= left+1; i< |
| - | + | if ((*compare)(a[i], | |
| - | for(i=left+1; | + | swap(a, ++last, i); |
| - | if((*compare)(a[i], | + | swap(a, left, last); |
| - | swap(a, ++last, i); | + | |
| - | swap(a, left, last); | + | qsort(a, left, last-1, compare); |
| - | + | qsort(a, last+1, right, compare); | |
| - | qsort(a, left, last-1, compare); | + | } |
| - | qsort(a, last+1, right, compare); | + | </ |
| - | } | + | |
| + | La declaración '' | ||
| + | a una función. | ||
| + | tipo p y q, la expresion '' | ||
| + | |||
| + | === Primer ejemplo de uso === | ||
| + | |||
| + | El siguiente programa utiliza la función anterior para ordenar líneas lexicográficamente: | ||
| + | |||
| + | < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | void qsort(void *a[], int left, int right, | ||
| + | int (*compare)(void *, void *)); | ||
| + | |||
| + | #define ANCHO 1000 | ||
| + | #define MAX 10000 | ||
| + | |||
| + | int main() { | ||
| + | char s[ANCHO]; | ||
| + | char *linea[MAX]; | ||
| + | int i, j; | ||
| + | /* El siguiente es el criterio para las comparaciones */ | ||
| + | int (*compare)(void *, void *)= (int (*)(void *, void*)) strcmp; /* Ver nota */ | ||
| + | |||
| + | for (i= 0; fgets(s, ANCHO, stdin)!=NULL; | ||
| + | linea[i]= strdup(s); | ||
| + | |||
| + | qsort((void **)linea, 0, i-1, compare); | ||
| + | |||
| + | for (j= 0; j<i; ++j) | ||
| + | fputs(linea[j], | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | La expresión '' | ||
| + | para compatibilizar strcmp con el tipo de la variable asignada. | ||
| + | Si se pasa directamente strcmp a qsort, el compilador podría reclamar conflicto | ||
| + | de tipos. | ||
| + | |||
| + | Otra forma de hacer esto mismo: | ||
| + | |||
| + | < | ||
| + | typedef int (*Comparator)(void *, void *); | ||
| + | |||
| + | void qsort(void *a[], int left, int right, Comparator compare) { | ||
| + | ... | ||
| + | } | ||
| + | |||
| + | int main() { | ||
| + | ... | ||
| + | Comparator compare= (Comparator)strcmp; | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Lo cual es mucho más legible. | ||
| + | |||
| + | === Segundo ejemplo de uso === | ||
| + | |||
| + | < | ||
| + | int numcmp(char *s1, char *s2) /* compara numéricamente */ { | ||
| + | int i1, i2; | ||
| + | |||
| + | i1= atoi(s1); | ||
| + | i2= atoi(s2); | ||
| + | |||
| + | return i1<i2? -1 : i1==i2? 0 : 1; | ||
| + | } | ||
| + | |||
| + | main() { | ||
| + | char s[ANCHO]; | ||
| + | char *linea[MAX]; | ||
| + | int i, j; | ||
| + | Comparator compare= (Comparator)numcmp; | ||
| + | |||
| + | for (i= 0; fgets(s, ANCHO, stdin)!=NULL; | ||
| + | linea[i]= strdup(s); | ||
| + | |||
| + | qsort((void **)linea, 0, i-1, compare); | ||
| + | |||
| + | for (j= 0; j<i; ++j) | ||
| + | fputs(linea[j], | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Estudie en los [[http:// | ||
| + | un programa que recibe la opción " | ||
| + | por omisión ordena alfabéticamente. | ||
funciones.1346026149.txt.gz · Última modificación: por lmateu
