feat: Initial commit
This commit is contained in:
3
Laboratorio 4/Esercizio 2/anag1.txt
Normal file
3
Laboratorio 4/Esercizio 2/anag1.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
A0001 Mario Rossi 01/02/1990 ViaMarcoPolo Torino 10129
|
||||
A0002 Lucia Verdi 07/11/1989 ViaTorino Milano 20123
|
||||
A0003 Antonio Neri 19/04/1999 GalleriaTermini Roma 00185
|
||||
2
Laboratorio 4/Esercizio 2/anag2.txt
Normal file
2
Laboratorio 4/Esercizio 2/anag2.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
A0004 Anna Gialli 04/09/1995 FratelliAlinari Firenze 50123
|
||||
A0005 Marco Bianchi 03/07/1993 PiazzaMazzini Napoli 80135
|
||||
192
Laboratorio 4/Esercizio 2/item.c
Normal file
192
Laboratorio 4/Esercizio 2/item.c
Normal file
@@ -0,0 +1,192 @@
|
||||
// Laboratorio 4 - Esercizio 2 - item.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef ITEM_C_DEFINED
|
||||
#define ITEM_C_DEFINED
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include "item.h"
|
||||
|
||||
int DateGreater(Date a, Date b) {
|
||||
if (a.year > b.year)
|
||||
return true;
|
||||
else if (a.year < b.year)
|
||||
return false;
|
||||
|
||||
if (a.month > b.month)
|
||||
return true;
|
||||
else if (a.month < b.month)
|
||||
return false;
|
||||
|
||||
if (a.day >= b.day)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Link newNode(Item val, Link next) {
|
||||
Link t = (Link) malloc(sizeof(Node));
|
||||
if (t == NULL) {
|
||||
printf("Impossibile allocare memoria");
|
||||
exit(2);
|
||||
}
|
||||
t->val = val;
|
||||
t->next = next;
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
void freeNode(Link node) {
|
||||
free(node->val);
|
||||
free(node);
|
||||
}
|
||||
|
||||
Item ItemFromString(char *str) {
|
||||
Item item;
|
||||
|
||||
if ((item = (Item) malloc(sizeof(struct item_s))) == NULL) {
|
||||
printf("Impossibile allocare memoria");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
if (sscanf(str, " %5s %50s %50s %d/%d/%d %50s %50s %d",
|
||||
item->code,
|
||||
item->name,
|
||||
item->surname,
|
||||
&item->date.day,
|
||||
&item->date.month,
|
||||
&item->date.year,
|
||||
item->address,
|
||||
item->city,
|
||||
&item->cap
|
||||
) != 9) {
|
||||
free(item);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
Item ItemRead(FILE * fp) {
|
||||
Item item;
|
||||
|
||||
if ((item = (Item) malloc(sizeof(struct item_s))) == NULL) {
|
||||
printf("Impossibile allocare memoria");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
if (fscanf(fp, " %5s %50s %50s %d/%d/%d %50s %50s %d",
|
||||
item->code,
|
||||
item->name,
|
||||
item->surname,
|
||||
&item->date.day,
|
||||
&item->date.month,
|
||||
&item->date.year,
|
||||
item->address,
|
||||
item->city,
|
||||
&item->cap
|
||||
) != 9) {
|
||||
free(item);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
Key ItemKey(Item val) {
|
||||
return val->code;
|
||||
}
|
||||
|
||||
int ItemEq(Key x, Key y) {
|
||||
return strcmp(x, y) == 0;
|
||||
}
|
||||
|
||||
void ItemPrint(FILE *fp, Item x) {
|
||||
fprintf(fp, "%s %s %s %02d/%02d/%04d %s %s %05d\n",
|
||||
x->code,
|
||||
x->name,
|
||||
x->surname,
|
||||
x->date.day,
|
||||
x->date.month,
|
||||
x->date.year,
|
||||
x->address,
|
||||
x->city,
|
||||
x->cap
|
||||
);
|
||||
}
|
||||
|
||||
Link insertOrderedByDate(Item val, Link head) {
|
||||
Link x, p;
|
||||
|
||||
if (head == NULL || DateGreater(val->date, head->val->date))
|
||||
return newNode(val, head);
|
||||
|
||||
for (p = head, x = head->next; x != NULL && DateGreater(x->val->date, val->date); p = x, x = x->next);
|
||||
p->next = newNode(val, x);
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
Item getItemByCode(Link head, Key key) {
|
||||
for (Link t = head; t != NULL; t = t->next) {
|
||||
if (ItemEq(ItemKey(t->val), key))
|
||||
return t->val;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void printList(FILE *fp, Link head) {
|
||||
for (Link t = head; t != NULL; t = t->next) {
|
||||
ItemPrint(fp, t->val);
|
||||
}
|
||||
}
|
||||
|
||||
Item extractCode(Link *headp, Key key) {
|
||||
Link *t, r;
|
||||
Item element = NULL;
|
||||
|
||||
for (t = headp; *t != NULL; t = &((*t)->next)) {
|
||||
if (ItemEq(ItemKey((*t)->val), key)) {
|
||||
element = (*t)->val;
|
||||
// Salvo `r` per poi chiamare free
|
||||
r = *t;
|
||||
// rimuovo il nodo dalla lista
|
||||
*t = (*t)->next;
|
||||
// Faccio free solo del nodo (non dell'item)
|
||||
free(r);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
Item extractDates(Link *headp, Date first, Date last) {
|
||||
Link *t, r;
|
||||
Item element = NULL;
|
||||
|
||||
for (t = headp; *t != NULL; t = &((*t)->next)) {
|
||||
if (DateGreater((*t)->val->date, first) && DateGreater(last, (*t)->val->date)) {
|
||||
element = (*t)->val;
|
||||
r = *t;
|
||||
*t = (*t)->next;
|
||||
free(r);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
void freeList(Link head) {
|
||||
Link p, x;
|
||||
for (p = head, x = head->next; x != NULL; p = x, x = x->next){
|
||||
freeNode(p);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
55
Laboratorio 4/Esercizio 2/item.h
Normal file
55
Laboratorio 4/Esercizio 2/item.h
Normal file
@@ -0,0 +1,55 @@
|
||||
// Laboratorio 4 - Esercizio 2 - item.h
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef ITEM_H_DEFINED
|
||||
#define ITEM_H_DEFINED
|
||||
|
||||
#define CODE_LEN 5
|
||||
#define STR_LEN 50
|
||||
|
||||
typedef struct date_s {
|
||||
int day;
|
||||
int month;
|
||||
int year;
|
||||
} Date;
|
||||
|
||||
typedef struct item_s {
|
||||
char code[CODE_LEN+1];
|
||||
char name[STR_LEN+1];
|
||||
char surname[STR_LEN+1];
|
||||
Date date;
|
||||
char address[STR_LEN+1];
|
||||
char city[STR_LEN+1];
|
||||
int cap;
|
||||
} *Item;
|
||||
|
||||
typedef struct node_s *Link;
|
||||
typedef struct node_s {
|
||||
Item val;
|
||||
Link next;
|
||||
} Node;
|
||||
|
||||
typedef char* Key;
|
||||
|
||||
// Date
|
||||
|
||||
int DateGreater(Date a, Date b);
|
||||
|
||||
// Item
|
||||
|
||||
Item ItemFromString(char *str);
|
||||
Item ItemRead(FILE * fp);
|
||||
Key ItemKey(Item item);
|
||||
int ItemEq(Key x, Key y);
|
||||
void ItemPrint(FILE *fp, Item x);
|
||||
|
||||
// List
|
||||
|
||||
Link insertOrderedByDate(Item val, Link head);
|
||||
Item getItemByCode(Link head, Key key);
|
||||
void printList(FILE *fp, Link head);
|
||||
Item extractCode(Link *headp, Key key);
|
||||
Item extractDates(Link *headp, Date first, Date last);
|
||||
void freeList(Link head);
|
||||
|
||||
#endif
|
||||
220
Laboratorio 4/Esercizio 2/main.c
Normal file
220
Laboratorio 4/Esercizio 2/main.c
Normal file
@@ -0,0 +1,220 @@
|
||||
// Laboratorio 4 - Esercizio 2 - main.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include "item.h"
|
||||
#define MAX_LEN 30
|
||||
|
||||
const int MAXL = 100;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
k_codice_tratta,
|
||||
k_partenza,
|
||||
k_destinazione,
|
||||
k_data
|
||||
} corsa_keys;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
r_leggi_file,
|
||||
r_leggi_tastiera,
|
||||
r_ricerca_codice,
|
||||
r_cancella_codice,
|
||||
r_cancella_date,
|
||||
r_stampa,
|
||||
r_fine
|
||||
} t_comandi;
|
||||
|
||||
// Trasforma in lowercase tutti i caratteri di una stringa
|
||||
char *toLower(char *s)
|
||||
{
|
||||
for (char *p = s; *p; p++)
|
||||
*p = tolower(*p);
|
||||
return s;
|
||||
}
|
||||
|
||||
t_comandi leggiComando()
|
||||
{
|
||||
t_comandi c;
|
||||
char cmd[MAXL];
|
||||
char tabella[7][20] = {
|
||||
"leggi_file", "leggi_tastiera", "ricerca_codice", "cancella_codice", "cancella_date", "stampa", "fine"};
|
||||
printf("comando (leggi_file/leggi_tastiera/ricerca_codice");
|
||||
printf("/cancella_codice/cancella_date/stampa/fine): ");
|
||||
scanf("%s", cmd);
|
||||
toLower(cmd);
|
||||
c = r_leggi_file;
|
||||
while (c < 8 && strcmp(cmd, tabella[c]) != 0)
|
||||
c++;
|
||||
return (c);
|
||||
}
|
||||
|
||||
void carica_file(char *argomenti, Link * head)
|
||||
{
|
||||
char filename[MAX_LEN+1];
|
||||
FILE *fp_read;
|
||||
if (sscanf(argomenti, " %30s", filename) != 1)
|
||||
{
|
||||
puts("Sintassi non valida. La sintassi è 'carica <nome file>'");
|
||||
return;
|
||||
}
|
||||
|
||||
puts("Carico il nuovo file...");
|
||||
|
||||
if ((fp_read = fopen(filename, "r")) == NULL)
|
||||
{
|
||||
puts("Impossibile aprire il file");
|
||||
return;
|
||||
}
|
||||
|
||||
while(!feof(fp_read)) {
|
||||
Item nuovo = ItemRead(fp_read);
|
||||
// skip invalid entries
|
||||
if (nuovo != NULL)
|
||||
*head = insertOrderedByDate(nuovo, *head);
|
||||
}
|
||||
|
||||
fclose(fp_read);
|
||||
}
|
||||
|
||||
void carica_tastiera(char *argomenti, Link * head)
|
||||
{
|
||||
Item new = ItemFromString(argomenti);
|
||||
|
||||
if (new == NULL) {
|
||||
puts("Anagrafica non valida");
|
||||
return;
|
||||
}
|
||||
|
||||
*head = insertOrderedByDate(new, *head);
|
||||
}
|
||||
|
||||
void ricerca_codice(char *argomenti, Link * head)
|
||||
{
|
||||
char codice[CODE_LEN+1];
|
||||
if (sscanf(argomenti, " %5s", codice) != 1)
|
||||
{
|
||||
puts("Sintassi non valida. La sintassi è 'ricerca_codice <codice>'");
|
||||
return;
|
||||
}
|
||||
|
||||
Item found = getItemByCode(*head, codice);
|
||||
|
||||
if (found == NULL) {
|
||||
puts("Non è stato trovato nessun elemento");
|
||||
return;
|
||||
}
|
||||
|
||||
ItemPrint(stdout, found);
|
||||
}
|
||||
|
||||
void cancella_codice(char *argomenti, Link * head)
|
||||
{
|
||||
char codice[CODE_LEN+1];
|
||||
if (sscanf(argomenti, " %5s", codice) != 1)
|
||||
{
|
||||
puts("Sintassi non valida. La sintassi è 'cancella_codice <codice>'");
|
||||
return;
|
||||
}
|
||||
|
||||
Item found = extractCode(head, codice);
|
||||
|
||||
if (found == NULL) {
|
||||
puts("Non è stato trovato nessun elemento");
|
||||
return;
|
||||
}
|
||||
|
||||
ItemPrint(stdout, found);
|
||||
free(found);
|
||||
}
|
||||
|
||||
void cancella_date(char *argomenti, Link * head)
|
||||
{
|
||||
Date start, end;
|
||||
if (sscanf(argomenti, " %d/%d/%d %d/%d/%d", &(start.day), &(start.month), &(start.year), &(end.day), &(end.month), &(end.year)) != 6)
|
||||
{
|
||||
puts("Sintassi non valida. La sintassi è 'cancella_date <data inizio> <data fine>'");
|
||||
return;
|
||||
}
|
||||
|
||||
Item found;
|
||||
|
||||
while (found = extractDates(head, start, end), found != NULL) {
|
||||
ItemPrint(stdout, found);
|
||||
free(found);
|
||||
}
|
||||
}
|
||||
|
||||
void stampa_file(char *argomenti, Link * head)
|
||||
{
|
||||
FILE *fp_write;
|
||||
char filename[MAX_LEN+1];
|
||||
if (sscanf(argomenti, " %30s", filename) != 1)
|
||||
{
|
||||
puts("Sintassi non valida. La sintassi è 'stampa <nome file>'");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if ((fp_write = fopen(filename, "w")) == NULL)
|
||||
{
|
||||
puts("Impossibile aprire il file");
|
||||
return;
|
||||
}
|
||||
|
||||
printList(fp_write, *head);
|
||||
fclose(fp_write);
|
||||
}
|
||||
|
||||
void menuParola(Link * head)
|
||||
{
|
||||
t_comandi comando;
|
||||
char argomenti[MAXL];
|
||||
int i, continua = 1;
|
||||
while (continua)
|
||||
{
|
||||
comando = leggiComando();
|
||||
fgets(argomenti, MAXL, stdin); /* resto della riga */
|
||||
switch (comando)
|
||||
{
|
||||
case r_leggi_file:
|
||||
carica_file(argomenti, head);
|
||||
break;
|
||||
case r_leggi_tastiera:
|
||||
carica_tastiera(argomenti, head);
|
||||
break;
|
||||
case r_ricerca_codice:
|
||||
ricerca_codice(argomenti, head);
|
||||
break;
|
||||
case r_cancella_codice:
|
||||
cancella_codice(argomenti, head);
|
||||
break;
|
||||
case r_cancella_date:
|
||||
cancella_date(argomenti, head);
|
||||
break;
|
||||
case r_stampa:
|
||||
stampa_file(argomenti, head);
|
||||
break;
|
||||
case r_fine:
|
||||
continua = 0;
|
||||
break;
|
||||
default:
|
||||
puts("Comando non valido\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Link head = NULL;
|
||||
|
||||
menuParola(&head);
|
||||
freeList(head);
|
||||
|
||||
return 0;
|
||||
}
|
||||
2
Laboratorio 4/Esercizio 2/out
Normal file
2
Laboratorio 4/Esercizio 2/out
Normal file
@@ -0,0 +1,2 @@
|
||||
A0004 Anna Gialli 04/09/1995 FratelliAlinari Firenze 50123
|
||||
A0002 Lucia Verdi 07/11/1989 ViaTorino Milano 20123
|
||||
Reference in New Issue
Block a user