Docsity

Algoritmi e Strutture Dati, Dispense di Elementi di Informatica

Anteprima parziale del testo

Scarica Algoritmi e Strutture Dati e più Dispense in PDF di Elementi di Informatica solo su Docsity!

1

Algoritmi e

Strutture Dati

Lezione 1

Antonio Luca alfeo

[email protected]

http://mlpi.ing.unipi.it/alfeo

2 “Ma io so già programmare!” Fondamenti I Sia dato un array contenente delle frasi. Scrivere un programma che prenda in input una stringa e restituisca tutte le frasi che la contengono. Fondamenti II Realizzare un motore di ricerca che abbia complessità O(log n) sulle operazioni di lettura. Algoritmi e Strutture Dati

4 Sommario

  • Debug Triviale
  • Gestione Dinamica Input e Liste
  • Debug Assistito
  • Soluzioni della Standard Template Library
  • Gestione Stringhe e Vector

If debugging is the process

of removing software bugs,

then programming must be the

process

of putting them in

E. Dijkstra

Debugging

7 Tecniche  Testo  Visuale  “Debugger” (es GDB )  Compilatore  Analisi Memoria ( Valgrind )

8 Debug Visuale 1 7 3 5 2 10

10 Debug Visuale // stampa con buco void stampaArray( int arr[] , int len , int buco) // PER OGNI ELEMENTO // SE SONO IN POSIZIONE buco, SALTO // ALTRIMENTI STAMPO ELEMENTO // stampa "segno" in "posizione" void stampaSegno( int posizione , int segno ) // SALTO TUTTI GLI ELEMENTI FINO A posizione // STAMPO IL SEGNO

11 Debug Visuale // stampa con buco void stampaArray( int arr[] , int len , int buco) { for ( int i=0 ; i < len ; ++i ) { if (i==buco) cout << "\t"; else cout << arr[i] << "\t" ; } cout << endl; } // stampa "segno" in "posizione" void stampaSegno( int posizione , int segno ) { for ( int i = 0 ; i < posizione ; ++i ) cout << "\t"; cout << segno << "\n"; }

13 Tipo Accessi vs

14 Lettura Input SORGENTE (^) DATI DESTINAZIONE DATI DATI DATI std::cin std::cout std::ifstream std::ofstream

16 Lettura Input leggiInput( ) // 1) LEGGO PRIMO VALORE (numero elementi) // 2) ALLOCAZIONE MEMORIA // 3) LETTURA CARATTERE PER CARATTERE

17 Lettura Input int * leggiInput( ) { cin >> len; int * arr = new int[len]; for ( int i = 0 ; i < len ; ++i ) cin >> arr[i]; return arr; }

19 liste head tail value next value next value next Solo inserimento in coda

20 Lettura su Lista Obj * leggiInput() { // LEGGO LUNGHEZZA // VARIABILI DI APPOGGIO // PER TUTTA LA LUNGHEZZA { // LEGGO VALORE // CREO E INIZIALIZZO OGGETTO // AGGIORNO TESTA } // RITORNO TESTA }