Corso di Informatica Medica
Esercitazione IX
!
Alessandro A. Nacci
[email protected] - alessandronacci.com
1
LA MAPPA DEL TESORO
2
Una mappa particolare!
•
Immaginiamo di avere una mappa del tesoro… un po particolare!!
•
E’ una sorta di mappa termica radiale della“vicinanza” al tesoro
3
La strategia del pirata
•
Un pirata chevuole muoversi su questa mappa per cercare il tesoro, si sposterà
sempre dalle
celle con colori più freddi verso celle con colori più caldi
4
Traccia dell’esercizio
•
Scrivere un programma C che sia in grado di•
gestire la mappa appena introdotta•
date le coordinate del tesoro, creare la mappa termica di vicinanza•
date delle coordinate di partenza,tracciare un percorso valido per arrivare al tesoro
5
Gestione della mappa termica
•
Come rappresentiamo la mappa termica?6
Gestione della mappa termica
•
Come rappresentiamo la mappa termica?6
•
Usiamo una matrice•
I colori diventano numeriGestione della mappa termica
•
Come rappresentiamo la mappa termica?6
•
Usiamo una matrice•
I colori diventano numeriInclude, define e dichiarazione variabili
7
#include <stdio.h>
#include <math.h>
#define W 10
#define H 10
#define VALORE_TESORO 15
void stampa_mappa(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
printf("%d\t", mappa[x][y]);
}
printf("\n");
}
printf("\n\n\n");
}
void stampa_percorso(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
if (mappa[x][y] == -1) printf("#\t");
else printf("-\t");
}
printf("\n");
}
printf("\n\n\n");
}
void init_mappa(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
mappa[x][y] = 0;
} }
}
void metti_tesoro(int mappa[W][H], int tes_x, int tes_y) {
int radius;
int x,y;
int start_x, start_y;
int val_tesoro = VALORE_TESORO;
mappa[tes_x][tes_y] = val_tesoro;
for (radius = 0; radius < W; radius++) {
start_x = tes_x - radius;
start_y = tes_y - radius;
end_x = tes_x + radius;
end_y = tes_y + radius;
if (start_x >= 0 && start_y >= 0 && end_x < W && end_y < H ) for (x = start_x; x <= end_x; x++)
for (y = start_y; y <= end_y; y++)
if ( ((x == start_x) || (x == end_x)) || ((y == start_y) || (y == end_y))) mappa[x][y] = val_tesoro - radius;
} }
int cerca_tesoro(int mappa[W][H], int start_x, int start_y) {
if (mappa[start_x][start_y] == VALORE_TESORO) return 1;
int x,y;
for (x = start_x - 1; x <= start_x + 1; x++)
for (y = start_y - 1; y <= start_y + 1; y++) if (x>0 && y >= 0 && x<W && y<H)
if (mappa[x][y] > mappa[start_x][start_y]) {
mappa[start_x][start_y] = -1;
return cerca_tesoro(mappa, x, y);
} return 0;
}
int main() {
int mappa[W][H];
int x,y;
int trovato = 0;
init_mappa(mappa);
stampa_mappa(mappa);
metti_tesoro(mappa, 5,5);
stampa_mappa(mappa);
trovato = cerca_tesoro(mappa, 0,2);
if (trovato) printf("Ho trovato il tesoro!\n");
stampa_mappa(mappa);
stampa_percorso(mappa);
}
VALORE_TESORO è legato alla dimensione della mappa
Inizializziamo la mappa
8
#include <stdio.h>
#include <math.h>
#define W 10
#define H 10
#define VALORE_TESORO 15
void stampa_mappa(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
printf("%d\t", mappa[x][y]);
}
printf("\n");
}
printf("\n\n\n");
}
void stampa_percorso(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
if (mappa[x][y] == -1) printf("#\t");
else printf("-\t");
}
printf("\n");
}
printf("\n\n\n");
}
void init_mappa(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
mappa[x][y] = 0;
} }
}
void metti_tesoro(int mappa[W][H], int tes_x, int tes_y) {
int radius;
int x,y;
int start_x, start_y;
int end_x, end_y;
Scrivere una funziona che riempia di ‘0’ una matrice ‘mappa’ in ingresso.
Inizializziamo la mappa
8
#include <stdio.h>
#include <math.h>
#define W 10
#define H 10
#define VALORE_TESORO 15
void stampa_mappa(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
printf("%d\t", mappa[x][y]);
}
printf("\n");
}
printf("\n\n\n");
}
void stampa_percorso(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
if (mappa[x][y] == -1) printf("#\t");
else printf("-\t");
}
printf("\n");
}
printf("\n\n\n");
}
void init_mappa(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
mappa[x][y] = 0;
} }
}
void metti_tesoro(int mappa[W][H], int tes_x, int tes_y) {
int radius;
int x,y;
int start_x, start_y;
int end_x, end_y;
Stampiamo la mappa
9
#include <stdio.h>
#include <math.h>
#define W 10
#define H 10
#define VALORE_TESORO 15
void stampa_mappa(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
printf("%d\t", mappa[x][y]);
}
printf("\n");
}
printf("\n\n\n");
}
void stampa_percorso(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
if (mappa[x][y] == -1) printf("#\t");
else printf("-\t");
}
printf("\n");
}
printf("\n\n\n");
}
void init_mappa(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
mappa[x][y] = 0;
} }
}
void metti_tesoro(int mappa[W][H], int tes_x, int tes_y) {
int radius;
int x,y;
int start_x, start_y;
int end_x, end_y;
Scrivere una funziona che visualizzi a schermo una matrice ‘mappa’ in
ingresso
Stampiamo la mappa
9
#include <stdio.h>
#include <math.h>
#define W 10
#define H 10
#define VALORE_TESORO 15
void stampa_mappa(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
printf("%d\t", mappa[x][y]);
}
printf("\n");
}
printf("\n\n\n");
}
void stampa_percorso(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
if (mappa[x][y] == -1) printf("#\t");
else printf("-\t");
}
printf("\n");
}
printf("\n\n\n");
}
void init_mappa(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
mappa[x][y] = 0;
} }
}
void metti_tesoro(int mappa[W][H], int tes_x, int tes_y) {
int radius;
int x,y;
int start_x, start_y;
int end_x, end_y;
Posiziona il tesoro sulla mappa…
10
void metti_tesoro(int mappa[W][H], int tes_x, int tes_y) {
int radius;
int x,y;
int start_x, start_y;
int end_x, end_y;
int val_tesoro = VALORE_TESORO;
mappa[tes_x][tes_y] = val_tesoro;
for (radius = 0; radius < W; radius++) {
start_x = tes_x - radius;
start_y = tes_y - radius;
end_x = tes_x + radius;
end_y = tes_y + radius;
if (start_x >= 0 && start_y >= 0 && end_x < W && end_y < H ) for (x = start_x; x <= end_x; x++)
for (y = start_y; y <= end_y; y++)
if ( ((x == start_x) || (x == end_x)) || ((y == start_y) || (y == end_y))) mappa[x][y] = val_tesoro - radius;
} }
int cerca_tesoro(int mappa[W][H], int start_x, int start_y) {
if (mappa[start_x][start_y] == VALORE_TESORO) return 1;
int x,y;
for (x = start_x - 1; x <= start_x + 1; x++)
for (y = start_y - 1; y <= start_y + 1; y++) if (x>0 && y >= 0 && x<W && y<H)
if (mappa[x][y] > mappa[start_x][start_y]) {
mappa[start_x][start_y] = -1;
return cerca_tesoro(mappa, x, y);
} return 0;
}
int main() {
int mappa[W][H];
int x,y;
int trovato = 0;
init_mappa(mappa);
stampa_mappa(mappa);
metti_tesoro(mappa, 5,5);
stampa_mappa(mappa);
trovato = cerca_tesoro(mappa, 0,2);
if (trovato) printf("Ho trovato il tesoro!\n");
Così sbaglio a gestire i bordi!
radius dovrebbe partire da 1 perchè…
il valore massimo di radius non dovrebbe essere ‘w’ perché…
Scrivere una funziona che, date due coordinate tes_x e tes_y - che sono le coordinate del tesoro - crei la mappa termica
radiale presentata in precedenza all’interno di una matrice
‘mappa’ passata in ingresso.
Posiziona il tesoro sulla mappa…
10
void metti_tesoro(int mappa[W][H], int tes_x, int tes_y) {
int radius;
int x,y;
int start_x, start_y;
int end_x, end_y;
int val_tesoro = VALORE_TESORO;
mappa[tes_x][tes_y] = val_tesoro;
for (radius = 0; radius < W; radius++) {
start_x = tes_x - radius;
start_y = tes_y - radius;
end_x = tes_x + radius;
end_y = tes_y + radius;
if (start_x >= 0 && start_y >= 0 && end_x < W && end_y < H ) for (x = start_x; x <= end_x; x++)
for (y = start_y; y <= end_y; y++)
if ( ((x == start_x) || (x == end_x)) || ((y == start_y) || (y == end_y))) mappa[x][y] = val_tesoro - radius;
} }
int cerca_tesoro(int mappa[W][H], int start_x, int start_y) {
if (mappa[start_x][start_y] == VALORE_TESORO) return 1;
int x,y;
for (x = start_x - 1; x <= start_x + 1; x++)
for (y = start_y - 1; y <= start_y + 1; y++) if (x>0 && y >= 0 && x<W && y<H)
if (mappa[x][y] > mappa[start_x][start_y]) {
mappa[start_x][start_y] = -1;
return cerca_tesoro(mappa, x, y);
} return 0;
}
int main() {
int mappa[W][H];
int x,y;
int trovato = 0;
init_mappa(mappa);
stampa_mappa(mappa);
metti_tesoro(mappa, 5,5);
stampa_mappa(mappa);
trovato = cerca_tesoro(mappa, 0,2);
if (trovato) printf("Ho trovato il tesoro!\n");
Così sbaglio a gestire i bordi!
radius dovrebbe partire da 1 perchè…
il valore massimo di radius non dovrebbe essere ‘w’ perché…
Metti tesoro - corretto
11
Cerchiamo il tesoro e tracciamo il percorso…
12
void metti_tesoro(int mappa[W][H], int tes_x, int tes_y) {
int radius;
int x,y;
int start_x, start_y;
int end_x, end_y;
int val_tesoro = VALORE_TESORO;
mappa[tes_x][tes_y] = val_tesoro;
for (radius = 0; radius < W; radius++) {
start_x = tes_x - radius;
start_y = tes_y - radius;
end_x = tes_x + radius;
end_y = tes_y + radius;
if (start_x >= 0 && start_y >= 0 && end_x < W && end_y < H ) for (x = start_x; x <= end_x; x++)
for (y = start_y; y <= end_y; y++)
if ( ((x == start_x) || (x == end_x)) || ((y == start_y) || (y == end_y))) mappa[x][y] = val_tesoro - radius;
} }
int cerca_tesoro(int mappa[W][H], int start_x, int start_y) {
if (mappa[start_x][start_y] == VALORE_TESORO) return 1;
int x,y;
for (x = start_x - 1; x <= start_x + 1; x++)
for (y = start_y - 1; y <= start_y + 1; y++) if (x>0 && y >= 0 && x<W && y<H)
if (mappa[x][y] > mappa[start_x][start_y]) {
mappa[start_x][start_y] = -1;
return cerca_tesoro(mappa, x, y);
} return 0;
}
int main() {
int mappa[W][H];
int x,y;
int trovato = 0;
init_mappa(mappa);
stampa_mappa(mappa);
metti_tesoro(mappa, 5,5);
stampa_mappa(mappa);
trovato = cerca_tesoro(mappa, 0,2);
if (trovato) printf("Ho trovato il tesoro!\n");
Scrivere una funzione C che date due coordinate da cui il pirata parte (‘start_x’
e ‘start_y’), cerchi un percorso corretto per arrivare al tesoro.
!
Per creare un percorso corretto, dato un punto generico in cui il pirata si trova, il pirata può spostarsi solo in una cella adiacente che abbia un valore superiore a
quello della cella corrente.
!
Una volta che una cella viene visitata, è necessario ‘marcarla’ per indicare che quella cella è parte del percorso scelto.
Cerchiamo il tesoro e tracciamo il percorso…
12
void metti_tesoro(int mappa[W][H], int tes_x, int tes_y) {
int radius;
int x,y;
int start_x, start_y;
int end_x, end_y;
int val_tesoro = VALORE_TESORO;
mappa[tes_x][tes_y] = val_tesoro;
for (radius = 0; radius < W; radius++) {
start_x = tes_x - radius;
start_y = tes_y - radius;
end_x = tes_x + radius;
end_y = tes_y + radius;
if (start_x >= 0 && start_y >= 0 && end_x < W && end_y < H ) for (x = start_x; x <= end_x; x++)
for (y = start_y; y <= end_y; y++)
if ( ((x == start_x) || (x == end_x)) || ((y == start_y) || (y == end_y))) mappa[x][y] = val_tesoro - radius;
} }
int cerca_tesoro(int mappa[W][H], int start_x, int start_y) {
if (mappa[start_x][start_y] == VALORE_TESORO) return 1;
int x,y;
for (x = start_x - 1; x <= start_x + 1; x++)
for (y = start_y - 1; y <= start_y + 1; y++) if (x>0 && y >= 0 && x<W && y<H)
if (mappa[x][y] > mappa[start_x][start_y]) {
mappa[start_x][start_y] = -1;
return cerca_tesoro(mappa, x, y);
} return 0;
}
int main() {
int mappa[W][H];
int x,y;
int trovato = 0;
init_mappa(mappa);
stampa_mappa(mappa);
metti_tesoro(mappa, 5,5);
stampa_mappa(mappa);
trovato = cerca_tesoro(mappa, 0,2);
if (trovato) printf("Ho trovato il tesoro!\n");
Mostriamo a schermo il percorso…
13
#include <stdio.h>
#include <math.h>
#define W 10
#define H 10
#define VALORE_TESORO 15
void stampa_mappa(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
printf("%d\t", mappa[x][y]);
}
printf("\n");
}
printf("\n\n\n");
}
void stampa_percorso(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
if (mappa[x][y] == -1) printf("#\t");
else printf("-\t");
}
printf("\n");
}
printf("\n\n\n");
}
void init_mappa(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
mappa[x][y] = 0;
} }
}
Scrivere una cella che visualizzi in modo chiaro quale è il percorso scelto dal pirata per raggiungere il tesoro.
Mostriamo a schermo il percorso…
13
#include <stdio.h>
#include <math.h>
#define W 10
#define H 10
#define VALORE_TESORO 15
void stampa_mappa(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
printf("%d\t", mappa[x][y]);
}
printf("\n");
}
printf("\n\n\n");
}
void stampa_percorso(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
if (mappa[x][y] == -1) printf("#\t");
else printf("-\t");
}
printf("\n");
}
printf("\n\n\n");
}
void init_mappa(int mappa[W][H]) {
int x,y;
for (x = 0; x < W; x++) {
for (y = 0; y < H; y++) {
mappa[x][y] = 0;
} }
}
E facciamo il main :)
14 int val_tesoro = VALORE_TESORO;
mappa[tes_x][tes_y] = val_tesoro;
for (radius = 0; radius < W; radius++) {
start_x = tes_x - radius;
start_y = tes_y - radius;
end_x = tes_x + radius;
end_y = tes_y + radius;
if (start_x >= 0 && start_y >= 0 && end_x < W && end_y < H ) for (x = start_x; x <= end_x; x++)
for (y = start_y; y <= end_y; y++)
if ( ((x == start_x) || (x == end_x)) || ((y == start_y) || (y == end_y))) mappa[x][y] = val_tesoro - radius;
} }
int cerca_tesoro(int mappa[W][H], int start_x, int start_y) {
if (mappa[start_x][start_y] == VALORE_TESORO) return 1;
int x,y;
for (x = start_x - 1; x <= start_x + 1; x++)
for (y = start_y - 1; y <= start_y + 1; y++) if (x>0 && y >= 0 && x<W && y<H)
if (mappa[x][y] > mappa[start_x][start_y]) {
mappa[start_x][start_y] = -1;
return cerca_tesoro(mappa, x, y);
} return 0;
}
int main() {
int mappa[W][H];
int x,y;
int trovato = 0;
init_mappa(mappa);
stampa_mappa(mappa);
metti_tesoro(mappa, 5,5);
stampa_mappa(mappa);
trovato = cerca_tesoro(mappa, 0,2);
if (trovato) printf("Ho trovato il tesoro!\n");
stampa_mappa(mappa);
stampa_percorso(mappa);
}
GESTIONE
AUTOMOBILI
15
Automobili: terza versione
• Rappresentare in C una automobile. Nel nostro caso, una automobile è descritta da un nome, un costo, un colore, da un insieme di componenti e da un libretto di circolazione.
• Un componente ha un nome, un costo ed una categoria. Le categorie possibili sono TRAZIONE, MULTIMEDIA, SICUREZZA
• Il libretto di circolazione riporta invece l’anno e la provincia di immatricolazione e in che classe Euro rientra.
!
• Il programma deve poter permettere la creazione di auto e la stampa a schermo di tutti i dati relativi ad un’auto
• Deve poter permettere inoltre di modificare il nome dell’auto
• Deve poter calcolare il costo totale per la produzione dell’auto
16
QUALCHE LEZIONE FA..
Automobili: Le strutture dati - Codice C
17
QUALCHE LEZIONE FA..
AUTOMOBILE NOME COSTO COLORE
COMPONENTI NOME COSTO CATEGORIA LIBRETTO
ANNO IMM.
PROVINCIA CLASSE EURO
18
Lavoriamo con i file
•
Vogliamo poter salvare tutte le informazioni di una automobile su file e poterle rileggere indietro19
Per poter salvare un’automobile...
20
AUTOMOBILE NOME COSTO COLORE
COMPONENTI NOME COSTO CATEGORIA LIBRETTO
ANNO IMM.
PROVINCIA CLASSE EURO
DOBBIAMO POTER SALVARE I
COMPONENTI
DOBBIAMO POTER SALVARE I
LIBRETTI
E’ UN DATO STRUTTURATO!
Organizzazione dei dati
21
auto_db
componenti_db
libretto_db
Organizzazione dei dati
22
auto_db
componenti_db
libretto_db
AUTOMOBILE NOME COSTO COLORE
COMPONENTI NOME COSTO CATEGORIA LIBRETTO
ANNO IMM.
PROVINCIA CLASSE EURO
Scrittura su file
•
Ci è comodo avere una funzione per la scrittura di una singola linea di un file...23
Scrittura su file
•
Ci è comodo avere una funzione per la scrittura di una singola linea di un file...23
Salviamo un libretto...
24
Salviamo un libretto...
24
Salviamo un libretto...
24
Salviamo i componenti...
25
Salviamo i componenti...
25
Salviamo i componenti...
25
Salviamo l’auto...
26
Salviamo l’auto...
26
Salviamo l’auto...
26
Lettura da file
Scriviamo ora il codice
per leggere i dati da file..
27
Lettura del libretto di circolazione
28
Lettura del libretto di circolazione
28
Leggiamo i componenti
29
E’ una stringa!
Sono più linee!
Leggiamo i componenti
30
E’ una stringa!
Sono più linee!
Leggiamo i componenti
30
E’ una stringa!
Sono più linee!
Leggiamo i componenti
30
E’ una stringa!
Sono più linee!
Leggiamo i componenti
31
Leggiamo l’automobile
32
Leggiamo l’automobile
32
Finiamo il main() ...
33
Tutte il materiale sarà disponibile sul mio sito internet:
alessandronacci.com
34
Potete lasciare il vostro giudizio qui:
http://tinyurl.com/IEIMExe2014