Untitled
unknown
plain_text
2 years ago
8.7 kB
11
Indexable
#include <stdio.h> #include <string.h> //#include "ex11.h" #define MAX_CHARS 100 #define MAX_REGISTERS 25 typedef struct { char full_name[MAX_CHARS]; int special_discount; } Owner; typedef struct { char name[MAX_CHARS]; char breed[MAX_CHARS]; } AnimalType; typedef struct { char full_name[MAX_CHARS]; Owner owner; AnimalType type; int vaccinated; // 0-not vaccinated 1-vaccinated } Animal; //Do not write the types given in the exercise. // Implement here the procedure "showAnimalsByOwnerName" void showAnimalsByOwnerName(Animal animalesRecibidos[MAX_CHARS], int n_animals, char owner_name[MAX_CHARS]) { int j = 0; int hayAnimal = 0; for (j = 0; j < n_animals; j++) { //strcmp(que, que) -> = 0 si coinciden | != 0 si no coinciden if (!strcmp(owner_name, animalesRecibidos[j].owner.full_name)) { printf("\nName of animal: %s\n",animalesRecibidos[j].full_name); printf("Type of animal: %s\n", animalesRecibidos[j].type.name); printf("Animal breed: %s\n", animalesRecibidos[j].type.breed); printf("Owner name: %s\n", animalesRecibidos[j].owner.full_name); if (animalesRecibidos[j].owner.special_discount == 1) { printf("Special discount\n"); } else { printf("No special discount\n"); } if (animalesRecibidos[j].vaccinated == 1) { printf("Animal vaccinated\n"); } else { printf("Animal not vaccinated\n"); } hayAnimal++; } } // hayAnimal == 0 <---> !hayAnimal if (!hayAnimal) { printf("\nNOT FOUND.\n"); } } // Implement here the procedure "showAnimalsByTypeAndStatus" void showAnimalsByTypeAndStatus(Animal all_animals[MAX_CHARS], int n_animals, char type[MAX_CHARS], char vaccinated) { int j = 0; for (j = 0; j < n_animals; j++) { if (strcmp(type, all_animals[j].type.name) == 0) { //mostrar solo gatos o perros if (vaccinated == 'Y' && all_animals[j].vaccinated == 1) { //muestro vacunados printf("\nName of animal: %s\n",all_animals[j].full_name); printf("Type of animal: %s\n", all_animals[j].type.name); printf("Animal breed: %s\n", all_animals[j].type.breed); printf("Owner name: %s\n", all_animals[j].owner.full_name); if (all_animals[j].owner.special_discount == 1) { printf("Special discount\n"); } else { printf("No special discount\n"); } printf("Animal vaccinated\n"); } else { if(vaccinated == 'N' && all_animals[j].vaccinated == 0) { //muestro no vacunados printf("\nName of animal: %s\n",all_animals[j].full_name); printf("Type of animal: %s\n", all_animals[j].type.name); printf("Animal breed: %s\n", all_animals[j].type.breed); printf("Owner name: %s\n", all_animals[j].owner.full_name); if (all_animals[j].owner.special_discount == 1) { printf("Special discount\n"); } else { printf("No special discount\n"); } printf("Animal not vaccinated\n"); } else { if (vaccinated == 'A') { //muestro todos printf("\nName of animal: %s\n",all_animals[j].full_name); printf("Type of animal: %s\n", all_animals[j].type.name); printf("Animal breed: %s\n", all_animals[j].type.breed); printf("Owner name: %s\n", all_animals[j].owner.full_name); if (all_animals[j].owner.special_discount == 1) { printf("Special discount\n"); } else { printf("No special discount\n"); } if (all_animals[j].vaccinated == 1) { printf("Animal vaccinated\n"); } else { printf("Animal not vaccinated\n"); } } } } } else { if (strcmp(type, "All") == 0) { //mostrar todos if (vaccinated == 'Y' && all_animals[j].vaccinated == 1) { //muestro vacunados printf("\nName of animal: %s\n",all_animals[j].full_name); printf("Type of animal: %s\n", all_animals[j].type.name); printf("Animal breed: %s\n", all_animals[j].type.breed); printf("Owner name: %s\n", all_animals[j].owner.full_name); if (all_animals[j].owner.special_discount == 1) { printf("Special discount\n"); } else { printf("No special discount\n"); } printf("Animal vaccinated\n"); } else { if(vaccinated == 'N' && all_animals[j].vaccinated == 0) { //muestro no vacunados printf("\nName of animal: %s\n",all_animals[j].full_name); printf("Type of animal: %s\n", all_animals[j].type.name); printf("Animal breed: %s\n", all_animals[j].type.breed); printf("Owner name: %s\n", all_animals[j].owner.full_name); if (all_animals[j].owner.special_discount == 1) { printf("Special discount\n"); } else { printf("No special discount\n"); } printf("Animal not vaccinated\n"); } else { if(vaccinated == 'A') { //muestro todos printf("\nName of animal: %s\n",all_animals[j].full_name); printf("Type of animal: %s\n", all_animals[j].type.name); printf("Animal breed: %s\n", all_animals[j].type.breed); printf("Owner name: %s\n", all_animals[j].owner.full_name); if (all_animals[j].owner.special_discount == 1) { printf("Special discount\n"); } else { printf("No special discount\n"); } if (all_animals[j].vaccinated == 1) { printf("Animal vaccinated\n"); } else { printf("Animal not vaccinated\n"); } } } } } } } } int main() { Animal all_animals[MAX_CHARS]; int n_animals = 0; char owner_name[MAX_CHARS], type[MAX_CHARS], vaccinated,aux; //n_animals = loadAnimalsInfo (all_animals); for(int i = 0; i < 4; i++) { printf("Nombre animal: "); fgets(all_animals[i].full_name, MAX_CHARS, stdin); all_animals[i].full_name[strlen(all_animals[i].full_name)-1] = '\0'; all_animals[i].vaccinated = 1; printf("Nombre dueño: "); fgets(all_animals[i].owner.full_name, MAX_CHARS, stdin); all_animals[i].owner.full_name[strlen(all_animals[i].owner.full_name)-1] = '\0'; all_animals[i].owner.special_discount = 0; printf("Tipo animal: "); fgets(all_animals[i].type.name, MAX_CHARS, stdin); all_animals[i].type.name[strlen(all_animals[i].type.name)-1] = '\0'; printf("Raza animal: "); fgets(all_animals[i].type.breed, MAX_CHARS, stdin); all_animals[i].type.breed[strlen(all_animals[i].type.breed)-1] = '\0'; } n_animals = 4; printf ("Enter owner name: "); fgets (owner_name, 100, stdin); owner_name[strlen(owner_name) - 1] = '\0'; showAnimalsByOwnerName(all_animals, n_animals, owner_name); printf ("\nWant to show dogs (Dog) cats (Cat) or both (All)? : "); scanf ("%s", type); scanf("%c", &aux); printf ("\nShow vaccinated (Y), not vaccinated (N) or all (A)? : "); scanf ("%c", &vaccinated); showAnimalsByTypeAndStatus(all_animals, n_animals, type, vaccinated); return 0; }
Editor is loading...