void fileHandle();
static HausTier_t Anfangsbestand[] = { { "Mau", Katze, { 7, 6, 2017 } }, { "Bello", Hund, { 4, 2, 2010 } }, { "Spitz", Maus, { 2, 11, 2018 } }, };
static HausTier_t *bestand = NULL; static int tierZahl = 0;
static void hausTierTypAusgeben(const HausTierTyp_t typ); static void datumAusgeben(const Datum_t *dat);
void HausTierAusgeben(const HausTier_t *tier) { printf(" Name: %s\n", tier->name); printf(" Typ: "); hausTierTypAusgeben(tier->typ); printf("\n"); printf(" Geboren: "); datumAusgeben(&tier->gebDat); printf("\n"); }
static void hausTierTypAusgeben(const HausTierTyp_t typ) { switch (typ) { case Katze: printf("Katze"); break; case Hund: printf("Hund"); break; case Maus: printf("Maus"); break; case Fisch: printf("Fisch"); break; case Vogel: printf("Vogel"); break; default: printf("Irgendwas"); break; } }
static void datumAusgeben(const Datum_t *dat) { printf("%d.%d.%d", dat->tag, dat->monat, dat->jahr); }
void HausTiereAusgeben(void) { printf("Haustierbestand:\n\n"); for (int pos = 0; pos < tierZahl; pos++) { if (bestand[pos].name[0]) { HausTierAusgeben(&bestand[pos]); printf("\n"); } } }
int HausTierVerwInit(void) { if ((bestand = malloc(sizeof(Anfangsbestand))) == NULL) return(HAUSTIER_VERWINI_FEHL);
int pos; for (pos = 0; pos < sizeof(Anfangsbestand) / sizeof(HausTier_t); pos++) { bestand[pos] = Anfangsbestand[pos]; } tierZahl = pos;
return(HAUSTIER_VERWINI_OK); }
void HausTierVerwExit(void) { free(bestand); }
int HausTierEintragen(const HausTier_t tier) { / Positionsvariablen fuer freie Position im Tierbestand und Position mit identischem Eintrag (Name) */ int posFrei = -1, posIdent = -1; int pos; int stat = HAUSTIER_EIN_FEHL_ALLG;
for (pos = 0; pos < tierZahl; pos++) { if (bestand[pos].name[0]) { if (strcmp(bestand[pos].name, tier->name) == 0) posIdent = pos; } else if (posFrei < 0) { posFrei = pos; } }
if (posIdent >= 0) stat = HAUSTIER_EIN_FEHL_EXISTIERT; else { if (posFrei < 0) { HausTier_t *bestandNeu = realloc(bestand, (tierZahl + 1) * sizeof(HausTier_t));
if (bestandNeu != NULL)
{
bestand = bestandNeu;
posFrei = tierZahl;
tierZahl++;
}
}
if (posFrei < 0) stat = HAUSTIER_EIN_FEHL_VOLL;
else
{
/* Freie Position gefunden und Eintrag existiert
noch nicht */
bestand[posFrei] = *tier;
stat = HAUSTIER_EIN_OK;
}
}
return(stat); }
int HausTierLoeschen(const char *name) { int pos; int stat = HAUSTIER_LOE_FEHL_ALLG;
for (pos = 0; pos < tierZahl; pos++) { if (strcmp(bestand[pos].name, name) == 0) break; }
if (pos >= tierZahl) stat = HAUSTIER_LOE_FEHL_EXNICHT; else { bestand[pos].name[0] = 0; stat = HAUSTIER_LOE_OK; }
return(stat); }