diff --git a/P06_Lösung_Andrin/list.h b/P06_Lösung_Andrin/list.h new file mode 100644 index 0000000..4a5830d --- /dev/null +++ b/P06_Lösung_Andrin/list.h @@ -0,0 +1,17 @@ +#include "person.h" +#include +#ifndef LIST_H +#define LIST_H + +typedef struct node { + person_t content; + struct node *next; +} node_t; + +int insert_list(node_t *,person_t *); +int remove_list(node_t *,person_t *); +void show_list(node_t *); +void clear_list(node_t *); + + +#endif diff --git a/P06_Lösung_Andrin/main.c b/P06_Lösung_Andrin/main.c new file mode 100644 index 0000000..b7b6f51 --- /dev/null +++ b/P06_Lösung_Andrin/main.c @@ -0,0 +1,119 @@ +/* ---------------------------------------------------------------------------- + * -- _____ ______ _____ - + * -- |_ _| | ____|/ ____| - + * -- | | _ __ | |__ | (___ Institute of Embedded Systems - + * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - + * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - + * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - + * ---------------------------------------------------------------------------- + */ +/** + * @file + * @brief Lab implementation + */ +#include +#include +#include "person.h" +#include "list.h" +#include +#define MAX_AGE 200 +#define MIN_AGE 0 + +void getPerson(person_t *neu) { + + int run = 1; + + while(run == 1) { + printf("Enter Name: "); + char input[100]; + scanf("%s",input); // evt limite + + if(strlen(input) <= NAME_LEN) { + strcpy(neu->name,input); + run = 0; + } else { + printf("Falsche Eingabe!!\n"); + } + while(getchar() != '\n') {} + } + + run = 1; + + while(run == 1) { + printf("Enter First Name: "); + char input[100]; + scanf("%s",input); + + if(strlen(input) <= NAME_LEN) { + strcpy(neu->first_name,input); + run = 0; + } else { + printf("Falsche Eingabe!!\n"); + } + while(getchar() != '\n') { + } + } + + run = 1; + + while(run == 1) { + int age = -1; + printf("Age: "); + if(scanf("%d",&age) != 0) { + neu->age = age; + run = 0; + } else { + printf("Falsche Eingabe!!\n"); + } + while(getchar() != '\n') { + } + } +} + +void insert(node_t *liste) { + person_t neu; + getPerson(&neu); + insert_list(liste,&neu); +} + +void removeIt(node_t *liste) { + person_t del; + getPerson(&del); + remove_list(liste,&del); +} + + +/** + * @brief Main entry point. + * @param[in] argc The size of the argv array. + * @param[in] argv The command line arguments... + * @returns Returns EXIT_SUCCESS (=0) on success, EXIT_FAILURE (=1) there is an expression syntax error. + */ +int main(int argc, char* argv[]) +{ + // BEGIN-STUDENTS-TO-ADD-CODE + node_t personen_liste; + personen_liste.next = &personen_liste; + int run = 1; + + while(run == 1) { + char cmd[100]; + printf("I to Insert new Person\nR to Remove Person\nS to Show List\nC to Clear all\nE to End\n-> "); + scanf("%s",cmd); + if(cmd[0] == 'I') { + insert(&personen_liste); + }else if(cmd[0] == 'E') { + run = 0; + }else if(cmd[0] == 'R') { + removeIt(&personen_liste); + }else if(cmd[0] == 'S') { + show_list(&personen_liste); + }else if(cmd[0] == 'C') { + clear_list(&personen_liste); + } + + } + + // END-STUDENTS-TO-ADD-CODE + return EXIT_SUCCESS; +} diff --git a/P06_Lösung_Andrin/person.c b/P06_Lösung_Andrin/person.c new file mode 100644 index 0000000..794ea1e --- /dev/null +++ b/P06_Lösung_Andrin/person.c @@ -0,0 +1,17 @@ +#include "person.h" +#include + +int person_compare(const person_t *a, const person_t *b) { + int result; + result = strcmp(a->name,b->name); + if(result) { + return result; + } + + result = strcmp(a->first_name,b->first_name); + if(result) { + return result; + } + + return a->age - b->age; +} diff --git a/P06_Lösung_Andrin/person.h b/P06_Lösung_Andrin/person.h new file mode 100644 index 0000000..5f82731 --- /dev/null +++ b/P06_Lösung_Andrin/person.h @@ -0,0 +1,14 @@ +#ifndef PERSON_H +#define PERSON_H + +#define NAME_LEN 20 + +typedef struct { + char name[NAME_LEN]; + char first_name[NAME_LEN]; + unsigned int age; +} person_t; + +int person_compare(const person_t *a, const person_t *b); + +#endif