Skip to content
Snippets Groups Projects
Commit 00a61623 authored by 20041679's avatar 20041679
Browse files

added part of todays exercises

parent aafe904a
No related branches found
No related tags found
No related merge requests found
Showing
with 404 additions and 0 deletions
a.out
.nfs*
*.o
main
CC = cc
CLFAGS = -g -Wall -Wextra
objects = list.o person.o input.o main.o
%.o : %.c
$(CC) -c $(CFLAGS) $< -o $@
all: $(objects)
$(CC) -o main $(objects)
clean:
rm main $(objects)
#include "input.h"
#include <stdio.h>
#include <string.h>
char *fgets_strip(char *s, int size, FILE *stream) {
char *ret = fgets(s, size, stream);
s[strcspn(s, "\n")] = 0;
return ret;
}
#ifndef INPUT_H
#define INPUT_H
#include <stdio.h>
char *fgets_strip(char *s, int size, FILE *stream);
#endif
Mario Rossi 10
Marco Bianchi 20
Mirko Verdi 30
END
#include "list.h"
#include <stdlib.h>
Link list_new_raw(void) {
return malloc(sizeof(ListHead));
}
Link list_new(Data data) {
Link ret = list_new_raw();
ret->data = data;
ret->next = NULL;
return ret;
}
Link list_input(FILE *f, int (*input) (FILE *, Data *)) {
Data data;
if (input(f, &data)) {
Link head = list_new(data);
head->next = list_input(f, input);
return head;
}
return NULL;
}
void list_print(Link head, void (*print) (Data *)) {
if (head) {
print(&head->data);
list_print(head->next, print);
}
}
#ifndef LIST_H
#define LIST_H
#include "person.h"
#include <stdio.h>
typedef Person Data;
typedef struct ListHead {
struct ListHead *next;
Data data;
} ListHead;
typedef ListHead *Link;
Link list_new_(void);
Link list_input(FILE *f, int (*input) (FILE *, Data *));
void list_print(Link head, void (*print) (Data *));
#endif
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include "person.h"
int main(int argc, char *argv[]) {
if (argc != 2) {
return EXIT_FAILURE;
}
FILE *f;
if (!(f = fopen(argv[1], "r"))) {
return EXIT_FAILURE;
}
Link head = list_input(f, person_read);
fclose(f);
list_print(head, person_print);
}
#include "person.h"
#include "util.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
int person_parse(char *s, Person *person) {
char format[4 + 1 + 4 + 1 + 2 + 1];
assert((size_t)snprintf(
format,
ARR_LEN(format),
"%%%zus %%%zus %%u",
ARR_LEN(person->first_name),
ARR_LEN(person->last_name)
) < ARR_LEN(format));
return sscanf(
s,
format,
person->first_name,
person->last_name,
&person->age
) < 3 ? -1 : 0;
}
int person_read(FILE *f, Person *person) {
char buf[256];
fgets_strip(buf, ARR_LEN(buf), f);
if (strncmp(buf, FILE_END, ARR_LEN(FILE_END)) == 0) {
return 0;
}
return person_parse(buf, person) < 0 ? 0 : 1;
}
void person_print(Person *person) {
printf(
"nome: %s, cognome: %s, eta': %u\n",
person->first_name,
person->last_name,
person->age
);
}
#ifndef PERSON_H
#define PERSON_H
#include "input.h"
#include <stdio.h>
#define FILE_END "END"
typedef struct {
char first_name[64];
char last_name[64];
unsigned int age;
} Person;
int person_parse(char *s, Person *person);
int person_read(FILE *f, Person *person);
void person_print(Person *person);
#endif
#ifndef UTIL_H
#define UTIL_H
#define ARR_LEN(arr) (sizeof(arr) / sizeof(*arr))
#endif
CC = cc
CLFAGS = -g -Wall -Wextra
objects = list.o person.o input.o main.o
%.o : %.c
$(CC) -c $(CFLAGS) $< -o $@
all: $(objects)
$(CC) -o main $(objects)
run: all
./main input.txt
clean:
rm main $(objects)
#include "input.h"
#include <stdio.h>
#include <string.h>
char *fgets_strip(char *s, int size, FILE *stream) {
char *ret = fgets(s, size, stream);
s[strcspn(s, "\n")] = 0;
return ret;
}
void flush_stdin(void) {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
#ifndef INPUT_H
#define INPUT_H
#include <stdio.h>
char *fgets_strip(char *s, int size, FILE *stream);
void flush_stdin(void);
#endif
Mario Rossi 10
Marco Bianchi 20
Mirko Verdi 30
END
#include "list.h"
#include <stdlib.h>
Link list_new_raw(void) {
return malloc(sizeof(ListHead));
}
Link list_new(Data data) {
Link ret = list_new_raw();
ret->data = data;
ret->next = NULL;
return ret;
}
Link list_input(FILE *f, int (*input) (FILE *, Data *)) {
Data data;
if (input(f, &data)) {
Link head = list_new(data);
head->next = list_input(f, input);
return head;
}
return NULL;
}
void list_print(Link head, void (*print) (Data *)) {
if (head) {
print(&head->data);
list_print(head->next, print);
}
}
Link list_nth(Link head, size_t n) {
if (head) {
return n > 0 ? list_nth(head->next, n - 1) : head;
}
return NULL;
}
Link list_find(Link head, int (*compare) (Data *, Data *), Data *key) {
if (head) {
if (compare(&head->data, key)) {
return head;
}
return list_find(head->next, compare, key);
}
return NULL;
}
#ifndef LIST_H
#define LIST_H
#include "person.h"
#include <stdio.h>
typedef Person Data;
typedef struct ListHead {
struct ListHead *next;
Data data;
} ListHead;
typedef ListHead *Link;
Link list_new_(void);
Link list_input(FILE *f, int (*input) (FILE *, Data *));
void list_print(Link head, void (*print) (Data *));
Link list_find(Link head, int (*compare) (Data *, Data *), Data *key);
Link list_nth(Link head, size_t n);
#endif
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include "person.h"
#include "input.h"
#include "util.h"
#define MESSAGE_NOT_FOUND "persona non trovata"
int main(int argc, char *argv[]) {
if (argc != 2) {
return EXIT_FAILURE;
}
FILE *f;
if (!(f = fopen(argv[1], "r"))) {
return EXIT_FAILURE;
}
Link list = list_input(f, person_read);
fclose(f);
puts("ricerca per [c]ognome o [p]osizione?");
int choice = getchar();
flush_stdin();
Link found;
switch (choice) {
case 'c': {
puts("inserire cognome");
Person key;
fgets_strip(key.last_name, ARR_LEN(key.last_name), stdin);
found = list_find(list, person_is_related, &key);
break;
}
case 'p': {
puts("inserire posizione");
size_t n;
scanf("%zu", &n);
found = list_nth(list, n);
break;
}
default:
return EXIT_FAILURE;
break;
}
if (found) {
person_print(&found->data);
} else {
fputs(MESSAGE_NOT_FOUND "\n", stderr);
}
}
#include "person.h"
#include "util.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
int person_parse(char *s, Person *person) {
char format[4 + 1 + 4 + 1 + 2 + 1];
assert((size_t)snprintf(
format,
ARR_LEN(format),
"%%%zus %%%zus %%u",
ARR_LEN(person->first_name),
ARR_LEN(person->last_name)
) < ARR_LEN(format));
return sscanf(
s,
format,
person->first_name,
person->last_name,
&person->age
) < 3 ? -1 : 0;
}
int person_read(FILE *f, Person *person) {
char buf[256];
fgets_strip(buf, ARR_LEN(buf), f);
if (strncmp(buf, FILE_END, ARR_LEN(FILE_END)) == 0) {
return 0;
}
return person_parse(buf, person) < 0 ? 0 : 1;
}
void person_print(Person *person) {
printf(
"nome: %s, cognome: %s, eta': %u\n",
person->first_name,
person->last_name,
person->age
);
}
int person_is_related(Person *p1, Person *p2) {
return strcmp(p1->last_name, p2->last_name) == 0;
}
#ifndef PERSON_H
#define PERSON_H
#include "input.h"
#include <stdio.h>
#define FILE_END "END"
typedef struct {
char first_name[64];
char last_name[64];
unsigned int age;
} Person;
int person_parse(char *s, Person *person);
int person_read(FILE *f, Person *person);
void person_print(Person *person);
int person_is_related(Person *p1, Person *p2);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment