LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH ....

18
LE STRUTTURE Tipo di dati derivato, contenitore di altri tipi Esempio: definizione di una struttura struct car { int wheels; //membri della struttura int cc; // ma anche array, puntatori o altre strutture }; Esempio dichiarazione di variabile: struct car mycar; Una struttura puo’ contentere altre strutture Una struttura non puo’ contentere se stessa Ma un puntatore del suo tipo si ad es : struct car *second_car; Ricordiamocelo! ci sarà utile quando faremo le liste

Transcript of LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH ....

Page 1: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

LE STRUTTURE •  Tipo di dati derivato, contenitore di altri tipi •  Esempio: definizione di una struttura

struct car { int wheels; //membri della struttura int cc; // ma anche array, puntatori o altre strutture

}; •  Esempio dichiarazione di variabile:

•  struct car mycar; •  Una struttura puo’ contentere altre strutture •  Una struttura non puo’ contentere se stessa

•  Ma un puntatore del suo tipo si •  ad es : struct car *second_car;

•  Ricordiamocelo! ci sarà utile quando faremo le liste

Page 2: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

LE STRUTTURE

•  Altre dichiarazioni: struct car { //L’etichetta della struttura è facoltativa

int wheels; int cc;

} my_car; •  Dichiarazione con inizializzione

•  struct car my_car = {4, 1500}; •  struct car my_car = {.wheels=4, .cc=1500}; •  struct car my_car = {.wheels=4 }; //il resto a 0

•  Accedere ai membri: •  Tramite il nome della variabile:

•  printf (“La mia macchina ha %d ruote\n”, my_car.wheels); •  O un puntatore:

•  struct car *my_car_ptr; •  my_car_ptr = &my_car; •  printf (“La mia macchina ha %d ruote\n”, my_car_ptr->wheels);

wheels cc

struct car

Page 3: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

ATTENZIONE ALLA SEMANTICA

struct car { int wheels; int cc;

}; •  Esempio di cattiva programmazione

•  struct car my_bike; •  my_bike.wheels = 2; •  my_bike.cc = 0;

•  Anche se sintatticamente corretto, è semanticamente sbagliato •  Se leggi un programma, cosa ti aspetti da una struttura

che si chiama “car”?

Page 4: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

TYPEDEF •  Le strutture sono passate alle funzioni per valore •  Con typedef è possibile creare alias:

•  Possiamo definire alias di interi, puntatori, strutture etc. •  esempio: typedef struct car Car; •  Utile per scrivere in forma piu’ compatta:

•  ad es: Car c; •  … o per renderlo più portabile, leggibile e mantenibile

•  Molto usate •  ad es. in <stdint.h> c’è

typedef signed char int8_t •  Differenza typedef e #define

•  typedef agisce sui TIPI •  define (tipicamente) sui VALORI

Page 5: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

UNIONI •  Tipo di dato derivato (come le strutture) ma i membri

condividono lo stesso spazio di memoria •  Esempio:

union number { int x; double y;

}; Contiene UN elemento, o un intero o un double

double y int x

Union number

double

int

Page 6: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

ESERCITAZIONE STRUCT 1.  Lavoriamo su punti geometrici (x, y) tra con ogni punto

compreso tra 0 e 100 e definiamoci una struct 1.  Utilizzare il typedef per le struct

2.  Array di questi punti

1.  locale 3.  Inserire i punti con una rand

1.  Include guards 4.  Calcoliamo il baricentro dei punti

Page 7: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

SWITCH

Page 8: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

COMBINAZIONI

AND

OR

Page 9: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

TRANSISTOR

Page 10: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

ALGEBRA BOOLEANA •  Particolare tipo di algebra in cui le variabili

possono solo assumere i valori 0 e 1 •  Ideata da George Boole (1815-1864)

•  Ogni circuito digitale può essere espresso attraverso una funzione booleana

•  Tre operatori principali: AND, OR e NOT •  tutte le funzioni booleane posso essere

espresse con questi operatori •  Ogni funzione booleana puo’ essere

espressa da una tabella di verità •  Di dimensione 2n se n sono gli argomenti

della funzioni

George Boole

Page 11: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

ALGEBRA BOOLEANA

Simboli Operazione ∧ AND V OR ⊻ XOR ¬ NOT

Page 12: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

TABELLE DI VERITÀ

AND 0 0 0 0 1 0 1 0 0 1 1 1

OR 0 0 0 0 1 1 1 0 1 1 1 1

XOR 0 0 0 0 1 1 1 0 1 1 1 0

NOT 0 1 1 0

Esercizio: date le variabili booleane A e B, scrivere la tabella di verità di •  NOT (A AND NOT B) •  A XOR B XOR B

Page 13: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

ESEMPIO

Page 14: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

LEGGI DI DE-MORGAN •  Regole di trasformazioni di espressioni booleane:

•  ¬(A ∧ B) ç è ¬A V ¬B •  ¬(A V B) ç è ¬A ∧ ¬B

•  Dimostrazione tabellare

•  generalizzabili ad N variabili… •  ¬(A ∧ B ∧ C) ç è ¬A V ¬B V ¬C

Page 15: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

C: APPLICAZIONI if ( !( condizione1 && condizione2) ) { …. Puo’ diventare:

if (!condizione1 || !condizione2) ) { ….

Page 16: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

REGOLE DI SEMPLIFICAZIONE

•  Per semplificare una formula booleana puo’ essere ottenuta applicando queste regole

•  O mediante altri sistemi come le mappe di Karnaugh •  Particolarmente importante per semplificare circuiti logici

•  Ai fini di questo corso non approfondiremo l’argomento

Page 17: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

LAVORARE SUI BIT Non esiste il tipo di dato “bit”, si lavora su interi (short, int etc) Ci sono pero’ alcuni operatori “bit a bit”:

Operatore & And bit a bit | OR bit a bit ^ OR esclusivo bit a bit << spostamento (shift a

sinstra) >> spostamento a destra ~ complemento a 1 (NOT)

Page 18: LE STRUTTURE - uniroma2.itstud.netgroup.uniroma2.it/~lorenzo/fondinf2018/C5.pdf · SWITCH . COMBINAZIONI AND OR . TRANSISTOR . ALGEBRA BOOLEANA ... • Ogni circuito digitale può

ESEMPIO •  memorizzare 2^3 su un numero intero •  moltiplichiamo il numero precedente per 2 •  stampare tutti i bit che compongono uno short

•  subnetting

•  crittografia