Upload files to 'P06_Lösung_Andrin'

This commit is contained in:
Andrin Fassbind 2022-04-07 14:24:54 +02:00
parent e54dc6a4d5
commit 6c94df5e4d
4 changed files with 167 additions and 0 deletions

17
P06_Lösung_Andrin/list.h Normal file
View File

@ -0,0 +1,17 @@
#include "person.h"
#include <stdlib.h>
#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

119
P06_Lösung_Andrin/main.c Normal file
View File

@ -0,0 +1,119 @@
/* ----------------------------------------------------------------------------
* -- _____ ______ _____ -
* -- |_ _| | ____|/ ____| -
* -- | | _ __ | |__ | (___ 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 "person.h"
#include "list.h"
#include <string.h>
#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;
}

View File

@ -0,0 +1,17 @@
#include "person.h"
#include <string.h>
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;
}

View File

@ -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