Está en la página 1de 4

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

#define​ dimensionarI(n) (​int​ *) ​malloc​ ( n * ​sizeof​( int ) )


#define​ dimensionarPersona(n) (tipoPersona *) ​malloc​ ( n * ​sizeof​( tipoPersona ) )

const​ ​int​ ​MAX_NOMBRE​ = 30;

typedef​ ​struct
{
​char​ nombre [ ​MAX_NOMBRE​ ];
​int​ edad;
​float​ peso;

} tipoPersona;

int​ leerEntero ( ​char​ pregunta[] );

void​ leerPersona ( tipoPersona *persona );


void​ imprimirPersona ( tipoPersona persona );

tipoPersona *leerGrupo ( char pregunta[], int n );


int​ *ordenarAlfabetico ( tipoPersona *grupo, ​int​ n );
int​ *ordenarEdad ( tipoPersona *grupo, ​int​ n );
void​ imprimirGrupo ( tipoPersona *grupo, ​int​ n, ​int​ *indice );
int​ ​main​ ()
{
tipoPersona *grupoPersonas;
int​ cantidadPersonas;
int​ *indiceAlfabetico;
int​ *indiceEdad;

cantidadPersonas = leerEntero ( "Ingrese la cantidad de personas: ");

grupoPersonas = leerGrupo ( "Ingrese datos del grupo", cantidadPersonas );

indiceAlfabetico = ordenarAlfabetico ( grupoPersonas, cantidadPersonas );


indiceEdad = ordenarEdad ( grupoPersonas, cantidadPersonas );

printf​ ( "\nOrden alfabético\n" );


imprimirGrupo ( grupoPersonas, cantidadPersonas, indiceAlfabetico );

printf​ ( "\nOrden por edad\n\n" );


imprimirGrupo ( grupoPersonas, cantidadPersonas, indiceEdad );

free​ ( indiceAlfabetico );
free​ ( indiceEdad );
free​ ( grupoPersonas );

return​ 0;
}

int​ leerEntero ( ​char​ pregunta[] )


{
int​ d;

printf​ ( "%s", pregunta );


scanf​ ( "%d%*c", &d );

return​ d;
}

void​ leerPersona ( tipoPersona *persona )


{
printf​ ( "Ingrese el nombre: " );
fgets​ ( persona -> nombre, ​MAX_NOMBRE​, stdin );
persona -> nombre[ strlen( persona -> nombre ) - 1 ] = '\0';

printf​ ( "Ingrese la edad: " );


scanf​ ( "%d%*c", &persona -> edad );

printf​ ( "Ingrese el peso: " );


scanf​ ( "%f%*c", &persona -> peso );
}
void​ imprimirPersona ( tipoPersona persona )
{
printf​ ( "%-​30​s %5d %5.2f\n", persona.nombre, persona.edad, persona.peso );
}

tipoPersona *leerGrupo ( ​char​ pregunta[], ​int​ n )


{
int​ i;
tipoPersona *arreglo;

arreglo = dimensionarPersona ( n );

printf​ ( "%s\n", pregunta );

for​ ( i = 0 ; i < n ; i++ )


{
printf​ ( "\nPersona %d de %d:\n", (i+1), n );
leerPersona ( &arreglo[ i ] );
}

return​ arreglo;

int​ *ordenarAlfabetico ( tipoPersona *grupo, ​int​ n )


{
int​ i, j, aux, *indice;

indice = dimensionarI ( n );

for​ ( i = 0 ; i < n ; i++ )


{
indice[ i ] = i;
}

for​ ( i = 0 ; i < n - 1 ; i++ )


{
for​ ( j = i + 1 ; j < n ; j++ )
{
​if​ ( strcmp ( grupo[ indice [ i ] ].nombre, grupo[ indice [ j ] ].nombre ) > 0 )
{
aux = indice [ i ];
indice[ i ] = indice [ j ];
indice[ j ] = aux;
}
}
}

return​ indice;
}
int​ *ordenarEdad ( tipoPersona *grupo, ​int​ n )
{
int i, j, aux, *indice;

indice = dimensionarI ( n );

for​ ( i = 0 ; i < n ; i++ )


{
indice[ i ] = i;
}

for​ ( i = 0 ; i < n - 1 ; i++ )


{
for​ ( j = i + 1 ; j < n ; j++ )
{
if​ ( grupo[ indice [ i ] ].edad < grupo[ indice [ j ] ].edad )
{
aux = indice [ i ];
indice[ i ] = indice [ j ];
indice[ j ] = aux;
}
}
}

return​ indice;
}

void​ imprimirGrupo ( tipoPersona *grupo, ​int​ n, ​int​ *indice )


{
int​ i;

for​ ( i = 0 ; i < n ; i++ )


{
imprimirPersona ( grupo [ indice [ i ] ] );
}
}

También podría gustarte