From 596128d3565737dd52519b0d5cce9c67875a71b9 Mon Sep 17 00:00:00 2001 From: paulusja Date: Thu, 29 Oct 2026 14:32:39 +0100 Subject: [PATCH] Add main-/make-file. --- 05_structs/main.c | 98 +++++++++++++++++++++++++++++++++++++++++++++ 05_structs/makefile | 16 ++++++++ 2 files changed, 114 insertions(+) create mode 100644 05_structs/main.c create mode 100644 05_structs/makefile diff --git a/05_structs/main.c b/05_structs/main.c new file mode 100644 index 0000000..7f78c12 --- /dev/null +++ b/05_structs/main.c @@ -0,0 +1,98 @@ +/* + * Vervollständigen Sie das untenstehende Programm, indem Sie das Modul namesarchive in der + * namesarchive.h bzw. namesarchive.c implementieren. + * Die öffentlichen Schnittstellen mit genaueren Details entnehmen Sie der namesarchive.h. + * Das Programm soll das Modul als dynamische Laufzeitbibliothek nutzen. + * + */ + +#include +#include +#include +#include "namesarchive.h" + +#define MAX_BUFFER_LEN 100 + +int inputPerson(Person *person); +int inputName(char *buffer, unsigned int bufferSize); +int inputAge(); + +int main() +{ + Person person; + + printf("Wer soll hinzugefuegt werden?\n"); + + while(inputPerson(&person)) + { + if(addPerson(person)) + printf("%s (Alter: %u) wurde dem Archiv hinzugefuegt.\n", person.name, person.age); + else + printf("Fehler! %s (Alter: %u) konnte nicht hinzugefuegt werden.\n", person.name, person.age); + } + + printf("\nNach Eingabe:\n"); + printPersons(); + + printf("\nNach Sortierung:\n"); + sortPersons(); + printPersons(); + printf("\n"); + printf("Wer soll entfernt werden?\n"); + + while(inputPerson(&person)) + { + if(removePerson(person)) + printf("%s (Alter: %u) wurde aus dem Archiv entfernt.\n", person.name, person.age); + else + printf("Fehler! %s (Alter: %u) konnte nicht gefunden werden.\n", person.name, person.age); + } + + printf("\nNach Entfernen:\n"); + printPersons(); + + return EXIT_SUCCESS; +} + +int inputPerson(Person *person) +{ + if(inputName(person->name, MAX_NAME_LEN)) + { + person->age = inputAge(); + + return 1; + } + else + return 0; +} + +int inputName(char *buffer, unsigned int bufferSize) +{ + printf("Name: "); + if(fgets(buffer, bufferSize, stdin) != NULL) + { + if(buffer[strlen(buffer)-1] == '\n') + buffer[strlen(buffer)-1] = '\0'; + else + while(getchar() != '\n') {} + + return strlen(buffer) > 0; + } + else + return 0; +} + +int inputAge() +{ + int age = 0; + printf("Alter: "); + + while(scanf("%d", &age) != 1 || age <= 0) + { + printf("Fehler! Erneute Eingabe: "); + while(getchar() != '\n') {} + } + while(getchar() != '\n') {} + + return age; +} \ No newline at end of file diff --git a/05_structs/makefile b/05_structs/makefile new file mode 100644 index 0000000..e8014a9 --- /dev/null +++ b/05_structs/makefile @@ -0,0 +1,16 @@ +namesarchive: main.o libnamesarchive.so + gcc -Wall -o namesarchive main.o libnamesarchive.so + +main.o: main.c + gcc -c -Wall main.c + +namesarchive.o: namesarchive.c namesarchive.h + gcc -c -Wall namesarchive.c + +libnamesarchive.so: namesarchive.o + gcc -shared -o libnamesarchive.so namesarchive.o + +clean: + del /f namesarchive.exe *.o *.so +# Comment line above and uncomment line below for UNIX systems +# rm -f namesarchive *.o *.so \ No newline at end of file