diff --git a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/Makefile b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/Makefile index b363162..cedd6d6 100644 --- a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/Makefile +++ b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/Makefile @@ -2,7 +2,7 @@ SNP_SHARED_MAKEFILE := $(if $(SNP_SHARED_MAKEFILE),$(SNP_SHARED_MAKEFILE),"~/snp TARGET := bin/personen-verwaltung # BEGIN-STUDENTS-TO-ADD-CODE -MODULES := +MODULES := src/person.c src/list.c src/read.c # END-STUDENTS-TO-ADD-CODE SOURCES := src/main.c $(MODULES) TSTSOURCES := tests/tests.c $(MODULES) diff --git a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/bin/personen-verwaltung b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/bin/personen-verwaltung new file mode 100755 index 0000000..e6c3cea Binary files /dev/null and b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/bin/personen-verwaltung differ diff --git a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/list.c b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/list.c new file mode 100644 index 0000000..235f4da --- /dev/null +++ b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/list.c @@ -0,0 +1,44 @@ +/* ---------------------------------------------------------------------------- + * -- _____ ______ _____ - + * -- |_ _| | ____|/ ____| - + * -- | | _ __ | |__ | (___ Institute of Embedded Systems - + * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - + * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - + * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - + * ---------------------------------------------------------------------------- + */ +/** + * @file + * @brief Lab implementation + */ +#include +#include + +#include "list.h" + +void insertPerson(void){ + person_t person; + printf("\ninsert\n"); + int success = create_person(&person); + printf("success: %d\n", success); + printf("firstname: %s lastname: %s age: %d\n", person.first_name, person.name, person.age); + return; +} + + +void removePerson(void){ + printf("\nremove\n"); + return; +} + + +void showPerson(void){ + printf("\nshow\n"); + return; +} + + +void clearPerson(void){ + printf("\nclear\n"); + return; +} \ No newline at end of file diff --git a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/list.d b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/list.d new file mode 100644 index 0000000..d8d9f18 --- /dev/null +++ b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/list.d @@ -0,0 +1,22 @@ +src/list.o: src/list.c /usr/include/stdc-predef.h /usr/include/stdio.h \ + /usr/include/x86_64-linux-gnu/bits/libc-header-start.h \ + /usr/include/features.h /usr/include/x86_64-linux-gnu/sys/cdefs.h \ + /usr/include/x86_64-linux-gnu/bits/wordsize.h \ + /usr/include/x86_64-linux-gnu/bits/long-double.h \ + /usr/include/x86_64-linux-gnu/gnu/stubs.h \ + /usr/include/x86_64-linux-gnu/gnu/stubs-64.h \ + /usr/lib/gcc/x86_64-linux-gnu/7/include/stddef.h \ + /usr/include/x86_64-linux-gnu/bits/types.h \ + /usr/include/x86_64-linux-gnu/bits/typesizes.h \ + /usr/include/x86_64-linux-gnu/bits/types/__FILE.h \ + /usr/include/x86_64-linux-gnu/bits/types/FILE.h \ + /usr/include/x86_64-linux-gnu/bits/libio.h \ + /usr/include/x86_64-linux-gnu/bits/_G_config.h \ + /usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h \ + /usr/lib/gcc/x86_64-linux-gnu/7/include/stdarg.h \ + /usr/include/x86_64-linux-gnu/bits/stdio_lim.h \ + /usr/include/x86_64-linux-gnu/bits/sys_errlist.h /usr/include/stdlib.h \ + /usr/include/x86_64-linux-gnu/bits/floatn.h \ + /usr/include/x86_64-linux-gnu/bits/floatn-common.h \ + /usr/include/x86_64-linux-gnu/bits/stdlib-float.h src/list.h \ + src/person.h diff --git a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/list.h b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/list.h new file mode 100644 index 0000000..32f7f68 --- /dev/null +++ b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/list.h @@ -0,0 +1,17 @@ +#ifndef _LIST_H_ +#define _LIST_H_ + +#include "person.h" + +typedef struct node { + person_t content; + struct node *next; +} node_t; + + +void insertPerson(void); +void removePerson(void); +void showPerson(void); +void clearPerson(void); + +#endif // _LIST_H_ diff --git a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/list.o b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/list.o new file mode 100644 index 0000000..15f97de Binary files /dev/null and b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/list.o differ diff --git a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/main.c b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/main.c index 3dd772a..38075a2 100644 --- a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/main.c +++ b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/main.c @@ -13,6 +13,14 @@ */ #include #include +#include + +#include "person.h" +#include "list.h" +#include "read.h" + + +int toupper(int ch); /** * @brief Main entry point. @@ -23,6 +31,39 @@ int main(int argc, char* argv[]) { // BEGIN-STUDENTS-TO-ADD-CODE + char command; + char input; + int end = 0; + while(end == 0){ + printf("\nPlease choose a command:\nI(nsert)\nR(emove)\nS(how)\nC(lear)\nE(nd)\n"); + input = getchar(); + command = input; + while((input != EOL) && (input != EOF)){ + input = getchar(); + } + input = toupper(command); + switch(input){ + case 73: // I + insertPerson(); + break; + case 82: // R + removePerson(); + break; + case 83: // S + showPerson(); + break; + case 67: // C + clearPerson(); + break; + case 69: // E + end = 1; + printf("\nProgramm is ending\n"); + break; + default: + printf("\nCommand not found!\n"); + } + } + // END-STUDENTS-TO-ADD-CODE return EXIT_SUCCESS; diff --git a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/main.d b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/main.d new file mode 100644 index 0000000..7b1ff2d --- /dev/null +++ b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/main.d @@ -0,0 +1,22 @@ +src/main.o: src/main.c /usr/include/stdc-predef.h /usr/include/stdio.h \ + /usr/include/x86_64-linux-gnu/bits/libc-header-start.h \ + /usr/include/features.h /usr/include/x86_64-linux-gnu/sys/cdefs.h \ + /usr/include/x86_64-linux-gnu/bits/wordsize.h \ + /usr/include/x86_64-linux-gnu/bits/long-double.h \ + /usr/include/x86_64-linux-gnu/gnu/stubs.h \ + /usr/include/x86_64-linux-gnu/gnu/stubs-64.h \ + /usr/lib/gcc/x86_64-linux-gnu/7/include/stddef.h \ + /usr/include/x86_64-linux-gnu/bits/types.h \ + /usr/include/x86_64-linux-gnu/bits/typesizes.h \ + /usr/include/x86_64-linux-gnu/bits/types/__FILE.h \ + /usr/include/x86_64-linux-gnu/bits/types/FILE.h \ + /usr/include/x86_64-linux-gnu/bits/libio.h \ + /usr/include/x86_64-linux-gnu/bits/_G_config.h \ + /usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h \ + /usr/lib/gcc/x86_64-linux-gnu/7/include/stdarg.h \ + /usr/include/x86_64-linux-gnu/bits/stdio_lim.h \ + /usr/include/x86_64-linux-gnu/bits/sys_errlist.h /usr/include/stdlib.h \ + /usr/include/x86_64-linux-gnu/bits/floatn.h \ + /usr/include/x86_64-linux-gnu/bits/floatn-common.h \ + /usr/include/x86_64-linux-gnu/bits/stdlib-float.h /usr/include/string.h \ + src/person.h src/list.h src/read.h diff --git a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/main.o b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/main.o new file mode 100644 index 0000000..adc5905 Binary files /dev/null and b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/main.o differ diff --git a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/person.c b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/person.c new file mode 100644 index 0000000..02f3406 --- /dev/null +++ b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/person.c @@ -0,0 +1,86 @@ +/* ---------------------------------------------------------------------------- + * -- _____ ______ _____ - + * -- |_ _| | ____|/ ____| - + * -- | | _ __ | |__ | (___ Institute of Embedded Systems - + * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - + * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - + * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - + * ---------------------------------------------------------------------------- + */ +/** + * @file + * @brief Lab implementation + */ +#include +#include +#include +#include "read.h" +#include "person.h" + +int person_compare(const person_t *a, const person_t *b){ + //compaire name + int result = strcmp(a->name, b->name); + //if name is the same compare first_name + if(result == 0){ + result = strcmp(a->first_name, b->first_name); + } + //if first_name is the same compare age + if(result == 0){ + result = a->age - b->age; + } + return result; +} + + +int create_person(person_t *person){ + char input; + int count; + + //read last name + printf("Please enter Last Name:\n"); + count = 0; + input = getchar(); + if((input == EOL) || (input == EOF)){ + return 0; + } + while((input != EOL) && (input != EOF)){ + if(count < NAME_LEN){ + person->name[count] = input; + } + count ++; + input = getchar(); + } + if(count > NAME_LEN){ + return 0; + } + person->name[count] = '\0'; + + //read first name + printf("Please enter First Name:\n"); + count = 0; + input = getchar(); + if((input == EOL) || (input == EOF)){ + return 0; + } + while((input != EOL) && (input != EOF)){ + if(count < NAME_LEN){ + person->first_name[count] = input; + } + count ++; + input = getchar(); + } + if(count > NAME_LEN){ + return 0; + } + person->first_name[count] = '\0'; + + //read age + printf("Please enter age:\n"); + int age = getInt(100); + if(age >= 0){ + person->age = age; + return 1; + } + return 0; + +} diff --git a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/person.d b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/person.d new file mode 100644 index 0000000..cd2573e --- /dev/null +++ b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/person.d @@ -0,0 +1,23 @@ +src/person.o: src/person.c /usr/include/stdc-predef.h \ + /usr/include/stdio.h \ + /usr/include/x86_64-linux-gnu/bits/libc-header-start.h \ + /usr/include/features.h /usr/include/x86_64-linux-gnu/sys/cdefs.h \ + /usr/include/x86_64-linux-gnu/bits/wordsize.h \ + /usr/include/x86_64-linux-gnu/bits/long-double.h \ + /usr/include/x86_64-linux-gnu/gnu/stubs.h \ + /usr/include/x86_64-linux-gnu/gnu/stubs-64.h \ + /usr/lib/gcc/x86_64-linux-gnu/7/include/stddef.h \ + /usr/include/x86_64-linux-gnu/bits/types.h \ + /usr/include/x86_64-linux-gnu/bits/typesizes.h \ + /usr/include/x86_64-linux-gnu/bits/types/__FILE.h \ + /usr/include/x86_64-linux-gnu/bits/types/FILE.h \ + /usr/include/x86_64-linux-gnu/bits/libio.h \ + /usr/include/x86_64-linux-gnu/bits/_G_config.h \ + /usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h \ + /usr/lib/gcc/x86_64-linux-gnu/7/include/stdarg.h \ + /usr/include/x86_64-linux-gnu/bits/stdio_lim.h \ + /usr/include/x86_64-linux-gnu/bits/sys_errlist.h /usr/include/stdlib.h \ + /usr/include/x86_64-linux-gnu/bits/floatn.h \ + /usr/include/x86_64-linux-gnu/bits/floatn-common.h \ + /usr/include/x86_64-linux-gnu/bits/stdlib-float.h /usr/include/string.h \ + src/read.h src/person.h diff --git a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/person.h b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/person.h new file mode 100644 index 0000000..816888d --- /dev/null +++ b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/person.h @@ -0,0 +1,18 @@ + +#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); +int create_person(person_t *person); + +#endif // _PERSON_H_ diff --git a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/person.o b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/person.o new file mode 100644 index 0000000..ae07f16 Binary files /dev/null and b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/person.o differ diff --git a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/read.c b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/read.c new file mode 100644 index 0000000..2b998bd --- /dev/null +++ b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/read.c @@ -0,0 +1,67 @@ +/* ---------------------------------------------------------------------------- + * -- _____ ______ _____ - + * -- |_ _| | ____|/ ____| - + * -- | | _ __ | |__ | (___ Institute of Embedded Systems - + * -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur - + * -- _| |_| | | | |____ ____) | (University of Applied Sciences) - + * -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland - + * ---------------------------------------------------------------------------- + */ +/** + * @file + * @brief Lab implementation + */ +// begin students to add code for task 4.1 +#include +#include "read.h" +int getInt(int maxResult) { + char buffer[BUFFERSIZE] = {0}; + int result = 0; + + // read line: up to EOL or EOF (i.e. error while reading) + int bytes = 0; + int input = getchar(); + while ((input != EOL) && (input != EOF)) { // read whole line + if (bytes < BUFFERSIZE) { // only buffer first n characters + buffer[bytes] = (char)input; + bytes++; + } else { + result = PARSE_ERROR; // exceed buffer size, continue read line + } + input = getchar(); + } + if (input == EOF) { + result = READ_ERROR; + } + // check for numbers: skip leading and trailing spaces + // (i.e. this includes all control chars below the space ASCII code) + int pos = 0; + while((pos < bytes) && (buffer[pos] <= ASCII_SPACE)) pos++; // skip SP + int posOfFirstDigit = pos; + int posOfLastDigit = NO_POS; + while ((pos < bytes) + && (buffer[pos] >= ASCII_DIGIT_0) + && (buffer[pos] <= ASCII_DIGIT_9)) + { + posOfLastDigit = pos; + pos++; + } + while((pos < bytes) && (buffer[pos] <= ASCII_SPACE)) pos++; // skip SP + // produce return value + if (result != 0) { + // previously detected read or parse error given + } else if ((pos != bytes) || (posOfLastDigit == NO_POS)) { + result = PARSE_ERROR; + } else { // convert number + for(int i = posOfFirstDigit; i <= posOfLastDigit; i++) { + result = result * 10 + (buffer[i] - ASCII_DIGIT_0); + if (result > maxResult) { + result = PARSE_ERROR; + break; + } + } + } + return result; +} + +// end students to add code diff --git a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/read.d b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/read.d new file mode 100644 index 0000000..41dbb9d --- /dev/null +++ b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/read.d @@ -0,0 +1,18 @@ +src/read.o: src/read.c /usr/include/stdc-predef.h /usr/include/stdio.h \ + /usr/include/x86_64-linux-gnu/bits/libc-header-start.h \ + /usr/include/features.h /usr/include/x86_64-linux-gnu/sys/cdefs.h \ + /usr/include/x86_64-linux-gnu/bits/wordsize.h \ + /usr/include/x86_64-linux-gnu/bits/long-double.h \ + /usr/include/x86_64-linux-gnu/gnu/stubs.h \ + /usr/include/x86_64-linux-gnu/gnu/stubs-64.h \ + /usr/lib/gcc/x86_64-linux-gnu/7/include/stddef.h \ + /usr/include/x86_64-linux-gnu/bits/types.h \ + /usr/include/x86_64-linux-gnu/bits/typesizes.h \ + /usr/include/x86_64-linux-gnu/bits/types/__FILE.h \ + /usr/include/x86_64-linux-gnu/bits/types/FILE.h \ + /usr/include/x86_64-linux-gnu/bits/libio.h \ + /usr/include/x86_64-linux-gnu/bits/_G_config.h \ + /usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h \ + /usr/lib/gcc/x86_64-linux-gnu/7/include/stdarg.h \ + /usr/include/x86_64-linux-gnu/bits/stdio_lim.h \ + /usr/include/x86_64-linux-gnu/bits/sys_errlist.h src/read.h diff --git a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/read.h b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/read.h new file mode 100644 index 0000000..88eb750 --- /dev/null +++ b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/read.h @@ -0,0 +1,15 @@ +#ifndef READ_H +#define READ_H + +#define EOL 10 +#define PARSE_ERROR -1 +#define READ_ERROR -2 +#define ASCII_SPACE 32 +#define ASCII_DIGIT_0 48 +#define ASCII_DIGIT_9 57 +#define NO_POS -1 +#define BUFFERSIZE 10 + +int getInt(int maxResult); + +#endif diff --git a/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/read.o b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/read.o new file mode 100644 index 0000000..36acf25 Binary files /dev/null and b/P06_Personen_Verwaltung_Linked_List/work/personen-verwaltung/src/read.o differ