• Non ci sono risultati.

Corso di Informatica Medica

N/A
N/A
Protected

Academic year: 2021

Condividi "Corso di Informatica Medica"

Copied!
52
0
0

Testo completo

(1)

Corso di Informatica Medica

Esercitazione IX

!

Alessandro A. Nacci

[email protected] - alessandronacci.com

1

(2)

LA MAPPA DEL TESORO

2

(3)

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

(4)

La strategia del pirata

Un pirata che

vuole muoversi su questa mappa per cercare il tesoro, si sposterà

sempre dalle

celle con colori più freddi verso celle con colori più caldi

4

(5)

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

(6)

Gestione della mappa termica

Come rappresentiamo la mappa termica?

6

(7)

Gestione della mappa termica

Come rappresentiamo la mappa termica?

6

Usiamo una matrice

I colori diventano numeri

(8)

Gestione della mappa termica

Come rappresentiamo la mappa termica?

6

Usiamo una matrice

I colori diventano numeri

(9)

Include, 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

(10)

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.

(11)

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;

(12)

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

(13)

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;

(14)

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.

(15)

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é…

(16)

Metti tesoro - corretto

11

(17)

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.

(18)

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");

(19)

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.

(20)

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;

} }

}

(21)

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);

}

(22)

GESTIONE

AUTOMOBILI

15

(23)

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

(24)

Automobili: Le strutture dati - Codice C

17

QUALCHE LEZIONE FA..

AUTOMOBILE NOME COSTO COLORE

COMPONENTI NOME COSTO CATEGORIA LIBRETTO

ANNO IMM.

PROVINCIA CLASSE EURO

(25)

18

(26)

Lavoriamo con i file

Vogliamo poter salvare tutte le informazioni di una automobile su file e poterle rileggere indietro

19

(27)

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!

(28)

Organizzazione dei dati

21

auto_db

componenti_db

libretto_db

(29)

Organizzazione dei dati

22

auto_db

componenti_db

libretto_db

AUTOMOBILE NOME COSTO COLORE

COMPONENTI NOME COSTO CATEGORIA LIBRETTO

ANNO IMM.

PROVINCIA CLASSE EURO

(30)

Scrittura su file

Ci è comodo avere una funzione per la scrittura di una singola linea di un file...

23

(31)

Scrittura su file

Ci è comodo avere una funzione per la scrittura di una singola linea di un file...

23

(32)

Salviamo un libretto...

24

(33)

Salviamo un libretto...

24

(34)

Salviamo un libretto...

24

(35)

Salviamo i componenti...

25

(36)

Salviamo i componenti...

25

(37)

Salviamo i componenti...

25

(38)

Salviamo l’auto...

26

(39)

Salviamo l’auto...

26

(40)

Salviamo l’auto...

26

(41)

Lettura da file

Scriviamo ora il codice

per leggere i dati da file..

27

(42)

Lettura del libretto di circolazione

28

(43)

Lettura del libretto di circolazione

28

(44)

Leggiamo i componenti

29

E’  una  stringa!

Sono  più  linee!

(45)

Leggiamo i componenti

30

E’  una  stringa!

Sono  più  linee!

(46)

Leggiamo i componenti

30

E’  una  stringa!

Sono  più  linee!

(47)

Leggiamo i componenti

30

E’  una  stringa!

Sono  più  linee!

(48)

Leggiamo i componenti

31

(49)

Leggiamo l’automobile

32

(50)

Leggiamo l’automobile

32

(51)

Finiamo il main() ...

33

(52)

Tutte il materiale sarà disponibile sul mio sito internet:

alessandronacci.com

34

Potete lasciare il vostro giudizio qui:

http://tinyurl.com/IEIMExe2014

Riferimenti

Documenti correlati

una singola disposizione, ma ripensare più compiutamente sul valore dei principi di legalità, di obbligatorietà dell’azione penale, sulla discrezionalità della

Al fine di escludere un quadro di ipertensione endocranica con fundus oculi nella norma venivano testati i potenziali evocati visivi, risultati nella norma; per comparsa

Copyright© 2011-2016 owned by Ubaldo Pernigo, www.ubimath.org - contact: [email protected] Il presente lavoro è coperto da Licenza Creative Commons Attribuzione - Non commerciale -

aquilone alligatore pneumatico anatroccolo stivale aviatore edicola oceano

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

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

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

panino, biglietto, ristorante, vino, film, ufficio, gelato, albergo, autobus, bar, ospedale,. ombrello,