You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
2.6 KiB
C
128 lines
2.6 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define MAX_NUM_VOLI 10
|
|
#define LUNG_CODICE 5
|
|
|
|
typedef struct {
|
|
char codice[LUNG_CODICE];
|
|
char *destinazione;
|
|
int partenza_ore;
|
|
int partenza_minuti;
|
|
int posti;
|
|
} volo_t;
|
|
|
|
typedef struct {
|
|
volo_t voli[MAX_NUM_VOLI];
|
|
int num_voli;
|
|
} tabella_voli_t;
|
|
|
|
void stampa_volo(volo_t volo);
|
|
void dealloca_tabella(tabella_voli_t *tabella);
|
|
|
|
void stampa_tabella_voli(tabella_voli_t *tabella);
|
|
int aggiungi_volo(tabella_voli_t *tabella);
|
|
|
|
int main() {
|
|
tabella_voli_t tabella;
|
|
char scelta;
|
|
|
|
tabella.num_voli = 0;
|
|
|
|
while (1) {
|
|
printf(
|
|
"*** Gestione voli ***\n"
|
|
" 1) Stampa tutti i voli presenti\n"
|
|
" 2) Aggiungi un volo\n"
|
|
" Q) Esci dal programma\n"
|
|
"\n"
|
|
"Scelta: "
|
|
);
|
|
scanf(" %c", &scelta);
|
|
printf("\n");
|
|
|
|
switch (scelta) {
|
|
case '1':
|
|
stampa_tabella_voli(&tabella);
|
|
break;
|
|
case '2':
|
|
if (!aggiungi_volo(&tabella)) {
|
|
printf("Impossibile aggiungere il volo.\n");
|
|
};
|
|
break;
|
|
case 'Q':
|
|
case 'q':
|
|
printf("\nArrivederci!\n");
|
|
dealloca_tabella(&tabella);
|
|
return 0;
|
|
}
|
|
|
|
printf("\n");
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void stampa_volo(volo_t volo) {
|
|
printf("Volo %5s:\n", volo.codice);
|
|
printf("- Destinazione: %s\n", volo.destinazione);
|
|
printf("- Partenza: %02d:%02d\n", volo.partenza_ore, volo.partenza_minuti);
|
|
printf("- Posti disponibili: %d\n", volo.posti);
|
|
}
|
|
|
|
void dealloca_tabella(tabella_voli_t *tabella) {
|
|
int i;
|
|
|
|
for (i = 0; i < tabella->num_voli; i++) {
|
|
free(tabella->voli[i].destinazione);
|
|
}
|
|
}
|
|
|
|
void stampa_tabella_voli(tabella_voli_t *tabella) {
|
|
int i;
|
|
|
|
if (tabella->num_voli == 0) {
|
|
printf("Nessun volo presente!\n");
|
|
return;
|
|
}
|
|
|
|
for (i = 0; i < tabella->num_voli; i++) {
|
|
stampa_volo(tabella->voli[i]);
|
|
}
|
|
}
|
|
|
|
int aggiungi_volo(tabella_voli_t *tabella) {
|
|
int indice_volo;
|
|
char buffer[128];
|
|
char *dest_ptr;
|
|
|
|
indice_volo = tabella->num_voli;
|
|
if (indice_volo >= MAX_NUM_VOLI) {
|
|
return 0;
|
|
}
|
|
|
|
printf("Codice: ");
|
|
scanf(" %5c", tabella->voli[indice_volo].codice);
|
|
printf("Destinazione: ");
|
|
scanf("%s", buffer);
|
|
printf("Partenza: ");
|
|
scanf("%d:%d", &tabella->voli[indice_volo].partenza_ore,
|
|
&tabella->voli[indice_volo].partenza_minuti);
|
|
printf("Posti disponibili: ");
|
|
scanf("%d", &tabella->voli[indice_volo].posti);
|
|
|
|
dest_ptr = malloc(strlen(buffer) + 1);
|
|
if (dest_ptr == NULL) {
|
|
printf("Errore nell'allocazione di memoria.\n");
|
|
return 0;
|
|
}
|
|
|
|
strcpy(dest_ptr, buffer);
|
|
tabella->voli[indice_volo].destinazione = dest_ptr;
|
|
|
|
tabella->num_voli++;
|
|
|
|
return 1;
|
|
}
|