Praktikum 6 begonnen
This commit is contained in:
parent
ed8ce218b1
commit
ae13c0695a
|
@ -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)
|
||||
|
|
Binary file not shown.
|
@ -0,0 +1,44 @@
|
|||
/* ----------------------------------------------------------------------------
|
||||
* -- _____ ______ _____ -
|
||||
* -- |_ _| | ____|/ ____| -
|
||||
* -- | | _ __ | |__ | (___ Institute of Embedded Systems -
|
||||
* -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur -
|
||||
* -- _| |_| | | | |____ ____) | (University of Applied Sciences) -
|
||||
* -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland -
|
||||
* ----------------------------------------------------------------------------
|
||||
*/
|
||||
/**
|
||||
* @file
|
||||
* @brief Lab implementation
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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;
|
||||
}
|
|
@ -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
|
|
@ -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_
|
Binary file not shown.
|
@ -13,6 +13,14 @@
|
|||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
|
|
|
@ -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
|
Binary file not shown.
|
@ -0,0 +1,86 @@
|
|||
/* ----------------------------------------------------------------------------
|
||||
* -- _____ ______ _____ -
|
||||
* -- |_ _| | ____|/ ____| -
|
||||
* -- | | _ __ | |__ | (___ Institute of Embedded Systems -
|
||||
* -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur -
|
||||
* -- _| |_| | | | |____ ____) | (University of Applied Sciences) -
|
||||
* -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland -
|
||||
* ----------------------------------------------------------------------------
|
||||
*/
|
||||
/**
|
||||
* @file
|
||||
* @brief Lab implementation
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#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;
|
||||
|
||||
}
|
|
@ -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
|
|
@ -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_
|
Binary file not shown.
|
@ -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 <stdio.h>
|
||||
#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
|
|
@ -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
|
|
@ -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
|
Binary file not shown.
Loading…
Reference in New Issue