/* * File: ejer_C_1.c * Author: Paola Budan * * Created on September ...

26 sept. 2012 - File: ejer_C_1.c. * Author: Paola Budan. *. * Created on September 26, 2012, 9:02 PM. */. #include
105KB Größe 37 Downloads 52 Ansichten
/* * File: ejer_C_1.c * Author: Paola Budan * * Created on September 26, 2012, 9:02 PM */

#include #include #include #define MAXDIR 5 #define FALSE 0 #define TRUE 1

struct direccion { char nombre[40]; char calle[40]; char ciudad[20]; char codpostal[20]; char provin[25]; };

void cargarArreglo(struct direccion *d); void ordenarArreglo(struct direccion *d); void imprimirArreglo(struct direccion *d);

/* * */ int main(int argc, char** argv) {

struct direccion dir[MAXDIR];

printf("Carga del arreglo\n"); printf("=================\n"); cargarArreglo(dir); printf("\nOrdenando el arreglo...\n"); ordenarArreglo(dir); imprimirArreglo(dir); return (EXIT_SUCCESS); }

void imprimirArreglo(struct direccion *d) { int i; for (i = 0; i < MAXDIR; ++i) { printf("%d° elemento:\n", i + 1); printf("Nombre: %s\n", (d + i)->nombre); printf("Calle: %s\n", (d + i)->calle); printf("Ciudad: %s\n", (d + i)->ciudad); printf("Codigo Postal: %s\n", (d + i)->codpostal);

printf("Provincia: %s\n", (d + i)->provin);

} }

void ordenarArreglo(struct direccion *d) { int i, j; struct direccion aux;

i = 0; while (i < MAXDIR - 1) { j = i + 1; while (j < MAXDIR) { if (strcmp(d[i].nombre, d[j].nombre) > 0) { strcpy(aux.calle, d[i].calle); strcpy(d[i].calle, d[j].calle); strcpy(d[j].calle, aux.calle);

strcpy(aux.ciudad, d[i].ciudad); strcpy(d[i].ciudad, d[j].ciudad); strcpy(d[j].ciudad, aux.ciudad);

strcpy(aux.codpostal, d[i].codpostal); strcpy(d[i].codpostal, d[j].codpostal); strcpy(d[j].codpostal, aux.codpostal);

strcpy(aux.nombre, d[i].nombre); strcpy(d[i].nombre, d[j].nombre); strcpy(d[j].nombre, aux.nombre);

strcpy(aux.provin, d[i].provin); strcpy(d[i].provin, d[j].provin); strcpy(d[j].provin, aux.provin); } ++j; } ++i; }

}

void cargarArreglo(struct direccion *d) { int i; for (i = 0; i < MAXDIR; ++i) { printf("%d° elemento:\n", i + 1); printf("Ingrese el nombre: "); //scanf("%s", (d + i)->nombre); gets((d + i)->nombre); printf("Ingrese la calle: "); //scanf("%s", (d + i)->calle); gets((d + i)->calle); printf("Ingrese la ciudad: ");

//scanf("%s", (d + i)->ciudad); gets((d + i)->ciudad); printf("Ingrese el Codigo Postal: "); //scanf("%s", (d + i)->codpostal); gets((d + i)->codpostal); printf("Ingrese la provincia: "); //scanf("%s", (d + i)->provin); gets((d + i)->provin); } }