Professional Documents
Culture Documents
izi di C++
V0.8
Copyright
2006
bute and/or modify this do
ument under the terms of the GNU Free Do
umentation Li
ense, Version 1.2 or any later version published by the Free
Software Foundation; with no Invariant Se
tions, no Front-Cover Texts, and
no Ba
k-Cover Texts. A
opy of the li
ense is in
luded in the se
tion entitled
"GNU Free Do
umentation Li
ense".
Indi
e
Prefazione
I Eser izi
11
12
EL.1
. . . . . . . . . . . . . . . .
12
EL.2
Somma Elementi . . . . . . . . . . . . . . . . . . . . . . . .
13
EL.3
Coda Pari . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
EL.4
Min e Max
. . . . . . . . . . . . . . . . . . . . . . . . . . .
13
EL.5
ListaStati a . . . . . . . . . . . . . . . . . . . . . . . . . . .
14
EL.6
Ordinata . . . . . . . . . . . . . . . . . . . . . . . . . . .
14
EL.7
Elimina Tutti . . . . . . . . . . . . . . . . . . . . . . . . . .
15
EL.8
Somma Coda . . . . . . . . . . . . . . . . . . . . . . . . . .
15
EL.9
. . . . . . . . . . . . . . . . . . . . .
15
EL.10
15
EL.11
16
EL.12
Ribalta
17
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
18
EA.1
Albero Binario . . . . . . . . . . . . . . . . . . . . . . . . .
18
EA.2
Numero Elementi
. . . . . . . . . . . . . . . . . . . . . . .
19
EA.3
O orrenze
. . . . . . . . . . . . . . . . . . . . . . . . . . .
19
EA.4
O orrenza Massima . . . . . . . . . . . . . . . . . . . . . .
19
EA.5
Profondit Limitata
20
. . . . . . . . . . . . . . . . . . . . . .
EA.6
Somma
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
21
EA.7
Sostituis i . . . . . . . . . . . . . . . . . . . . . . . . . . . .
21
EA.8
. . . . . . . . . . . . . . . . . . . . . . .
21
EA.9
22
EA.10
Profondita Maggiore Di . . . . . . . . . . . . . . . . . . . .
22
EA.11
Profondit Massima
22
. . . . . . . . . . . . . . . . . . . . . .
INDICE
EA.12
Somma Livello . . . . . . . . . . . . . . . . . . . . . . . . .
23
EA.13
Eliminazione Foglia
. . . . . . . . . . . . . . . . . . . . . .
23
EA.14
Eliminazione Foglie
. . . . . . . . . . . . . . . . . . . . . .
23
EA.15
Operatore di Confronto
EA.16
24
EA.17
Conta Nodi . . . . . . . . . . . . . . . . . . . . . . . . . . .
24
. . . . . . . . . . . . . . . . . . . .
24
25
Push Greater . . . . . . . . . . . . . . . . . . . . . . . . . .
25
27
EC.1
Coda
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
27
EC.2
Coda on Perdite . . . . . . . . . . . . . . . . . . . . . . . .
28
EC.3
Coda a Priorit . . . . . . . . . . . . . . . . . . . . . . . . .
29
EC.4
PopMinMax
30
. . . . . . . . . . . . . . . . . . . . . . . . . .
32
EX.1
A umulatore . . . . . . . . . . . . . . . . . . . . . . . . . .
32
EX.2
Cifratore
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
32
EX.3
33
EX.4
Predittore di Temperatura
. . . . . . . . . . . . . . . . . .
34
EX.5
Contenitore . . . . . . . . . . . . . . . . . . . . . . . . . . .
35
EX.6
Lista Prenotazioni . . . . . . . . . . . . . . . . . . . . . . .
37
EX.7
Classi a
38
EX.8
Agenzia Matrimoniale . . . . . . . . . . . . . . . . . . . . .
39
EX.9
Par o Pattini . . . . . . . . . . . . . . . . . . . . . . . . . .
41
EX.10
Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
42
EX.11
Timer Avanzato
. . . . . . . . . . . . . . . . . . . . . . . .
44
EX.12
Votazioni . . . . . . . . . . . . . . . . . . . . . . . . . . . .
44
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
II Soluzioni
46
47
SL.1
. . . . . . . . . . . . . . . .
47
SL.2
Somma Elementi . . . . . . . . . . . . . . . . . . . . . . . .
52
SL.3
Coda Pari . . . . . . . . . . . . . . . . . . . . . . . . . . . .
53
SL.4
Min e Max
. . . . . . . . . . . . . . . . . . . . . . . . . . .
56
SL.5
ListaStati a . . . . . . . . . . . . . . . . . . . . . . . . . . .
56
SL.6
Ordinata . . . . . . . . . . . . . . . . . . . . . . . . . . .
58
SL.7
Elimina Tutti . . . . . . . . . . . . . . . . . . . . . . . . . .
59
INDICE
SL.8
Somma Coda . . . . . . . . . . . . . . . . . . . . . . . . . .
59
SL.9
. . . . . . . . . . . . . . . . . . . . .
60
SL.10
61
SL.11
62
SL.12
Ribalta
65
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
69
SA.1
Albero Binario . . . . . . . . . . . . . . . . . . . . . . . . .
69
SA.2
Numero Elementi
. . . . . . . . . . . . . . . . . . . . . . .
75
SA.3
O orrenze
. . . . . . . . . . . . . . . . . . . . . . . . . . .
75
SA.4
O orrenza Massima . . . . . . . . . . . . . . . . . . . . . .
76
SA.5
Profondit Limitata
80
. . . . . . . . . . . . . . . . . . . . . .
SA.6
Somma
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
81
SA.7
Sostituis i . . . . . . . . . . . . . . . . . . . . . . . . . . . .
81
SA.8
. . . . . . . . . . . . . . . . . . . . . . .
82
SA.9
87
SA.10
Profondita Maggiore Di . . . . . . . . . . . . . . . . . . . .
88
SA.11
Profondit Massima
. . . . . . . . . . . . . . . . . . . . . .
88
SA.12
Somma Livello . . . . . . . . . . . . . . . . . . . . . . . . .
89
SA.13
Eliminazione Foglia
. . . . . . . . . . . . . . . . . . . . . .
89
. . . . . . . . . . . . . . . . . . . . . .
94
SA.14
Eliminazione Foglie
SA.15
Operatore di Confronto
SA.16
SA.17
. . . . . . . . . . . . . . . . . . . . 100
107
111
SC.1
Coda
SC.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
SC.3
SC.4
PopMinMax
. . . . . . . . . . . . . . . . . . . . . . . . . . 124
125
SX.1
A umulatore . . . . . . . . . . . . . . . . . . . . . . . . . . 125
SX.2
Cifratore
SX.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
SX.4
Predittore di Temperatura
SX.5
Contenitore . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
. . . . . . . . . . . . . . . . . . 131
SX.6
INDICE
SX.7
Classi a
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
SX.8
SX.9
SX.10
Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
SX.11
Timer Avanzato
SX.12
Votazioni . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
. . . . . . . . . . . . . . . . . . . . . . . . 155
A.2
Verbatim Copying
161
. . . . . . . . . . . . . . . . . 161
. . . . . . . . . . . . . . . . . . . . . . . 163
A.3
A.4
A.5
A.6
A.7
A.8
Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
A.9
Termination
A.10
. . . . . . . . . . . . 166
. . . . . . . . . . . . . . . . . . . . . . . . . . 166
. . . . . . . . . . . . . . . . 167
Prefazione
Gli eser
izi presentati in questo eser
iziario sono stati proposti a studenti di
Ingegneria delle Tele
omuni
azioni nell'ambito di un
orso di Programmazio-
ne I.
Il
orso aveva lo s
opo di introdurre alla programmazione orientata agli
oggetti (OOP) utilizzando il linguaggio C++. Una rilevante parte del programma arontava lo studio dei tipi di dati astratti,
on parti
olare enfasi alle
strutture dati di tipo
ontenitore, stressandone i
on
etti di in
apsulamento
ed interfa
ia.
lass
Prefazione
Per esempio, la spe i a di un ipoteti o metodo di eliminazione di elementi da una lista, potrebbe apparire ome segue.
Nome
Elimina()
Param. Ingr.
TElem
Param. Us .
unsigned int
new).
Questa s elta,
. pp.
.h e . pp),
enfatizzando
Prefazione
main()) utile
Stampa()):
iostream,
in
out.
deniti in una
lasse, vengono automati
amente sintetizzati dal
ompilatore e, se invo
ati dall'utente, produ
ono una
opia super
iale dell'oggetto
(shallow-
opy ). Se questo
omportamento s
orretto o
omunque indesiderato possibile rendere del tutto indisponibili le funzionalit di
opia o
assegnazione tra oggetti della
lasse. Ci si ottiene di
hiarando nella sezione
private
plementazione [5. Cos fa
endo, qualsiasi
ostrutto
he nis
a per invo
are
uno di questi due metodi produrr un errore di
ompilazione. Tale te
ni
a
viene spesso utilizzata nelle soluzioni degli eser
izi presentati.
Compilare i sorgenti
Tutti i sorgenti presentati sono stati
ompilati
on la versione 3.3.1 della suite di
ompilazione GNU/GCC [6, utilizzando le seguenti opzioni di
ompilazione:
Prefazione
L'opzione
-Wall
dei messaggi di warning
he, pur non
ompromettendo la
orretta
ompilazione del programma, sono sintomi di impre
isioni all'interno del
odi
e. Le
altre due opzioni indu
ono il
ompilatore ad a
ettare es
lusivamente
odi
e
strettamente aderente allo standard ISO-C++ [7, riutando la
ompilazione
di eventuali estensioni non standard del linguaggio.
Il
odi
e sorgente delle soluzioni stato s
ritto utilizzando l'ambiente
di sviluppo Dev-C++ [8, nella sua versione 4.9.9.0, utilizzabile su sistemi
TM
operativi della famiglia Mi
rosoft
Windows. Tale software
onsiste di un
editor gra
o
he funge da interfa
ia per le operazioni di stesura,
ompilazione e debugging del
odi
e sorgente, oltre a fornire ed installare an
he la
suite di
ompilazione GNU/GCC. In ogni
aso, pur
h disponga di un
ompilatore
onforme allo standard ISO-C++, qualsiasi altro ambiente di sviluppo,
o an
he un sempli
e editor di testi, possono essere
onsiderati validi ai ni
della stesura del
odi
e sorgente.
Prefazione
int
main ( ) {
s t r i n g buf ;
map<s t r i n g , int > m;
while (
i n >> buf )
m[ buf ++;
Una volta
he la STL sia nota, i vantaggi di un tale appro
io risultano evidenti relativamente agli aspetti di (i) tempo di stesura; (ii)
orrettezza del
odi
e; (iii) individuazione
degli errori; (iv)
omprensibilit; (v) manutenibilit; (vi) estensibilit; (vii) aderenza agli
standard.
Nell'apprendere le nozioni della progettazione e le buone prassi per la
stesura del
odi
e, as
oltare
osa hanno da dir
i `i giganti' al proposito, pu
servire molto.
Prefazione
10
Modern C++ Design [11 parti
olarmente illuminante sotto questo punto di
vista. Il libro apre le porte ad un utilizzo estremamente elegante dei template,
inimmaginato perno da
hi li aveva originariamente progettati. Seguendo la
sua impostazione nella stesura del software e le sue linee guida si perviene al
progetto di ar
hitetture software limpide e snelle, ma
ontemporaneamente
estremamente potenti e versatili.
Contattare l'autore
Commenti, suggerimenti e segnalazioni sono graditi.
ontattato all'indirizzo email:
1 L'opzione
: -Weff ++.
mespositunina.it
L'autore pu essere
Parte I
Eser
izi
11
Capitolo EL
Eser
izi su liste
EL.1 Lista Sempli
emente Collegata
Soluzione a pag. 47
int
Lista.
Il tipo
TElem
seguito.
Nome
Param. Ingr.
Param. Us .
Lista()
Lista
TElem
int
Lista()
Costruttore di
opia.
Lista()
Distruttore.
Inseris
i()
Inserimento in testa alla lista.
NumeroElementi()
Svuota()
TElem
Svuota la lista.
Elimina()
12
13
Stampa sullo standard output tutti gli elementi ontenuti nella lista.
Ri er a()
TElem
bool
Dotare la lasse
spe
i
a.
Nome
Param. Ingr.
Param. Us .
Somma()
TElem
Dotare la lasse
LinkedList
CodaPari(),
se ondo
Param. Ingr.
CodaPari()
true
Restituis e
Param. Us .
false
bool
altrimenti.
Dotare la lasse
spe
i
a.
Nome
Param. Ingr.
Param. Us .
MinMax()
TElem,TElem
14
EL.5 ListaStati
a
Soluzione a pag. 56
int
Nome
Param. Ingr.
Param. Us .
Lista()
TElem
Costruttore.
Lista()
Distruttore.
Inseris iInCoda()
Svuota()
Count()
Stampa().
EL.6 Ordinata
Soluzione a pag. 58
Dotare la lasse
Lista
EOrdinata(),
se ondo la
Param. Ingr.
Param. Us .
EOrdinata()
Restituis
e true
bool
false
altrimenti.
15
Dotare la lasse
se ondo la
Param. Ingr.
Param. Us .
EliminaTutti()
TElem
int
Dotare la lasse
LinkedList
SommaCoda(),
se ondo
Param. Ingr.
Param. Us .
SommaCoda()
Dotare la lasse
Param. Ingr.
Param. Us .
SpostaTestaInCoda()
bool
true
se lo spostamento eettuato,
false
Il metodo restituis e
altrimenti.
Dotare la
lasse
ed
16
el
el
el
last
first
Nome
Param. Ingr.
Param. Us .
EliminaElPostoPari()
unsigned int
Elimina dalla lista tutti gli elementi di posto pari (0, 2, 4, ...).
Restituis
e il numero di elementi eliminati.
EliminaElPostoDispari()
unsigned int
Elimina dalla lista tutti gli elementi di posto dispari (1, 3, 5, ...).
Restituis
e il numero di elementi eliminati.
TElem
uguale al tipo
int.
Lista.
Nome
Param. Ingr.
Param. Us .
Lista()
TElem
Costruttore.
Lista()
Distruttore.
Inseris i()
Svuota()
Svuota la lista.
17
Count()
unsigned int
StampaDiretta()
StampaInversa()
StampaAlternata()
Stampa il
ontenuto della lista nel seguente ordine: primo elemento, ultimo elemento, se
ondo elemento, penultimo elemento, terzo
elemento, terzultimo elemento...
Gli uni
i metodi della
lasse Lista
he possono utilizzare lo standardoutput (
out) sono i metodi di stampa. Gli altri metodi (pubbli
i, privati o
protetti) non possono fare uso degli oggetti per l'I/O.
Si realizzi una funzione
EL.12 Ribalta
Soluzione a pag. 65
Dotare la lasse
Lista
Ribalta()
se ondo la se-
guente spe
i
a.
Nome
Param. Ingr.
Param. Us .
Ribalta()
Capitolo EA
Eser
izi su alberi binari
EA.1 Albero Binario
Soluzione a pag. 69
Realizzare la
lasse
tipo
int.
AlberoBinario.
Il tipo
TElem
Param. Ingr.
Param. Us .
AlberoBinario()
AlberoBinario()
AlberoBinario
Costruttore di opia.
AlberoBinario()
Distruttore della struttura.
AggiungiElem()
TElem
InAlb()
TElem
Ri er a un elemento nell'albero.
Restituis e
Elimina()
TElem
true
false
bool
nel
aso in
ui
altrimenti.
Svuota()
Svuota la struttura.
18
19
PostOrdine()
InOrdine()
Dotare la lasse
la seguente spe
i
a.
Nome
Param. Ingr.
Param. Us .
NumElem()
unsigned int
EA.3 O
orrenze
Soluzione a pag. 75
Dotare la lasse
AlberoBinario
O orrenze(),
Param. Ingr.
Param. Us .
O orrenze()
TElem
unsigned int
AlberoBinario
20
maxo .
AlberoBinario.
Nome
Param. Ingr.
Param. Us .
AlberoBinario()
unsigned int
Inseris
i()
Inseris
e
on
TElem
l'elemento
o orrenza
spe i ato
minore
bool
nell'albero
dell'o orrenza
se
esso
massima
presente
spe i ata
nel
ostruttore.
Il metodo restituis
e
true
false
a se onda he l'inserimento si
avvenuto o meno.
AlberoBinario
AlberoBinario:
Nome
Param. Ingr.
Param. Us .
AlberoBinario()
unsigned int
Costruttore
on parametro intero non negativo. Il parametro di ingresso indi
a la massima profondit
he l'albero pu assumere durante
il suo
i
lo di vita.
Inseris i()
TElem
bool
true
false
21
EA.6 Somma
Soluzione a pag. 81
Dotare la lasse
AlberoBinario
Somma()
se ondo
la seguente spe
i
a.
Nome
Param. Ingr.
Param. Us .
Somma()
TElem
EA.7 Sostituis
i
Soluzione a pag. 81
Dotare la lasse
AlberoBinario
Sostituis i()
Sostituis
i()
Detti i e j i parametri
o
orrenze dell'elemento
Param. Ingr.
Param. Us .
TElem,TElem
unsigned int
on l'elemento
j.
Restituis e il numero di
sostituzioni eettuate.
N.B.: questo metodo in generale non preserva la propriet di ordinamento
dell'albero.
albero ordinato.
Dotare la lasse
AlberoBinario
ContaMinMax(),
Param. Ingr.
Param. Us .
ContaMinMax()
TElem,TElem
unsigned int
Min
Max
22
Dotare la lasse
Param. Ingr.
Param. Us .
ProfMaggioreDiDue()
bool
false
altrimenti.
Dotare la lasse
Param. Ingr.
Param. Us .
ProfMaggioreDi()
unsigned int
bool
Predi ato he indi a se la profondit dell'albero strettamente maggiore del valore intero rappresentato dal parametro di ingresso. Restituis e
true
false
altrimenti.
Dotare la lasse
AlberoBinario
Profondita(),
Param. Ingr.
Param. Us .
Profondita()
TElem
int,bool
23
Dotare la lasse
AlberoBinario
SommaLivello()
Param. Ingr.
Param. Us .
SommaLivello()
TElem
Dotare la lasse
AlberoBinario
EliminaFoglia()
Param. Ingr.
Param. Us .
EliminaFoglia()
TElem
bool
false
true
in aso di eliminazione
altrimenti.
Dotare la lasse
AlberoBinario
EliminaFoglie()
Param. Ingr.
Param. Us .
EliminaFoglie()
unsigned int
24
AlberoBinario
Dotare la lasse
Param. Ingr.
Param. Us .
operator==()
AlberoBinario
bool
true
false
altrimenti.
Dotare la lasse
Param. Ingr.
Param. Us .
ContaNodiNonFoglia()
unsigned int
Dotare la lasse
AlberoBinario
ContaNodi()
Param. Ingr.
Param. Us .
ContaNodi()
unsigned int,
unsigned int,
unsigned int
1 e 2 gli,
se-
Capitolo EP
Eser
izi su pile
EP.1 Push Greater
Soluzione a pag. 107
Pila
ostrutto
Il tipo
Pila.
Nome
Param. Ingr.
Param. Us .
Pila()
TElem
Pila()
Distruttore.
Push()
PushGreater()
TElem
bool
Top()
TElem
26
TElem
Svuota()
int
Svuota la pila.
Count()
Empty()
bool
out).
main().
Capitolo EC
Eser
izi su
ode
EC.1 Coda
Soluzione a pag. 111
Coda
ostrutto
Il tipo
Coda.
Nome
Param. Ingr.
Param. Us .
Coda()
TElem
TElem
Coda()
Distruttore.
Push()
A
oda l'elemento spe
i
ato.
Top()
Pop()
TElem
27
28
Somma()
TElem
Svuota()
int
Svuota la oda.
Count()
Empty()
bool
out).
main().
int.
lass
TElem
Coda
Coda.
Nome
Param. Ingr.
Param. Us .
Coda()
unsigned int
Costruttore
on parametro intero. Il parametro indi
a il numero massimo di posti in
oda, oltre il quale non deve essere possibile inserire
ulteriori elementi.
Coda()
Distruttore.
Push()
TElem
false
altrimenti.
true
bool
in
aso di elemento
29
Top()
TElem
Pop()
TElem
Pop()
unsigned int
TElem
Svuota()
int
Svuota la oda.
Count()
Empty()
bool
out).
main().
elementi
he
(bassa). Un
30
Coda.
Nome
Param. Ingr.
Param. Us .
PriorityQueue()
TElem
Costruttore.
PriorityQueue()
Distruttore.
PushLow()
PushHigh()
TElem
Pop()
TElem
Svuota()
bool
Svuota la oda.
Empty()
out).
main().
EC.4 PopMinMax
Soluzione a pag. 124
Dotare la lasse
la seguente spe i a.
31
Nome
Param. Ingr.
Param. Us .
PopMax()
Detto n il
unsigned int
TElem
estrae i primi
PopMin()
Detto n il
unsigned int
TElem
estrae i primi
Capitolo EX
Altri eser
izi
EX.1 A
umulatore
Soluzione a pag. 125
Si realizzi la lasse
A umulatore
Nome
Param. Ingr.
Param. Us .
A umulatore()
float
Add()
Reset()
float
GetValue()
EX.2 Cifratore
Soluzione a pag. 126
Implementare la lasse
Cifratore
ratteri attraverso uno slittamento del
odi
e ASCII dei
aratteri
omponenti
la stringa (
.d.
odi
e di Cesare). L'interfa
ia della
lasse sia la seguente:
32
33
Nome
Param. Ingr.
Param. Us .
Cifratore()
int
Cifra()
har
har
De ifra()
har
har
Cifra()
ListaDellaSpesa
lass del linguaggio e ri
orrendo ad un'implemen-
Arti olo
Param. Ingr.
Param. Us .
ListaDellaSpesa()
Costruttore.
ListaDellaSpesa()
Distruttore.
34
Aggiungi()
Nome,Quantita
Quantita
Elimina()
Nome
bool
Elimina dalla lista l'elemento avente il nome spe
i
ato (se presente).
Il metodo restituis
e
true
false
altrimenti.
GetQuantita()
Nome
Quantita
Svuota()
Svuota la lista.
Stampa()
Nome:
Quantit, ...
Nome:
Quantit,
Realizzare la
lasse
temperatura.
TempPredi tor
La stima operata a
Per esempio,
35
Param. Ingr.
Param. Us .
TempPredi tor()
Time,Temp
SetTemp()
Time,Temp
EstimateTemp()
Time
Temp
EstimateTemp(),
io, fornir
del ostruttore . Su essivamente l'utente omuni her all'oggetto nuovi valori della temperatura attraverso ripetute hiamate al metodo
SetTemp(),
EX.5 Contenitore
Soluzione a pag. 133
Contenitore
me-
Un Contenitore pu ontenere
};
36
Nome n;
Peso p;
Contenitore.
Nome
Param. Ingr.
Param. Us .
Contenitore()
Peso
Peso.
Il parametro indi a il
Contenitore()
Nome,Peso
bool
Distruttore.
Inseris i()
Inseris e nel ontenitore un oggetto avente il nome e il peso spe i ato. Il metodo restituis e
true
false
altrimenti.
Svuota()
PesoComplessivo()
Peso
PesoResiduo()
Peso
NumElem()
unsigned int
Stampa()
37
Contenitore
Stampa(). Gli
altri metodi dovranno restituire l'esito delle operazioni eseguite mediante gli
opportuni parametri di passaggio riportati nelle spe
i
he.
lass
ListaPrenotazioni
Prenotazione
Gli
ListaPrenotazioni
siano on-
Param. Ingr.
Param. Us .
ListaPrenotazioni()
int
Costruttore
on parametro intero. Il parametro indi
a il numero massimo di posti disponibili per l'evento, oltre i quali non deve essere
possibile inserire ulteriori prenotazioni.
ListaPrenotazioni()
Matri ola,Nome
bool
Distruttore.
Prenota()
EliminaPrenotazione()
Matri ola
boolt
Elimina dalla lista la prenotazione relativa al
ampo matri
ola spe
i
ato (se presente). Il metodo restituis
e
un elemento,
false
altrimenti.
true
se stato eliminato
38
GetPostiDisponibili()
int
EsistePrenotazione()
Matri
ola
Restituis
e true se esiste la prenotazione
matri
ola spe
i
ato, false altrimenti.
Svuota()
bool
relativa al numero di
Svuota la lista.
Stampa()
Stampa
il
Matri
ola1:
Nome3, ...
ontenuto
dell'intera
lista
nel
formato
seguente:
ListaPrenotazioni
he pu utilizzare lo
metodo Stampa(). Gli altri metodi (pubbli
i,
EX.7 Classi
a
Soluzione a pag. 140
Classifi a
mediante
zata mediante puntatori ed allo
azione dinami
a della memoria. Gli elementi
della lista siano di tipo
TElem,
Classifi a.
39
Nome
Param. Ingr.
Param. Us .
Classifi a()
Nome,unsigned
int
unsigned int
Costruttore.
Classifi
a()
Distruttore.
Aggiungi()
Se la squadra non gi presente, la aggiunge alla struttura e le assegna il punteggio spe
i
ato. Nel
aso di squadra gi presente, aggiunge il punteggio spe
i
ato a quello gi totalizzato. Restituis
e il
numero di punti
orrentemente totalizzati dalla squadra.
Svuota()
Svuota la struttura.
Stampa()
Count()
unsigned int
lass
del linguaggio.
AgenziaMatrimoniale
L'implementazione deve
TElem,
40
AgenziaMatrimoniale.
Nome
Param. Ingr.
Param. Us .
AgenziaMatrimoniale()
Nome,bool
bool
Costruttore.
AgenziaMatrimoniale ()
Distruttore.
AggiungiPersona()
false)
Re-
altrimenti (se
Sposa()
Nome,Nome
bool
Mar a ome sposate le due persone presenti nella struttura ed aventi nomi spe i ati dai parametri di ingresso.
Restituis e l'esito
41
Coniugato()
Nome
bool, bool
NumeroSposi()
unsigned int
NumeroCoppie()
unsigned int
Svuota()
Svuota la struttura.
Stampa()
AgenziaMatrimoniale
he pu utilizzare lo
metodo Stampa(). Gli altri metodi (pubbli
i,
privati o protetti) non possono fare uso degli oggetti per l'I/O.
Si realizzi una funzione
Par oPattini
Param. Ingr.
Param. Us .
Par oPattini()
42
Par oPattini()
Taglia
Distruttore.
AggiungiPattini()
Svuota()
unsigned int
NumeroTotPattini()
Fitta()
Taglia
bool
Mar a ome ttati un paio di pattini della taglia spe i ata dal
Disponibilita()
Taglia
unsigned int
NumeroPattini()
Taglia
unsigned int
Restituzione()
Taglia
bool
true
se eettivamente esisteva un
paio di pattini della taglia spe i ata mar ati ome ttati,
false
altrimenti.
Stampa()
EX.10 Timer
Soluzione a pag. 154
Si realizzi la lasse
Timer
Tale og-
43
INATTIVO
10
GETTIME
START
ATTIVO
10
15
20
INATTIVO
15
GETTIME GETTIME
25
30
35
40
STOP
GETTIME
RESET
GETTIME
Valori conteggio
Nome
Param. Ingr.
Param. Us .
Start()
Time
Stop()
Arresta il
onteggio del tempo.
Reset()
Arresta ed azzera il timer.
GetTime()
Suggerimenti
time t = time(0);
istanzia una variabile
di tipo
al tempo di
di un intero
time()
time.h.
44
Timer
Modi are, se ne essario, l'implementazione del timer per rendere la lasse onforme a queste ulteriori spe i he.
EX.12 Votazioni
Soluzione a pag. 157
Ad ogni intervistato
all'us
ita dal seggio si
hiede il partito per
ui ha votato. In ogni momento
bisogna poi essere in grado di dire quanti voti ha ottenuto
ias
un partito
e qual la distribuzione dei voti tra i partiti. Mediante l'uso del
ostrutto
lass
Votazioni.
Nome
Param. Ingr.
Param. Us .
Votazioni()
unsigned int
unsigned int
Costruttore.
Votazioni()
Distruttore.
AggiungiVoto()
Aggiunge un voto al partito avente il
odi
e spe
i
ato dal parametro di ingresso. Restituis
e il numero di voti a
umulati no a quel
momento dal partito.
Svuota()
Svuota la struttura.
45
GetVotiPartito()
unsigned int
unsigned int
GetNumeroVoti()
unsigned int
GetSituazione()
Votazioni
he
GetSituazione().
Parte II
Soluzioni
46
Capitolo SL
Soluzioni degli eser
izi su liste
SL.1 Lista Sempli
emente Collegata
Tra
ia a pag. 12
File Lista.h
#ifndef _LISTA_H_
#define _LISTA_H_
stru
t Re
ord ;
typedef Re
ord PRe
;
typedef int TElem ;
stru
t Re
ord {
};
TElem e l ;
PRe
s u
;
lass L i s t a {
private :
PRe
f i r s t ;
int
ount ;
publi
:
L i s t a ( ) ; //
o s t r u t t o r e
L i s t a (
onst L i s t a& l ) ;
~ L i s t a ( ) ; // d i s t r u t t o r e
};
senza parametri
//
o s t r u t t o r e d i
o p i a
#endif
/ _LISTA_H_ /
47
lista
48
File Lista. pp
onst
new
Lista : : Lista (
L i s t a& l ) : f i r s t ( 0 ) ,
ount ( l .
ount ) {
( l . f i r s t ) { // s e l a l i s t a l vuota non e s e g u e al
una o p e r a z i o n e
first =
Re
ord ;
f i r s t >e l = l . f i r s t >e l ;
if
PRe
l p = l . f i r s t ;
PRe
p = f i r s t ;
( lp >s u
) {
Re
ord ;
p>s u
=
while
new
p = p>s u
;
l p = lp >s u
;
p>e l = lp >e l ;
}
p>s u
= 0 ; // imposta a 0 i l su
d e l l ' u l t i m o elemento d e l l a l i s t a
void
Lista : : I n s e r i s
i (
PRe
p =
Re
ord ;
p>e l = e l ;
p>s u
= f i r s t ;
first = p;
new
onst
TElem& e l ) {
ount++;
void L i s t a : : Svuota ( )
PRe
tbd ;
while ( f i r s t ) {
}
}
tbd = f i r s t ;
f i r s t = f i r s t >s u
;
tbd ;
delete
ount = 0 ;
void
if
onst
L i s t a : : Elimina (
TElem& e l ) {
( f i r s t ) { // l a l i s t a non vuota
( f i r s t >e l == e l ) { // l ' elemento da e l i m i n a r e i n t e s t a
PRe
tbd = f i r s t ; // t b d = t o be d e l e t e d
if
49
f i r s t = f i r s t >s u
;
tbd ;
ount ;
delete
}
in t e s t a
p u n t a t o da p deve e s s e r e e l i m i n a t o
delete
trovato =
ount ;
else
true ;
p = p>s u ;
void
L i s t a : : Stampa ( )
PRe
p = f i r s t ;
onst
while
}
(p) {
out << p>e l << " " ;
p = p>s u
;
el )
onst
p = p>s u ;
return
trovato ;
File main. pp
void
void
void
void
l );
Lista l i s t a ;
do
swit
h (
) {
ase ' 1 ' :
Inseris
i ( lista );
break ;
ase ' 2 ' :
Ri
er
a ( l i s t a ) ;
break ;
ase ' 3 ' :
Elimina ( l i s t a ) ;
break ;
ase ' 4 ' :
Svuota ( l i s t a ) ;
break ;
ase ' 5 ' :
Stampa ( l i s t a ) ;
break ;
ase ' 6 ' :
NumeroElementi ( l i s t a ) ;
break ;
ase ' 7 ' :
CopiaLista ( l i s t a ) ;
break ;
ase ' 8 ' :
break ;
default :
out << " S
e l t a non v a l i d a . \ n" ;
break ;
}
} while (
!= ' 8 ' ) ;
return 0 ;
void
{
stampaMenu ( ) ;
i n >>
;
in . ignore ( ) ;
stampaMenu ( ) {
out << e n d l ;
out << " 1 . I n s e r i s
i " << e n d l ;
out << " 2 . R i
e r
a " << e n d l ;
out << " 3 . Elimina " << e n d l ;
out << " 4 . Svuota " << e n d l ;
out << " 5 . Stampa" << e n d l ;
out << " 6 . Numero Elementi " << e n d l ;
out << " 7 . Copia " << e n d l ;
out << " 8 . E s
i " << e n d l ;
out << e n d l ;
out << " S
e l t a : " ;
50
void I n s e r i s
i ( L i s t a&
int i ;
}
51
l) {
void R i
e r
a ( L i s t a&
int i ;
l) {
if
else
l) {
void
}
Svuota ( L i s t a& l ) {
out << " Svuotamento l i s t a . " << e n d l ;
l . Svuota ( ) ;
void
}
Stampa ( L i s t a& l ) {
out << "Stampa : \ n" ;
l . Stampa ( ) ;
out << e n d l ;
void
}
NumeroElementi ( L i s t a& l ) {
out << "Numero Elementi : " << l . NumeroElementi ( ) << e n d l ;
void
C o p i a L i s t a ( L i s t a& l ) {
Lista l
opia ( l ) ;
Elimina()
an
ora pi sinteti
a. Tale variante, a dierenza dell'implementazione pre
edente, non dis
rimina il
aso in
ui l'elemento da eliminare sia posizionato in
testa alla struttura, ma tratta tutti i
asi in maniera omogenea. Per ottenere
questo, su
iente utilizzare un puntatore a
vedi Figura SL.1).
Inizialmente il puntatore
pp
PRe
(puntatore a puntatore
PRe *
52
pp
first
el
count
succ
el
succ
el
succ
Lista
Figura SL.1: Un puntatore s
orre la lista puntando ai puntatori
ontenuti
in essa.
su
dell'elemento di testa
void
if
onst
L i s t a : : Elimina (
TElem\& e l ) {
( f i r s t ) { // l a l i s t a non vuota
PRe
pp = \& f i r s t ; // i n d i r i z z o d e l l a v a r i a b i l e f i r s t
trovato =
;
( ( pp ) \&\& ( ! t r o v a t o ) ) {
( ( pp)> e l == e l ) {
PRe
tbd = pp ;
pp = ( pp)>s u
;
tbd ;
bool
while
if
false
delete
trovato =
ount ;
else
true ;
pp = \&(( pp)>s u ) ;
onst
TElem L i s t a : : Somma( )
{
PRe
p = f i r s t ;
TElem somma = 0 ;
(p) {
somma = somma + p>e l ;
p=p>s u
;
}
while
return
somma ;
53
TElem e l ;
PRe
s u
;
lass L i s t a {
private :
PRe
f i r s t ;
int nelem ;
PRe
getPuntCoda ( )
onst ;
};
void I n s e r i s
i ( TElem e l ) ;
void Svuota ( ) ;
void Stampa ( )
onst ;
int Count ( )
onst ;
bool SpostaTestaInCoda ( ) ;
bool CodaPari ( )
onst ;
L i s t a : : L i s t a ( ) : f i r s t ( 0 ) , nelem ( 0 ) {
}
Lista ::~ Lista () {
Svuota ( ) ;
}
void
L i s t a : : I n s e r i s
i ( TElem e l ) {
// I n s e r i m e n t o i n t e s t a
PRe
p =
Re
ord ;
p>e l = e l ;
p>s u
= f i r s t ;
first = p;
new
nelem++;
void L i s t a : : Svuota ( ) {
PRe
tbd ; // t o be d e l e t e d
while ( f i r s t != 0) {
tbd = f i r s t ;
f i r s t = f i r s t >s u
;
}
}
delete
tbd ;
nelem = 0 ;
54
onst
PRe
L i s t a : : getPuntCoda ( )
{
// R e s t i t u i s
e i l p u n t a t o r e a l l a
oda d e l l a l i s t a
( f i r s t ) { // non e s i s t e una
oda s e l a l i s t a vuota ;
PRe
p = f i r s t ;
( p>s u
)
p = p>s u
;
if
while
return
else
return
}
}
p;
0;
bool L i s t a : : SpostaTestaInCoda ( ) {
PRe
p = getPuntCoda ( ) ; // r e s t i t u i s
e
if ( p ) {
}
}
p>s u
= f i r s t ;
f i r s t = f i r s t >s u
;
p>su
>s u
= 0 ;
return
( p != 0 ) ; // s e p non 0 , l o spostamento s t a t o e f f e t t u a t o
void
stampa_menu( ) {
out << " 1 : I n s e r i s
i . \ n" ;
out << " 2 : Svuota . \ n" ;
out << " 3 : Stampa . \ n" ;
out << " 4 : Count . \ n" ;
out << " 5 : SpostaTestaInCoda . \ n" ;
out << " 6 : CodaPari . \ n" ;
out << " 7 : E s
i . \ n" ;
void
void
I n s e r i s
i ( L i s t a& l ) ;
Svuota ( L i s t a& l ) ;
a l l a oda , s e ' .
l );
Lista l ;
int s
e l t a ;
do {
stampa_menu ( ) ;
i n >> s
e l t a ;
( s
elta ) {
1:
Inseris
i ( l );
;
2:
Svuota ( l ) ;
;
3:
Stampa ( l ) ;
;
4:
Count ( l ) ;
;
5:
SpostaTestaInCoda ( l ) ;
;
6:
CodaPari ( l ) ;
;
7:
;
:
out << " S
e l t a non v a l i d a . \ n" ;
;
}
}
( s
e l t a != 7 ) ;
swit
h
ase
break
ase
break
ase
break
ase
break
ase
break
ase
break
ase
break
default
break
while
return 0 ;
void
}
I n s e r i s
i ( L i s t a& l ) {
TElem e l ;
out << " I n s e r i s
i v a l o r e elemento : " ;
i n >> e l ;
l . Inseris
i ( el );
void
}
Svuota ( L i s t a& l ) {
l . Svuota ( ) ;
out << " L i s t a s v u o t a t a . \ n" ;
void
}
Stampa ( L i s t a& l ) {
out << " I l
o n t e n u t o d e l l a l i s t a e ' : " ;
l . Stampa ( ) ;
out << e n d l ;
void
Count ( L i s t a& l ) {
55
56
{
i n
oda e ' p a r i . \ n" ;
void
if
onst
while ( p ) {
if ( p>e l < min )
min = p>e l ;
if ( p>e l > max)
}
max = p>e l ;
p = p>s u
;
SL.5 ListaStati
a
Tra
ia a pag. 14
};
void I n s e r i s
i I n C o d a ( TElem
void Svuota ( ) ;
void Stampa ( )
onst ;
int Count ( )
onst ;
el );
L i s t a : : L i s t a ( ) : nelem ( 0 ) {
}
Lista ::~ Lista () {
// Qui non n e
e s s a r i a al
una o p e r a z i o n e
// I l d i s t r u t t o r e p o t e v a an
he e s s e r e omesso d e l t u t t o .
}
void
if
}
L i s t a : : I n s e r i s
i I n C o d a ( TElem e l ) {
( nelem < NMAX) {
v [ nelem = e l ;
nelem++;
void
}
L i s t a : : Svuota ( ) {
nelem = 0 ;
i ++)
void
stampa_menu( ) {
out << " 1 : I n s e r i s
i I n C o d a . \ n" ;
out << " 2 : Svuota . \ n" ;
out << " 3 : Stampa . \ n" ;
out << " 4 : Count . \ n" ;
out << " 5 : E s
i . \ n" ;
void I n s e r i s
i I n C o d a ( L i s t a&
void Svuota ( L i s t a& l ) ;
void Stampa ( L i s t a& l ) ;
void Count ( L i s t a& l ) ;
int main ( )
{
Lista l ;
int s
e l t a ;
do {
stampa_menu ( ) ;
i n >> s
e l t a ;
( s
elta ) {
1:
Inseris
iInCoda ( l ) ;
;
swit
h
ase
break
l );
57
ase 2 :
Svuota ( l ) ;
break ;
ase 3 :
Stampa ( l ) ;
break ;
ase 4 :
Count ( l ) ;
break ;
ase 5 :
break ;
default :
out << " S
e l t a non
break ;
}
} while ( s
e l t a != 5 ) ;
return 0 ;
58
v a l i d a . \ n" ;
void
}
I n s e r i s
i I n C o d a ( L i s t a& l ) {
TElem e l ;
out << " I n s e r i s
i v a l o r e elemento : " ;
i n >> e l ;
l . Inseris
iInCoda ( el ) ;
void
}
Svuota ( L i s t a& l ) {
l . Svuota ( ) ;
out << " L i s t a s v u o t a t a . \ n" ;
void
}
Stampa ( L i s t a& l ) {
out << " I l
o n t e n u t o d e l l a l i s t a e ' : " ;
l . Stampa ( ) ;
out << e n d l ;
void
}
Count ( L i s t a& l ) {
out << "N. Elem : " << l . Count ( ) << e n d l ;
SL.6 Ordinata
Tra
ia a pag. 14
i ++;
return true ;
s e t r o v a un ' i n v e r s i o n e
59
nelem = 5
nelem = 8
EliminaTutti()
nelem-1,
j:
va da 0 a
EliminaTutti().
i n u t i l e r i
o p i a r e s e i == j )
v [ j = v [ i ; // l o r i
o p i o n e l v e t t o r e r i p u l i t o
j ++; // i l v e t t o r e r i p u l i t o ha ora un elemento i n p i
return
60
nel memorizzare in un membro privato della lista il valore della
oda. Tale valore deve essere
ostantemente aggiornato, a
ura di tutti i metodi
he
possono potenzialmente alterarlo: inserimento, eliminazione, svuotamento. . .
SommaCoda() nis e per alterare il valore delmostrano le implementazioni dei metodi Inseris i()
SommaCoda(),
lass Lista {
private:
...
TElem valoreCoda;
...
};
void
L i s t a : : I n s e r i s
i ( TElem e l ) {
// I n s e r i m e n t o i n t e s t a
PRe
p =
Re
ord ;
p>e l = e l ;
p>s u
= f i r s t ;
first = p;
nelem++;
new
if
void
if
L i s t a : : SommaCoda ( ) {
( first ) {
// Se l a l i s t a non vuota , l a v a r i a b i l e membro
o n t i e n e un v a l o r e
o r r e t t o .
//Lo sommo a t u t t i g l i e l e m e n t i .
PRe
p = f i r s t ;
(p) {
p>e l = p>e l + valoreCoda ;
p = p>s u
;
}
while
getPuntCoda()
61
// Metodo p r i v a t o
onst
PRe
L i s t a : : getPuntCoda ( )
{
// R e s t i t u i s
e i l p u n t a t o r e a l l a
oda d e l l a l i s t a
( f i r s t ) { // non e s i s t e una
oda s e l a l i s t a vuota ;
PRe
p = f i r s t ;
( p>s u
)
p = p>s u
;
if
while
return
else
return
}
}
p;
0;
// Metodo p u b b l i o
bool L i s t a : : SpostaTestaInCoda ( ) {
PRe
p = getPuntCoda ( ) ; // r e s t i t u i s
e
if ( p ) {
}
}
p>s u
= f i r s t ;
f i r s t = f i r s t >s u
;
p>su
>s u
= 0 ;
return
( p != 0 ) ; // s e p non 0 , l o spostamento s t a t o e f f e t t u a t o
unsigned int
int n = 0 ;
if ( f i r s t )
Lista : : EliminaElementiPostoDispari () {
{
PRe
p = f i r s t ; // p punta a l primo elemento ( d i i n d i
e 0 , q u i n d i p a r i )
while
delete
}
}
return
n;
unsigned int
Lista : : EliminaElementiPostoPari () {
62
int n = 0 ;
if ( f i r s t )
{ // e s
i s u b i t o s e l a l i s t a vuota . . .
// . . . a l t r i m e n t i
a n
e l l a s u b i t o i l primo elemento ( i n d i
e 0)
PRe
tbd = f i r s t ;
f i r s t = f i r s t >s u
;
tbd ;
n++;
delete
}
}
// p o i
h f i r s t s t a t o s p o s t a t o i n a v a n t i d i un elemento non mi r e s t a
he
// e l i m i n a r e t u t t i g l i e l e m e n t i d i p o s t o d i s p a r i d e l l ' a t t u a l e l i s t a .
n = n + EliminaElementiPostoDispari ( ) ;
return
n;
TElem e l ;
PRe
p r e
;
PRe
s u
;
lass L i s t a
private :
PRe
f i r s t ;
PRe
l a s t ;
};
void I n s e r i s
i ( TElem e l ) ;
void Svuota ( ) ;
void StampaDiretta ( )
onst ;
void StampaInversa ( )
onst ;
void StampaAlternata ( )
onst ;
unsigned int Count ( )
onst ;
L i s t a : : L i s t a ( ) : f i r s t ( 0 ) , l a s t ( 0 ) , nelem ( 0 ) {
}
Lista ::~ Lista () {
63
Svuota ( ) ;
void
L i s t a : : I n s e r i s
i ( TElem e l ) {
// I n s e r i m e n t o i n
oda
PRe
p =
Re
ord ;
p>e l = e l ;
p>s u
= 0 ;
p>p r e
= l a s t ;
( last )
l a s t >s u
= p ;
last = p;
new
if
if
}
(! first )
first = p;
nelem++;
void L i s t a : : Svuota ( ) {
PRe
tbd ; // t o be d e l e t e d
while ( f i r s t != 0) {
tbd = f i r s t ;
f i r s t = f i r s t >s u
;
tbd ;
delete
}
nelem = 0 ;
last = 0;
void
L i s t a : : StampaAlternata ( )
PRe
p = f i r s t ;
PRe
q = l a s t ;
done =
;
bool
false
while ( ( p ) && ! done )
out << p>e l << "
if ( q != p )
onst
{
";
if
true
p = p>s u ;
64
q = q>p r e ;
void
stampa_menu( ) {
out << " 1 : I n s e r i s
i . \ n" ;
out << " 2 : Svuota . \ n" ;
out << " 3 : Stampa D i r e t t a . \ n" ;
out << " 4 : Stampa I n v e r s a . \ n" ;
out << " 5 : Stampa A l t e r n a t a . \ n" ;
out << " 6 : Count . \ n" ;
out << " 7 : E s
i . \ n" ;
void I n s e r i s
i ( L i s t a& l ) ;
void Svuota ( L i s t a& l ) ;
void StampaDiretta ( L i s t a& l ) ;
void StampaInversa ( L i s t a& l ) ;
void StampaAlternata ( L i s t a& l ) ;
void Count ( L i s t a& l ) ;
int main ( )
{
Lista l ;
int s
e l t a ;
do {
stampa_menu ( ) ;
i n >> s
e l t a ;
( s
elta ) {
1:
Inseris
i ( l );
;
2:
Svuota ( l ) ;
;
3:
StampaDiretta ( l ) ;
;
4:
StampaInversa ( l ) ;
;
5:
StampaAlternata ( l ) ;
;
6:
Count ( l ) ;
;
7:
;
:
out << " S
e l t a non v a l i d a . \ n" ;
;
}
}
( s
e l t a != 7 ) ;
swit
h
ase
break
ase
break
ase
break
ase
break
ase
break
ase
break
ase
break
default
break
while
return 0 ;
65
void
}
I n s e r i s
i ( L i s t a& l ) {
TElem e l ;
out << " I n s e r i s
i v a l o r e elemento : " ;
i n >> e l ;
l . Inseris
i ( el );
void
}
Svuota ( L i s t a& l ) {
l . Svuota ( ) ;
out << " L i s t a s v u o t a t a . \ n" ;
void
}
StampaDiretta ( L i s t a& l ) {
out << "Stampa D i r e t t a : " ;
l . StampaDiretta ( ) ;
out << e n d l ;
void
}
StampaInversa ( L i s t a& l ) {
out << "Stampa I n v e r s a : " ;
l . StampaInversa ( ) ;
out << e n d l ;
void
}
StampaAlternata ( L i s t a& l ) {
out << "Stampa A l t e r n a t a : " ;
l . StampaAlternata ( ) ;
out << e n d l ;
void
}
Count ( L i s t a& l ) {
out << " I l numero d i e l e m e n t i
o n t e n u t i n e l l a l i s t a e ' : " <<
l . Count ( ) << e n d l ;
SL.12 Ribalta
Tra
ia a pag. 17
L'appro
io in generale pi e
iente per ribaltare la lista
onsiste nel modi
are la
ongurazione di tutti i puntatori
ontenuti nella struttura, senza
pertanto eettuare spostamenti si
i di elementi.
Appro
io iterativo
Si
onsideri la Figura SL.3(a), in
ui riportata la lista di partenza.
Per
el2 )
first
el4 )
el3 )
passi a
passi a puntare
( he punta ad
el1 )
dovr
first
el1
66
el2
eln
eli-1
eli
p1
p2
eli+1
eli-1
eli
eli+1
p1
p2
temp
first
el1
el2
eln
eln .
67
// Versione i t e r a t i v a d e l metodo R i b a l t a ( )
// Metodo P u b b l i
o
Lista : : Ribalta () {
( f i r s t && f i r s t >s u
) { // s e l a l i s t a
o n t i e n e almeno 2 e l e m e n t i
PRe
p1 = f i r s t ; // memorizzo i n p1 i l primo
PRe
p2 = p1>s u
; // memorizzo i n p2 i l se
ondo
p1>s u
= 0 ; // p1 , d i v e n t a n d o l ' u l t i m o elemento , deve puntare a z e r o
void
if
while
p1 = p2 ; // p1 d i v e n t a p2 ;
p2 = temp ; // p2 d i v e n t a temp
// i n q u e s t o punto d e l
o d i
e p1 e p2 puntano a g l i u l t i m i
// due e l e m e n t i d e l l a l i s t a .
p2>s u
= p1 ; // i l s u
e s s i v o d i p2 ora p1
f i r s t = p2 ; // p2 ora l a nuova t e s t a
Appro
io ri
orsivo
Il ribaltamento della lista pu essere appro
iato
ome un problema ri
orsivo.
Infatti, avendo una lista, la sua versione ribaltata si ottiene isolando il primo
elemento, ribaltando la restante parte della lista, e posponendo a questa
l'elemento isolato.
// Versione r i
o r s i v a d e l metodo R i b a l t a ( )
// Metodo p r i v a t o
PRe
L i s t a : : _ r i b a l t a ( PRe
p ) {
( ( ! p ) | | ( ! p>s u
) ) // s e l a l i s t a formata da 0 o 1 e l e m e n t i non f a
i o n i e n t e
p;
if
return
else
{
// memorizzo i n v e
h i a _ t e s t a l a v e
h i a t e s t a
PRe
v e
h i a _ t e s t a = p ;
// memorizzo i n ve
hio_se
ondo i l v e
h i o se
ondo elemento
PRe
ve
hio_se
ondo = p>s u
;
// r i b a l t o l a s o t t o l i s t a
on t e s t a i n ve
hio_se
ondo . . .
// . . . e memorizzo i n nuova_testa l a nuova t e s t a .
PRe
nuova_testa = _ r i b a l t a ( ve
hio_se
ondo ) ;
return
nuova_testa ; // r e s t i t u i s o l a nuova t e s t a
// Metodo p u b b l i
o
Lista : : Ribalta () {
f i r s t = _ribalta ( f i r s t ) ;
}
void
68
Capitolo SA
Soluzioni degli eser
izi su alberi
binari
SA.1 Albero Binario
Tra
ia a pag. 18
File AlberoBinario.h
#ifndef
#define
/
_ALBEROBINARIO_H_
_ALBEROBINARIO_H_
C l a s s e Albero Binario .
La
l a s s e implementa i metodi d i a g g i u n t a , e l i m i n a z i o n e e r i
e r
a d i un
elemento . I n o l t r e p e r m e t t e l a v i s i t a i n ordine , p r e o r d i n e e p o s t o r d i n e . E '
i n f i n e implementato i l
o s t r u t t o r e d i
o p i a .
L ' implementazione d i t u t t i i metodi b a s a t a su a p p o s i t i metodi r i
o r s i v i .
interi
PNodo r o o t ; // r a d i e d e l l ' a l b e r o
// Metodi r i
o r s i v i d i s u p p o r t o
_CopiaAlbero ( PNodo& dest ,
PNodo& s o u r
e ) ;
_Svuota (
PNodo& n ) ;
_AggiungiElem ( PNodo& n ,
TElem& e l ) ;
_ S o s t i t u i s
e ( PNodo& n , PNodo& p ) ;
_InAlb (
PNodo& n ,
TElem& e l )
;
_Elimina ( PNodo& n ,
TElem& e l ) ;
_PreOrdine (
PNodo& n )
;
_PostOrdine (
PNodo& n )
;
void
void
void
void
bool
void
void
void
onst
onst
onst
onst
onst
onst
onst
onst
onst
onst
69
onst
70
};
void
bool
void
void
void
void
void
#endif
onst
onst
onst
onst
onst
onst
di opia
AggiungiElem (
TElem& e l ) ;
InAlb (
TElem& e l )
;
Elimina (
TElem& e l ) ;
Svuota ( ) ;
PreOrdine ( )
;
PostOrdine ( )
;
InOrdine ( )
;
onst
/ _ALBEROBINARIO_H_ /
File AlberoBinario. pp
TElem e l ;
PNodo s i n ;
PNodo des ;
onst
AlberoBinario : : AlberoBinario (
AlberoBinario& a ) {
(
!= &a ) //
o p i a s o l o da un o g g e t t o d i f f e r e n t e
_CopiaAlbero ( root , a . r o o t ) ;
}
if this
AlberoBinario : :~ AlberoBinario () {
Svuota ( ) ;
}
// Metodi p r i v a t i r i o r s i v i d i s u p p o r t o
void
onst
if
new
//Ora b i s o g n a r i
r e a r e i l s o t t o a l b e r o s i n i s t r o ed i l s o t t o a l b e r o d e s t r o
// d e l nodo p u n t a t o da d e s t r i
o p i a n d o i r i s p e t t i v i s o t t o a l b e r i p u n t a t i da
// s o u r
e . R i f l e t t e n d o
i , l ' o p e r a z i o n e d e s i d e r a t a d e l t u t t o analoga a q u e l l a
// g i i n v o
a t a a p a r t i r e d a l l a r a d i
e . E ' q u i n d i p o s s i b i l e s f r u t t a r e l a
// r i
o r s i o n e ed i n v o
a r e l o s t e s s o " s e r v i z i o " _CopiaAlbero ( )
o n s i d e r a n d o
// sour
e >des e sour
e >s i n
ome r a d i
i d i due d i s t i n t i a l b e r i .
else
dest = 0;
71
void
if
}
new
else
if ( e l > n>e l )
_AggiungiElem ( n>des ,
else
el );
_AggiungiElem ( n>s i n , e l ) ;
bool
if
if
onst
A l b e r o B i n a r i o : : _AggiungiElem ( PNodo& n ,
TElem& e l ) {
( ! n) {
n =
Nodo ; // s i
rea un nuovo elemento d e l l ' a l b e r o . . .
n>e l = e l ; // . . . e l o s i i n i z i a l i z z a .
n>s i n = 0 ;
n>des = 0 ;
A l b e r o B i n a r i o : : _InAlb (
( ! n)
;
return false
onst
PNodo& n ,
onst
TElem& e l )
onst
return true ;
void
//
//
//
//
//
//
//
A l b e r o B i n a r i o : : _ S o s t i t u i s
e ( PNodo& n , PNodo& p ) {
Questo metodo ha
ome parametri d i i n g r e s s o u s
i t a :
n : un p u n t a t o r e a l l a r a d i
e d i un a l b e r o ;
p : un p u n t a t o r e ad un nodo .
I l suo e f f e t t o q u e l l o d i s o s t i t u i r e i l nodo p u n t a t o da p
on i l massimo
elemento d e l l ' a l b e r o n . La prima v o l t a q u e s t o metodo v i e n e i n v o
a t o ( n e l metodo
e l i m i n a )
on l a s i n t a s s i _ S o s t i s u i s
i ( n>sin , n ) , p e r
u i s i p r o v v e d e a l l a
s o s t i t u z i o n e d e l nodo n
on i l massimo d e l suo s o t t o a l b e r o s i n i s t r o .
PNodo q ;
( ! n>des ) {
q = n;
n = n>s i n ;
q>s i n=p>s i n ;
q>des=p>des ;
p=q ;
}
_ S o s t i t u i s
e ( n>des , p ) ;
if
else
void
if
onst
A l b e r o B i n a r i o : : _Elimina ( PNodo& n ,
TElem& e l ) {
( n ) { // Eliminare da un a l b e r o vuoto non produ
e al
una o p e r a z i o n e
( n>e l == e l ) {
//
a n
e l l a nodo
o r r e n t e
PNodo p = n ;
( ! n>s i n ) // i l ramo s i n i s t r o d e l nodo da e l i m i n a r e vuoto ?
n = n>des ; // s o s t i t u z i o n e d e l nodo
o l suo s o t t o a l b e r o d e s t r o
if
if
else
if ( ! n>des ) // i l ramo d e s t r o d e l nodo da e l i m i n a r e vuoto ?
n = n>s i n ; // s o s t i t u z i o n e d e l nodo
o l suo s o t t o a l b e r o s i n i s t r o
else // i l nodo da e l i m i n a r e ha s i a i l s o t t o a l b e r o s x
he q u e l l o dx
_ S o s t i t u i s
e ( n>s i n , n ) ; //
hiamo i l " s e r v i z i o " S o s t i t u i s
e ( )
delete p ;
else
if ( e l < n>e l )
_Elimina ( n>s i n ,
else
}
void
if
}
void
if
}
void
if
}
void
if
}
72
el );
_Elimina ( n>des , e l ) ;
onst
A l b e r o B i n a r i o : : _Svuota (
PNodo& n ) {
( n ) { // Agis
e s o l o s e l ' a l b e r o e s i s t e
_Svuota ( n>s i n ) ;
_Svuota ( n>des ) ;
n;
delete
A l b e r o B i n a r i o : : _PreOrdine (
(n) {
out << n>e l << " " ;
_PreOrdine ( n>s i n ) ;
_PreOrdine ( n>des ) ;
onst
A l b e r o B i n a r i o : : _PostOrdine (
(n) {
_PostOrdine ( n>s i n ) ;
_PostOrdine ( n>des ) ;
out << n>e l << " " ;
A l b e r o B i n a r i o : : _InOrdine (
(n) {
_InOrdine ( n>s i n ) ;
out << n>e l << " " ;
_InOrdine ( n>des ) ;
onst
onst
onst
PNodo& n )
PNodo& n )
PNodo& n )
onst
onst
// Metodi p u b b l i i
void
}
A l b e r o B i n a r i o : : AggiungiElem (
_AggiungiElem ( root , e l ) ;
onst
TElem& e l ) {
TElem& e l )
onst
void
}
A l b e r o B i n a r i o : : Elimina (
_Elimina ( root , e l ) ;
onst
TElem& e l ) {
void
}
A l b e r o B i n a r i o : : Svuota ( ) {
_Svuota ( r o o t ) ;
root = 0;
void
}
A l b e r o B i n a r i o : : PreOrdine ( )
_PreOrdine ( r o o t ) ;
onst
void
}
A l b e r o B i n a r i o : : PostOrdine ( )
_PostOrdine ( r o o t ) ;
void
}
A l b e r o B i n a r i o : : InOrdine ( )
_InOrdine ( r o o t ) ;
onst
onst
73
File main. pp
#in
lude
#in
lude
<i o s t r e a m . h>
" A l b e r o B i n a r i o . h"
// P r o t o t i p i d i f u n z i o n i d i s u p p o r t o per l a v e r i f i
a d e l
o r r e t t o funzionamento
// d e i metodi d e l l a
l a s s e A l b e r o B i n a r i o .
stampaMenu ( ) ;
I n s e r i s
i ( AlberoBinario& a ) ;
Ri
er
a ( AlberoBinario& a ) ;
Elimina ( A l b e r o B i n a r i o & a ) ;
Svuota ( A l b e r o B i n a r i o & a ) ;
PreOrdine ( A l b e r o B i n a r i o & a ) ;
InOrdine ( A l b e r o B i n a r i o & a ) ;
PostOrdine ( A l b e r o B i n a r i o & a ) ;
Copia ( A l b e r o B i n a r i o & a ) ;
void
void
void
void
void
void
void
void
void
int main ( )
har
;
AlberoBinario albero ;
do
{
stampaMenu ( ) ;
i n >>
;
in . ignore ( ) ;
swit
h (
) {
ase ' 1 ' :
I n s e r i s
i ( albero ) ;
break ;
ase ' 2 ' :
Ri
er
a ( albero ) ;
break ;
ase ' 3 ' :
Elimina ( a l b e r o ) ;
break ;
ase ' 4 ' :
Svuota ( a l b e r o ) ;
break ;
ase ' 5 ' :
PreOrdine ( a l b e r o ) ;
break ;
ase ' 6 ' :
InOrdine ( a l b e r o ) ;
break ;
ase ' 7 ' :
PostOrdine ( a l b e r o ) ;
break ;
ase ' 8 ' :
Copia ( a l b e r o ) ;
break ;
ase ' 9 ' :
break ;
default :
out << " S
e l t a
break ;
}
} while (
!= ' 9 ' ) ;
return 0 ;
non v a l i d a . \ n" ;
void
stampaMenu ( ) {
out << e n d l ;
out << " 1 . I n s e r i s
i " << e n d l ;
out << " 2 . R i
e r
a " << e n d l ;
out << " 3 . Elimina " << e n d l ;
out << " 4 . Svuota " << e n d l ;
out << " 5 . Pre o r d i n e " << e n d l ;
out << " 6 . InOrdine " << e n d l ;
out << " 7 . Post Ordine " << e n d l ;
out << " 8 . Copia a l b e r o " << e n d l ;
out << " 9 . E s
i " << e n d l ;
out << e n d l ;
out << " S
e l t a : " ;
void I n s e r i s
i ( A l b e r o B i n a r i o & a )
int i ;
}
void R i
e r
a ( A l b e r o B i n a r i o & a )
int i ;
if
else
void
}
Svuota ( A l b e r o B i n a r i o & a ) {
out << " Svuotamento a l b e r o . " << e n d l ;
a . Svuota ( ) ;
void
}
PreOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n pre o r d i n e : \ n" ;
a . PreOrdine ( ) ;
out << e n d l ;
void
InOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n o r d i n e : \ n" ;
a . InOrdine ( ) ;
74
75
out << e n d l ;
void
}
PostOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n post o r d i n e : \ n" ;
a . PostOrdine ( ) ;
out << e n d l ;
void
}
Copia ( A l b e r o B i n a r i o & a ) {
AlberoBinario b( a ) ;
out << "La v i s i t a i n o r d i n e d e l l ' a l b e r o
o p i a t o e ' : " ;
b . InOrdine ( ) ;
out << e n d l ;
PNodo& n )
onst
return
else
return
num ;
0;
// Metodo p u b b l i o
SA.3 O
orrenze
Tra
ia a pag. 19
// Metodo p r i v a t o
76
PNodo& n ,
onst
TElem& e l )
onst
o ++;
if
else
}
}
o = o + _O orrenze ( n>s i n , e l ) ;
return
o ;
// Metodo p u b b l i o
TElem& e l )
onst
File AlberoBinario.h
#ifndef _ALBEROBINARIO_H_
#define _ALBEROBINARIO_H_
stru
t Nodo ;
typedef Nodo PNodo ;
typedef int TElem ;
lass A l b e r o B i n a r i o {
private :
PNodo r o o t ;
int maxo
;
// Metodi r i
o r s i v i d i s u p p o r t o
_Svuota (
PNodo& n ) ;
_ I n s e r i s
i ( PNodo& n ,
TElem& e l ,
_PreOrdine (
PNodo& n )
;
_PostOrdine (
PNodo& n )
;
_InOrdine (
PNodo& n )
;
void
bool
void
void
void
onst
onst
int
urr_o
) ;
onst
onst
onst
onst
onst
onst
A l b e r o B i n a r i o (
onst A l b e r o B i n a r i o &); // i m p e d i s
e l a
o p i a . . .
A l b e r o B i n a r i o & operator =(
onst A l b e r o B i n a r i o &); // . . . e l ' a s s e g n a z i o n e .
publi
:
A l b e r o B i n a r i o ( unsigned int max_o
) ;
~AlberoBinario ( ) ;
};
bool
void
void
void
void
#endif
onst TElem&
onst ;
onst ;
onst ;
Inseris
i (
Svuota ( ) ;
PreOrdine ( )
PostOrdine ( )
InOrdine ( )
77
el );
/ _ALBEROBINARIO_H_ /
File AlberoBinario. pp
TElem e l ;
PNodo s i n ;
PNodo des ;
AlberoBinario : : AlberoBinario (
}
unsigned int
AlberoBinario : :~ AlberoBinario () {
Svuota ( ) ;
}
// Metodi p r i v a t i r i o r s i v i d i s u p p o r t o
bool
onst
int
A l b e r o B i n a r i o : : _ I n s e r i s
i ( PNodo& n ,
TElem& e l ,
urr_o
) {
// Questa l a f u n z i o n e r i
o r s i v a
he s i o
upa d e l l ' i n s e r i m e n t o n e l l ' a l b e r o
// d e l l ' elemento s p e
i f i
a t o i n e l .
//
// Essa s i basa s u l l ' i p o t e s i
he , durante l ' i n s e r i m e n t o d i un elemento i n un
// a l b e r o b i n a r i o o r d i n a t o , b i s o g n a n e
e s s a r i a m e n t e " p a s s a r e " per g l i e v e n t u a l i
// a l t r i nodi
o n t e n e n t i l o s t e s s o v a l o r e . E ' p o s s i b i l e dunque d i s
e n d e r e l ' a l b e r o
// i n
e r
a d e l l a p o s i z i o n e i n
u i a g g i u n g e r e l ' elemento e ,
ontemporaneamente ,
//
o n t a r e l ' o
o r r e n z a d e l l e e v e n t u a l i r e p l i
h e , interrompendo prematuramente
// l ' i n s e r i m e n t o i n
aso d i r a g g i u n g i m e n t o d e l numero massimo d i o
o r r e n z e .
if
new
return true
else {
if ( e l == n>e l ) { // s e l ' elemento
o r r e n t e p a r i ad e l . . .
urr_o
++; // . . . in
remento
urr_o
. . .
if (
urr_o
>= maxo
) // . . . e s e ha r a g g i u n t o i l l i m i t e . . .
return false ; // . . . e s
o
on i l v a l o r e f a l s e .
}
// Se sono q u i i l l i m i t e non s t a t o r a g g i u n t o .
( e l > n>e l )
_ I n s e r i s
i ( n>des , e l ,
urr_o
) ;
if
return
else
return
_ I n s e r i s i ( n>s i n , e l , urr_o ) ;
78
void
if
}
void
if
}
void
if
}
void
if
}
onst
A l b e r o B i n a r i o : : _Svuota (
PNodo& n ) {
( n ) { // Agis
e s o l o s e l ' a l b e r o e s i s t e
_Svuota ( n>s i n ) ;
_Svuota ( n>des ) ;
n;
delete
A l b e r o B i n a r i o : : _PreOrdine (
(n) {
out << n>e l << " " ;
_PreOrdine ( n>s i n ) ;
_PreOrdine ( n>des ) ;
onst
A l b e r o B i n a r i o : : _PostOrdine (
(n) {
_PostOrdine ( n>s i n ) ;
_PostOrdine ( n>des ) ;
out << n>e l << " " ;
A l b e r o B i n a r i o : : _InOrdine (
(n) {
_InOrdine ( n>s i n ) ;
out << n>e l << " " ;
_InOrdine ( n>des ) ;
onst
onst
onst
PNodo& n )
PNodo& n )
PNodo& n )
bool A l b e r o B i n a r i o : : I n s e r i s
i (
onst
return _ I n s e r i s
i ( root , e l , 0 ) ;
TElem& e l ) {
void
}
A l b e r o B i n a r i o : : Svuota ( ) {
_Svuota ( r o o t ) ;
root = 0;
void
}
void
}
A l b e r o B i n a r i o : : PostOrdine ( )
_PostOrdine ( r o o t ) ;
void
}
onst
A l b e r o B i n a r i o : : PreOrdine ( )
_PreOrdine ( r o o t ) ;
A l b e r o B i n a r i o : : InOrdine ( )
_InOrdine ( r o o t ) ;
onst
onst
File main. pp
#in
lude
#in
lude
<i o s t r e a m>
" A l b e r o B i n a r i o . h"
onst
onst
// Metodi p u b b l i i
using namespa e
79
std ;
// P r o t o t i p i d i f u n z i o n i d i s u p p o r t o per l a v e r i f i
a d e l
o r r e t t o funzionamento
// d e i metodi d e l l a
l a s s e A l b e r o B i n a r i o .
stampaMenu ( ) ;
I n s e r i s
i ( AlberoBinario& a ) ;
Svuota ( A l b e r o B i n a r i o & a ) ;
PreOrdine ( A l b e r o B i n a r i o & a ) ;
InOrdine ( A l b e r o B i n a r i o & a ) ;
PostOrdine ( A l b e r o B i n a r i o & a ) ;
void
void
void
void
void
void
int main ( ) {
har
;
int maxo
;
do
swit
h (
) {
ase ' 1 ' :
I n s e r i s
i ( albero ) ;
break ;
ase ' 2 ' :
Svuota ( a l b e r o ) ;
break ;
ase ' 3 ' :
PreOrdine ( a l b e r o ) ;
break ;
ase ' 4 ' :
InOrdine ( a l b e r o ) ;
break ;
ase ' 5 ' :
PostOrdine ( a l b e r o ) ;
break ;
ase ' 6 ' :
break ;
default :
out << " S
e l t a non
break ;
}
} while (
!= ' 6 ' ) ;
return 0 ;
void
{
stampaMenu ( ) ;
i n >>
;
in . ignore ( ) ;
v a l i d a . \ n" ;
stampaMenu ( ) {
out << e n d l ;
out << " 1 . I n s e r i s
i " << e n d l ;
out << " 2 . Svuota " << e n d l ;
out << " 3 . Pre o r d i n e " << e n d l ;
out << " 4 . InOrdine " << e n d l ;
out << " 5 . Post Ordine " << e n d l ;
out << " 6 . E s
i " << e n d l ;
out << e n d l ;
out << " S
e l t a : " ;
void I n s e r i s
i ( A l b e r o B i n a r i o & a )
int i ;
80
if
else
void
}
Svuota ( A l b e r o B i n a r i o & a ) {
out << " Svuotamento a l b e r o . " << e n d l ;
a . Svuota ( ) ;
void
}
PreOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n pre o r d i n e : \ n" ;
a . PreOrdine ( ) ;
out << e n d l ;
void
}
InOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n o r d i n e : \ n" ;
a . InOrdine ( ) ;
out << e n d l ;
void
}
PostOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n post o r d i n e : \ n" ;
a . PostOrdine ( ) ;
out << e n d l ;
AlberoBinario : : AlberoBinario (
unsigned int
bool
if
A l b e r o B i n a r i o : : _ I n s e r i s i ( PNodo& n ,
_maxDepth ) : r o o t ( 0 ) ,
maxDepth ( _maxDepth ) {
onst
TElem& e l ,
if
new
return true ;
else
if ( e l > n>e l )
return _ I n s e r i s
i ( n>des ,
else
return _ I n s e r i s
i ( n>s i n ,
unsigned int
( _maxDepth > 0) {
( ! n) {
n =
Nodo ; // s i
rea un nuovo elemento d e l l ' a l b e r o . . .
n>e l = e l ; // . . . e l o s i i n i z i a l i z z a .
n>s i n = 0 ;
n>des = 0 ;
e l , _maxDepth 1 ) ;
e l , _maxDepth 1 ) ;
_maxDepth ) {
81
return false ;
// Metodo p u b b l i o
el ) {
SA.6 Somma
Tra
ia a pag. 21
// Metodo p r i v a t o
void
if
}
A l b e r o B i n a r i o : : _Somma(
( n && ( i != 0 ) ) {
n>e l += i ;
_Somma( n>s i n , i ) ;
_Somma( n>des , i ) ;
onst
PNodo& n ,
int
i) {
// Metodo P u b b l i o
void
}
A l b e r o B i n a r i o : : Somma(
_Somma( root , i ) ;
int
i) {
SA.7 Sostituis
i
Tra
ia a pag. 21
// Metodo p r i v a t o
i , TElem j ) {
i , j );
s o s t i t u z i o n i = s o s t i t u z i o n i + _ S o s t i t u i s i ( n>s i n , i , j ) ;
// . . . p o i n e l l a r a d i
e
( n>e l == i ) {
n>e l = j ;
s o s t i t u z i o n i ++;
}
if
}
}
return
sostituzioni ;
82
// Metodo p u b b l i o
i , TElem j ) {
File AlberoBinario.h
#ifndef _ALBEROBINARIO_H_
#define _ALBEROBINARIO_H_
stru
t Nodo ; // Forward d e
l a r a t i o n
typedef Nodo PNodo ;
typedef unsigned int TElem ; //L ' a l b e r o
lass A l b e r o B i n a r i o {
private :
ontiene i n t er i p o s i t i v i
PNodo r o o t ; // r a d i e d e l l ' a l b e r o
// Metodi r i o r s i v i d i s u p p o r t o
};
#endif
/ _ALBEROBINARIO_H_ /
File AlberoBinario. pp
83
TElem e l ;
PNodo s i n ;
PNodo des ;
// Metodi p r i v a t i r i o r s i v i d i s u p p o r t o
void
if
}
new
else
if ( e l > n>e l )
_ I n s e r i s
i ( n>des ,
else
void
if
}
void
if
}
void
if
}
el );
_ I n s e r i s i ( n>s i n , e l ) ;
onst
A l b e r o B i n a r i o : : _ I n s e r i s
i ( PNodo& n ,
TElem& e l ) {
( ! n) {
n =
Nodo ; // s i
rea un nuovo elemento d e l l ' a l b e r o . . .
n>e l = e l ; // . . . e l o s i i n i z i a l i z z a .
n>s i n = 0 ;
n>des = 0 ;
void
if
onst
A l b e r o B i n a r i o : : _Svuota (
PNodo& n ) {
( n ) { // Agis
e s o l o s e l ' a l b e r o non vuoto
_Svuota ( n>s i n ) ;
_Svuota ( n>des ) ;
n;
delete
A l b e r o B i n a r i o : : _PreOrdine (
(n) {
out << n>e l << " " ;
_PreOrdine ( n>s i n ) ;
_PreOrdine ( n>des ) ;
onst
A l b e r o B i n a r i o : : _PostOrdine (
(n) {
_PostOrdine ( n>s i n ) ;
_PostOrdine ( n>des ) ;
out << n>e l << " " ;
A l b e r o B i n a r i o : : _InOrdine (
(n) {
_InOrdine ( n>s i n ) ;
out << n>e l << " " ;
onst
onst
onst
PNodo& n )
PNodo& n )
PNodo& n )
onst
onst
84
_InOrdine ( n>des ) ;
onst
if
if
}
}
return
else
return
ount ;
0 ; //L ' a l b e r o vuoto .
if
return false
return
// Metodi p u b b l i
i
AlberoBinario : : I n s e r i s
i (
_ I n s e r i s
i ( root , e l ) ;
}
void
onst
TElem& e l ) {
void
}
A l b e r o B i n a r i o : : Svuota ( ) {
_Svuota ( r o o t ) ;
root = 0;
void
A l b e r o B i n a r i o : : PreOrdine ( )
_PreOrdine ( r o o t ) ;
onst
onst
85
void
}
A l b e r o B i n a r i o : : PostOrdine ( )
_PostOrdine ( r o o t ) ;
void
}
A l b e r o B i n a r i o : : InOrdine ( )
_InOrdine ( r o o t ) ;
bool
//
//
//
//
//
//
onst
onst
operator onst
onst
AlberoBinario : :
==(
AlberoBinario& rhs )
{
Questo o p e r a t o r e v i e n e i n v o
a t o
on l a s e g u e n t e s i n t a s s i :
a1 == a2 ;
dove a1 ed a2 sono due i s t a n z e d e l l a
l a s s e A l b e r o B i n a r i o .
In q u e s t o
aso v i e n e i n v o
a t o l ' o p e r a t o r ==() s u l l ' o g g e t t o a1 ,
mentre a2 , parametro a t t u a l e , v i e n e p a s s a t o per r i f e r i m e n t o
prendendo i l p o s t o d e l parametro f o r m a l e r h s .
return
File main. pp
void
void
void
void
void
void
void
void
int main ( )
{
// I s t a n z i o due a l b e r i
AlberoBinario albero1 , albero2 ;
// I s t a n z i o un p u n t a t o r e
he , i n i z i a l m e n t e , punta a l primo a l b e r o .
A l b e r o B i n a r i o InUso = &a l b e r o 1 ;
;
har
do {
//Comuni
o a l l ' u t a n t e q u a l e d e i due a l b e r i s t a u t i l i z z a n d o .
if ( InUso == &a l b e r o 1 )
out << "Ora s t a i l a v o r a n d o s u l l ' a l b e r o 1 . " << e n d l ;
else
out << "Ora s t a i l a v o r a n d o s u l l ' a l b e r o 2 . " << e n d l ;
stampaMenu ( ) ;
i n >>
;
in . ignore ( ) ;
swit
h
ase
(
) {
'1 ' :
break ;
ase ' 2 ' :
Svuota ( InUso ) ;
break ;
ase ' 3 ' :
ContaMinMax ( InUso ) ;
break ;
ase ' 4 ' :
PreOrdine ( InUso ) ;
break ;
ase ' 5 ' :
InOrdine ( InUso ) ;
break ;
ase ' 6 ' :
PostOrdine ( InUso ) ;
break ;
ase ' 7 ' :
Uguali ( a l b e r o 1 , a l b e r o 2 ) ;
break ;
ase '
' : // passo da un a l b e r o a l l ' a l t r o
if ( InUso == &a l b e r o 1 )
InUso = &a l b e r o 2 ;
else
InUso = &a l b e r o 1 ;
break ;
ase ' 8 ' :
break ;
default :
out << " S
e l t a non v a l i d a . \ n" ;
break ;
}
} while (
!= ' 8 ' ) ;
return 0 ;
void
stampaMenu ( ) {
out << e n d l ;
out << " 1 . I n s e r i s
i " << e n d l ;
out << " 2 . Svuota " << e n d l ;
out << " 3 . ContaMinMax " << e n d l ;
out << " 4 . Pre o r d i n e " << e n d l ;
out << " 5 . InOrdine " << e n d l ;
out << " 6 . Post Ordine " << e n d l ;
out << " 7 . Uguali " << e n d l ;
out << "
. Cambia Albero . " << e n d l ;
out << " 8 . E s
i " << e n d l ;
out << e n d l ;
out << " S
e l t a : " ;
void I n s e r i s
i ( A l b e r o B i n a r i o & a )
int i ;
}
void
Svuota ( A l b e r o B i n a r i o & a ) {
out << " Svuotamento a l b e r o . " << e n d l ;
86
87
a . Svuota ( ) ;
out << " Ci sono " << a . ContaMinMax ( Min , Max) << " e l e m e n t i t r a " <<
Min << " e " << Max << " . \ n" ;
void
}
PreOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n pre o r d i n e : \ n" ;
a . PreOrdine ( ) ;
out << e n d l ;
void
}
InOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n o r d i n e : \ n" ;
a . InOrdine ( ) ;
out << e n d l ;
void
}
PostOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n post o r d i n e : \ n" ;
a . PostOrdine ( ) ;
out << e n d l ;
//
' l a r a d i
e e . . .
// . . . o e s i s t e i l nodo d i s i n i s t r a e q u e s t o ha almeno un f i g l i o
// oppure e s i s t e i l nodo d i d e s t r a e q u e s t o ha almeno un f i g l i o .
// Tradotto i n
o d i
e s i ha :
r o o t && (
( root >s i n && ( root >s i n >s i n | | root >s i n >des ) ) | |
( root >des && ( root >des >s i n | | root >des >des ) )
);
88
// Metodo p r i v a t o
A l b e r o B i n a r i o : : _ProfMaggioreDi (
PNodo& n ,
p)
{
( n ) { // s e l ' a l b e r o non vuoto . . .
( p == 0) // s e i l
o n t a t o r e ( s
e s o f i n o a ) z e r o . . .
; // . . . abbiamo s u p e r a t o l a p r o f . r i
h i e s t a . . .
// . . . a l t r i m e n t i b i s o g n a
o n t i n u a r e l a d i s
e s a n e i s o t t o a l b e r i de
rementando p .
( _ProfMaggioreDi ( n>s i n , p 1) | | _ProfMaggioreDi ( n>des , p 1 ) ) ;
}
// . . . a l t r i m e n t i f a l s o .
;
}
bool
if
if
return true
else
return
else
return false
// Metodo p u b b l i
o
A l b e r o B i n a r i o : : ProfMaggioreDi (
_ProfMaggioreDi ( root , p ) ;
}
bool
return
onst
unsigned int
unsigned int
p)
onst
onst
TElem& e l ,
bool&
foglia )
// . . . uso i l s e r v i z i o
he i o s t e s s o o f f r o : r i
o r s i o n e .
p = _Profondita ( n>des , e l , f o g l i a ) ;
else
p = _Profondita ( n>s i n , e l , f o g l i a ) ;
if
return
if
return
return
el ,
bool&
foglia )
onst
onst
89
// Metodo p r i v a t o
void
if
}
A l b e r o B i n a r i o : : _SommaLivello (
(n) {
n>e l += i ;
_SommaLivello ( n>s i n , i +1);
_SommaLivello ( n>des , i +1);
onst
PNodo& n ,
unsigned int
i) {
// Metodo p u b b l i o
void
}
A l b e r o B i n a r i o : : SommaLivello ( ) {
_SommaLivello ( root , 1 ) ;
File AlberoBinario.h
#ifndef _ALBEROBINARIO_H_
#define _ALBEROBINARIO_H_
stru
t Nodo ; // Forward d e
l a r a t i o n
typedef Nodo PNodo ;
typedef unsigned int TElem ; //L ' a l b e r o
lass A l b e r o B i n a r i o {
private :
PNodo r o o t ; // r a d i
e d e l l ' a l b e r o
unsigned int numelem ;
ontiene i n t er i p o s i t i v i
// Metodi r i o r s i v i d i s u p p o r t o
ostruttore
};
90
#endif
/ _ALBEROBINARIO_H_ /
File AlberoBinario. pp
TElem e l ;
PNodo s i n ;
PNodo des ;
A l b e r o B i n a r i o : : A l b e r o B i n a r i o ( ) : r o o t ( 0 ) , numelem ( 0 ) {
}
AlberoBinario : :~ AlberoBinario () {
Svuota ( ) ;
}
// Metodi p r i v a t i r i o r s i v i d i s u p p o r t o
void
if
}
new
else
if ( e l > n>e l )
_ I n s e r i s
i ( n>des ,
else
el );
_ I n s e r i s i ( n>s i n , e l ) ;
void
if
}
onst
A l b e r o B i n a r i o : : _ I n s e r i s
i ( PNodo& n ,
TElem& e l ) {
( ! n) {
n =
Nodo ; // s i
rea un nuovo elemento d e l l ' a l b e r o . . .
n>e l = e l ; // . . . e l o s i i n i z i a l i z z a .
n>s i n = 0 ;
n>des = 0 ;
bool
if
onst
A l b e r o B i n a r i o : : _Svuota (
PNodo& n ) {
( n ) { // Agis
e s o l o s e l ' a l b e r o non vuoto
_Svuota ( n>s i n ) ;
_Svuota ( n>des ) ;
n;
delete
onst
onst
onst
A l b e r o B i n a r i o : : _Cer
a (
PNodo& n ,
TElem& e l )
(n) {
( n>e l == e l ) // l ' elemento
e r
a t o n e l nodo
o r r e n t e ?
;
if
return true
}
}
91
inline bool
onst
A l b e r o B i n a r i o : : EUnaFoglia (
PNodo& n ) {
// metodo d i s u p p o r t o
he v e r i f i
a s e i l nodo
// p u n t a t o da n o meno una f o g l i a .
( ! n>s i n && ! n>des ) ;
}
return
bool
if
}
}
void
if
}
void
if
}
if
delete
return true
else
if
return
else
// r i p e t i l ' o p e r a z i o n e n e l s o t t o a l b .
return _EliminaFoglia ( n>s i n , e l ) ;
sinistro
return false ;
void
if
}
onst
A l b e r o B i n a r i o : : _EliminaFoglia ( PNodo& n ,
TElem& e l ) {
( n ) { // s e n punta ad un nodo ( e non a z e r o )
// s e l ' elemento p u n t a t o e l e i l nodo una f o g l i a
( ( n>e l == e l ) && EUnaFoglia ( n ) ) {
n ; // e l i m i n a l ' elemento
n = 0 ; // a z z e r a i l p u n t a t o r e
;
}
( e l > n>e l )
// r i p e t i l ' o p e r a z i o n e n e l s o t t o a l b . d e s t r o
_EliminaFoglia ( n>des , e l ) ;
A l b e r o B i n a r i o : : _PreOrdine (
(n) {
out << n>e l << " " ;
_PreOrdine ( n>s i n ) ;
_PreOrdine ( n>des ) ;
onst
A l b e r o B i n a r i o : : _PostOrdine (
(n) {
_PostOrdine ( n>s i n ) ;
_PostOrdine ( n>des ) ;
out << n>e l << " " ;
A l b e r o B i n a r i o : : _InOrdine (
(n) {
_InOrdine ( n>s i n ) ;
out << n>e l << " " ;
_InOrdine ( n>des ) ;
onst
PNodo& n )
onst
PNodo& n )
onst
PNodo& n )
void
AlberoBinario : : I n s e r i s
i (
_ I n s e r i s
i ( root , e l ) ;
onst
TElem& e l ) {
onst
onst
// Metodi p u b b l i i
92
numelem++;
void
}
A l b e r o B i n a r i o : : Svuota ( ) {
_Svuota ( r o o t ) ;
root = 0;
numelem = 0 ;
TElem& e l )
onst
bool
if
}
}
AlberoBinario : : EliminaFoglia(
( _EliminaFoglia ( root , e l ) ) {
numelem;
;
A l b e r o B i n a r i o : : PostOrdine ( )
_PostOrdine ( r o o t ) ;
void
}
onst
A l b e r o B i n a r i o : : PreOrdine ( )
_PreOrdine ( r o o t ) ;
void
}
TElem& e l ) {
return true
else
return false ;
void
}
onst
A l b e r o B i n a r i o : : InOrdine ( )
_InOrdine ( r o o t ) ;
onst
onst
File main. pp
void
void
void
void
void
void
void
void
void
int main ( ) {
AlberoBinario
har
;
do {
albero ;
swit
h (
) {
ase ' 1 ' :
I n s e r i s
i ( albero ) ;
break ;
ase ' 2 ' :
Svuota ( a l b e r o ) ;
break ;
ase ' 3 ' :
Cer
a ( a l b e r o ) ;
break ;
ase ' 4 ' :
EliminaFoglia( albero ) ;
break ;
ase ' 5 ' :
NumeroElementi ( a l b e r o ) ;
break ;
ase ' 6 ' :
PreOrdine ( a l b e r o ) ;
break ;
ase ' 7 ' :
InOrdine ( a l b e r o ) ;
break ;
ase ' 8 ' :
PostOrdine ( a l b e r o ) ;
break ;
ase ' 9 ' :
break ;
default :
out << " S
e l t a non v a l i d a . \ n" ;
break ;
}
} while (
!= ' 9 ' ) ;
return 0 ;
void
stampaMenu ( ) {
out << e n d l ;
out << " 1 . I n s e r i s
i " << e n d l ;
out << " 2 . Svuota " << e n d l ;
out << " 3 . Cer
a " << e n d l ;
out << " 4 . Elimina F o g l i a " << e n d l ;
out << " 5 . Numero Elementi " << e n d l ;
out << " 6 . Pre o r d i n e " << e n d l ;
out << " 7 . InOrdine " << e n d l ;
out << " 8 . Post Ordine " << e n d l ;
out << " 9 . E s
i " << e n d l ;
out << e n d l ;
out << " S
e l t a : " ;
void I n s e r i s
i ( A l b e r o B i n a r i o & a )
int i ;
}
93
94
void
}
Svuota ( A l b e r o B i n a r i o & a ) {
out << " Svuotamento a l b e r o . " << e n d l ;
a . Svuota ( ) ;
if ( a . Cer
a ( i ) )
out << " Elemento
else
t r o v a t o . \ n" ;
void E l i m i n a F o g l i a ( A l b e r o B i n a r i o & a )
int i ;
if
else
void
}
NumeroElementi ( A l b e r o B i n a r i o & a ) {
out << " I l numero d i e l e m e n t i e ' : " << a . NumElem ( ) << e n d l ;
void
}
PreOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n pre o r d i n e : \ n" ;
a . PreOrdine ( ) ;
out << e n d l ;
void
}
InOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n o r d i n e : \ n" ;
a . InOrdine ( ) ;
out << e n d l ;
void
}
PostOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n post o r d i n e : \ n" ;
a . PostOrdine ( ) ;
out << e n d l ;
File AlberoBinario.h
#ifndef _ALBEROBINARIO_H_
#define _ALBEROBINARIO_H_
stru
t Nodo ; // Forward d e
l a r a t i o n
typedef Nodo PNodo ;
typedef unsigned int TElem ; //L ' a l b e r o
lass A l b e r o B i n a r i o {
private :
PNodo r o o t ; // r a d i
e d e l l ' a l b e r o
unsigned int numelem ;
95
ontiene i n t er i p o s i t i v i
// Metodi r i o r s i v i d i s u p p o r t o
};
#endif
/ _ALBEROBINARIO_H_ /
File AlberoBinario. pp
TElem e l ;
PNodo s i n ;
PNodo des ;
A l b e r o B i n a r i o : : A l b e r o B i n a r i o ( ) : r o o t ( 0 ) , numelem ( 0 ) {
}
AlberoBinario : :~ AlberoBinario () {
Svuota ( ) ;
}
ostruttore
96
// Metodi p r i v a t i r i o r s i v i d i s u p p o r t o
void
if
}
onst
A l b e r o B i n a r i o : : _ I n s e r i s
i ( PNodo& n ,
TElem& e l ) {
( ! n) {
n =
Nodo ; // s i
rea un nuovo elemento d e l l ' a l b e r o . . .
n>e l = e l ; // . . . e l o s i i n i z i a l i z z a .
n>s i n = 0 ;
n>des = 0 ;
new
else
if ( e l > n>e l )
_ I n s e r i s
i ( n>des ,
else
_ I n s e r i s i ( n>s i n , e l ) ;
void
if
}
el );
bool
if
}
}
onst
A l b e r o B i n a r i o : : _Svuota (
PNodo& n ) {
( n ) { // Agis
e s o l o s e l ' a l b e r o non vuoto
_Svuota ( n>s i n ) ;
_Svuota ( n>des ) ;
n;
delete
onst
onst
onst
A l b e r o B i n a r i o : : _Cer
a (
PNodo& n ,
TElem& e l )
(n) {
( n>e l == e l ) // l ' elemento
e r
a t o n e l nodo
o r r e n t e ?
;
if
return true
inline bool
onst
A l b e r o B i n a r i o : : EUnaFoglia (
PNodo& n ) {
// metodo d i s u p p o r t o
he v e r i f i
a s e i l nodo
// p u n t a t o da n o meno una f o g l i a .
( ! n>s i n && ! n>des ) ;
}
return
}
}
n = 0;
numelem;
1;
return
else
return
return
void
if
0;
A l b e r o B i n a r i o : : _PreOrdine (
(n) {
out << n>e l << " " ;
_PreOrdine ( n>s i n ) ;
onst
PNodo& n )
onst
void
if
}
void
if
}
97
_PreOrdine ( n>des ) ;
A l b e r o B i n a r i o : : _PostOrdine (
(n) {
_PostOrdine ( n>s i n ) ;
_PostOrdine ( n>des ) ;
out << n>e l << " " ;
A l b e r o B i n a r i o : : _InOrdine (
(n) {
_InOrdine ( n>s i n ) ;
out << n>e l << " " ;
_InOrdine ( n>des ) ;
onst
PNodo& n )
onst
PNodo& n )
onst
TElem& e l ) {
onst
onst
// Metodi p u b b l i i
void
}
AlberoBinario : : I n s e r i s
i (
_ I n s e r i s
i ( root , e l ) ;
numelem++;
void
}
A l b e r o B i n a r i o : : Svuota ( ) {
_Svuota ( r o o t ) ;
root = 0;
numelem = 0 ;
TElem& e l )
onst
unsigned int A l b e r o B i n a r i o : : E l i m i n a F o g l i e ( )
return _EliminaFoglie ( r o o t ) ;
void
}
void
}
A l b e r o B i n a r i o : : PostOrdine ( )
_PostOrdine ( r o o t ) ;
void
}
onst
A l b e r o B i n a r i o : : PreOrdine ( )
_PreOrdine ( r o o t ) ;
A l b e r o B i n a r i o : : InOrdine ( )
_InOrdine ( r o o t ) ;
onst
onst
File main. pp
#in
lude
#in
lude
<i o s t r e a m>
" A l b e r o B i n a r i o . h"
using namespa e
98
std ;
// P r o t o t i p i d i f u n z i o n i d i s u p p o r t o per l a v e r i f i
a d e l
o r r e t t o funzionamento
// d e i metodi d e l l a
l a s s e A l b e r o B i n a r i o .
stampaMenu ( ) ;
I n s e r i s
i ( AlberoBinario& a ) ;
Svuota ( A l b e r o B i n a r i o & a ) ;
Cer
a ( A l b e r o B i n a r i o & a ) ;
EliminaFoglie ( AlberoBinario& a ) ;
NumeroElementi ( A l b e r o B i n a r i o & a ) ;
PreOrdine ( A l b e r o B i n a r i o & a ) ;
InOrdine ( A l b e r o B i n a r i o & a ) ;
PostOrdine ( A l b e r o B i n a r i o & a ) ;
void
void
void
void
void
void
void
void
void
int main ( ) {
AlberoBinario
har
;
do {
albero ;
stampaMenu ( ) ;
i n >>
;
in . ignore ( ) ;
swit
h (
) {
ase ' 1 ' :
I n s e r i s
i ( albero ) ;
break ;
ase ' 2 ' :
Svuota ( a l b e r o ) ;
break ;
ase ' 3 ' :
Cer
a ( a l b e r o ) ;
break ;
ase ' 4 ' :
EliminaFoglie ( albero ) ;
break ;
ase ' 5 ' :
NumeroElementi ( a l b e r o ) ;
break ;
ase ' 6 ' :
PreOrdine ( a l b e r o ) ;
break ;
ase ' 7 ' :
InOrdine ( a l b e r o ) ;
break ;
ase ' 8 ' :
PostOrdine ( a l b e r o ) ;
break ;
ase ' 9 ' :
break ;
default :
out << " S
e l t a non v a l i d a . \ n" ;
break ;
}
} while (
!= ' 9 ' ) ;
return 0 ;
void
stampaMenu ( ) {
out << e n d l ;
out
out
out
out
out
out
out
out
out
out
out
<<
<<
<<
<<
<<
<<
<<
<<
<<
<<
<<
void I n s e r i s
i ( A l b e r o B i n a r i o & a )
int i ;
}
void
}
Svuota ( A l b e r o B i n a r i o & a ) {
out << " Svuotamento a l b e r o . " << e n d l ;
a . Svuota ( ) ;
if ( a . Cer
a ( i ) )
out << " Elemento
else
t r o v a t o . \ n" ;
void
}
EliminaFoglie ( AlberoBinario& a ) {
out << a . E l i m i n a F o g l i e ( ) << " f o g l i e e l i m i n a t e . \ n" ;
void
}
NumeroElementi ( A l b e r o B i n a r i o & a ) {
out << " I l numero d i e l e m e n t i e ' : " << a . NumElem ( ) << e n d l ;
void
}
PreOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n pre o r d i n e : \ n" ;
a . PreOrdine ( ) ;
out << e n d l ;
void
}
InOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n o r d i n e : \ n" ;
a . InOrdine ( ) ;
out << e n d l ;
void
PostOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n post o r d i n e : \ n" ;
a . PostOrdine ( ) ;
out << e n d l ;
99
100
// Metodo p r i v a t o
{
sono v u o t i ) . . .
if
return false
return
// Metodo p u b b l i
o
AlberoBinario : :
==(
AlberoBinario& rhs )
{
// Questo o p e r a t o r e v i e n e i n v o
a t o
on l a s e g u e n t e s i n t a s s i :
//
a1 == a2 ;
// dove a1 ed a2 sono due i s t a n z e d e l l a
l a s s e A l b e r o B i n a r i o .
// In q u e s t o
aso v i e n e i n v o
a t o l ' o p e r a t o r ==() s u l l ' o g g e t t o a1 ,
// mentre a2 , parametro a t t u a l e , v i e n e p a s s a t o per r i f e r i m e n t o
// prendendo i l p o s t o d e l parametro f o r m a l e r h s .
bool
operator onst
onst
return
// Metodo p r i v a t o
if
PNodo& n )
onst
101
// e v e n t u a l e
o n t r i b u t o s o t t o a l b e r o d e s t r o
( n>des )
ount =
ount + _ContaNodiNonFoglia ( n>des ) ;
if
// e v e n t u a l e
o n t r i b u t o d e l p r e s e n t e nodo
( n>s i n | | n>des )
ount++;
if
return
ount ;
// Metodo p u b b l i o
File AlberoBinario.h
#ifndef _ALBEROBINARIO_H_
#define _ALBEROBINARIO_H_
stru
t Nodo ; // Forward d e
l a r a t i o n
typedef Nodo PNodo ;
typedef unsigned int TElem ; //L ' a l b e r o
lass A l b e r o B i n a r i o {
private :
PNodo r o o t ; // r a d i
e d e l l ' a l b e r o
unsigned int numelem ;
ontiene i n t er i p o s i t i v i
// Metodi r i
o r s i v i d i s u p p o r t o
_ I n s e r i s
i ( PNodo& n ,
TElem& e l ) ;
_Svuota (
PNodo& n ) ;
_Cer
a (
PNodo& n ,
TElem& e l )
;
_ContaNodi (
PNodo& n ,
& zero ,
void
void
bool
void
void
void
void
onst
onst
onst
onst
onst
onst
unsigned int
unsigned int& uno ,
unsigned int& due )
onst ;
_PreOrdine (
onst PNodo& n )
onst ;
_PostOrdine (
onst PNodo& n )
onst ;
_InOrdine (
onst PNodo& n )
onst ;
A l b e r o B i n a r i o (
onst A l b e r o B i n a r i o &); // i m p e d i s
e l a
o p i a mediante
o s t r u t t o r e
void operator=(
onst A l b e r o B i n a r i o &); // i m p e d i s
e l ' a s s e g n a z i o n e
publi
:
A l b e r o B i n a r i o ( ) ; //
o s t r u t t o r e senza parametri
~ A l b e r o B i n a r i o ( ) ; // D i s t r u t t o r e
void
void
bool
onst TElem& e l ) ;
onst TElem& e l )
onst ;
Inseris
i (
Svuota ( ) ;
Cer
a (
};
102
void ContaNodi ( unsigned int& zero , unsigned int& uno , unsigned int& due )
onst ;
unsigned int NumElem ( )
onst ;
void PreOrdine ( )
onst ;
void PostOrdine ( )
onst ;
void InOrdine ( )
onst ;
#endif
/ _ALBEROBINARIO_H_ /
File AlberoBinario. pp
TElem e l ;
PNodo s i n ;
PNodo des ;
A l b e r o B i n a r i o : : A l b e r o B i n a r i o ( ) : r o o t ( 0 ) , numelem ( 0 ) {
}
AlberoBinario : :~ AlberoBinario () {
Svuota ( ) ;
}
// Metodi p r i v a t i r i o r s i v i d i s u p p o r t o
void
if
}
new
else
if ( e l > n>e l )
_ I n s e r i s
i ( n>des ,
else
el );
_ I n s e r i s i ( n>s i n , e l ) ;
void
if
}
onst
A l b e r o B i n a r i o : : _ I n s e r i s
i ( PNodo& n ,
TElem& e l ) {
( ! n) {
n =
Nodo ; // s i
rea un nuovo elemento d e l l ' a l b e r o . . .
n>e l = e l ; // . . . e l o s i i n i z i a l i z z a .
n>s i n = 0 ;
n>des = 0 ;
bool
if
onst
A l b e r o B i n a r i o : : _Svuota (
PNodo& n ) {
( n ) { // Agis
e s o l o s e l ' a l b e r o non vuoto
_Svuota ( n>s i n ) ;
_Svuota ( n>des ) ;
n;
delete
onst
onst
onst
A l b e r o B i n a r i o : : _Cer
a (
PNodo& n ,
TElem& e l )
(n) {
( n>e l == e l ) // l ' elemento
e r
a t o n e l nodo
o r r e n t e ?
;
if
return true
}
}
void
if
103
e l ) ; // e r a n e l s o t t o a l b e r o s i n i s t r o
A l b e r o B i n a r i o : : _ContaNodi (
(n) {
_ContaNodi ( n>s i n , zero , uno , due ) ;
_ContaNodi ( n>des , zero , uno , due ) ;
void
if
}
void
if
}
void
if
}
uno++;
A l b e r o B i n a r i o : : _PreOrdine (
(n) {
out << n>e l << " " ;
_PreOrdine ( n>s i n ) ;
_PreOrdine ( n>des ) ;
onst
A l b e r o B i n a r i o : : _PostOrdine (
(n) {
_PostOrdine ( n>s i n ) ;
_PostOrdine ( n>des ) ;
out << n>e l << " " ;
A l b e r o B i n a r i o : : _InOrdine (
(n) {
_InOrdine ( n>s i n ) ;
out << n>e l << " " ;
_InOrdine ( n>des ) ;
onst
PNodo& n )
onst
PNodo& n )
onst
PNodo& n )
void
}
AlberoBinario : : I n s e r i s
i (
_ I n s e r i s
i ( root , e l ) ;
numelem++;
onst
TElem& e l ) {
void
}
A l b e r o B i n a r i o : : Svuota ( ) {
_Svuota ( r o o t ) ;
root = 0;
numelem = 0 ;
onst
onst
// Metodi p u b b l i i
TElem& e l )
104
onst
void
A l b e r o B i n a r i o : : ContaNodi (
unsigned int&
zero = 0;
uno = 0 ;
due = 0 ;
_ContaNodi ( root , zero , uno , due ) ;
void
}
A l b e r o B i n a r i o : : PostOrdine ( )
_PostOrdine ( r o o t ) ;
void
}
onst
A l b e r o B i n a r i o : : PreOrdine ( )
_PreOrdine ( r o o t ) ;
void
}
zero ,
A l b e r o B i n a r i o : : InOrdine ( )
_InOrdine ( r o o t ) ;
onst
onst
File main. pp
void
void
void
void
void
void
void
void
void
int main ( ) {
AlberoBinario
har
;
do {
albero ;
stampaMenu ( ) ;
i n >>
;
in . ignore ( ) ;
swit
h (
) {
ase ' 1 ' :
I n s e r i s
i ( albero ) ;
break ;
ase ' 2 ' :
Svuota ( a l b e r o ) ;
break ;
ase ' 3 ' :
Cer
a ( a l b e r o ) ;
break ;
ase ' 4 ' :
ContaNodi ( a l b e r o ) ;
break ;
ase ' 5 ' :
NumeroElementi ( a l b e r o ) ;
break ;
ase ' 6 ' :
PreOrdine ( a l b e r o ) ;
break ;
ase ' 7 ' :
InOrdine ( a l b e r o ) ;
break ;
ase ' 8 ' :
PostOrdine ( a l b e r o ) ;
break ;
ase ' 9 ' :
break ;
default :
out << " S
e l t a non v a l i d a . \ n" ;
break ;
}
} while (
!= ' 9 ' ) ;
return 0 ;
void
stampaMenu ( ) {
out << e n d l ;
out << " 1 . I n s e r i s
i " << e n d l ;
out << " 2 . Svuota " << e n d l ;
out << " 3 . Cer
a " << e n d l ;
out << " 4 . Conta Nodi " << e n d l ;
out << " 5 . Numero Elementi " << e n d l ;
out << " 6 . Pre o r d i n e " << e n d l ;
out << " 7 . InOrdine " << e n d l ;
out << " 8 . Post Ordine " << e n d l ;
out << " 9 . E s
i " << e n d l ;
out << e n d l ;
out << " S
e l t a : " ;
void I n s e r i s
i ( A l b e r o B i n a r i o & a )
int i ;
}
void
}
Svuota ( A l b e r o B i n a r i o & a ) {
out << " Svuotamento a l b e r o . " << e n d l ;
a . Svuota ( ) ;
if
( a . Cer a ( i ) )
105
else
a . ContaNodi (num0 ,
out << " Nodi
on
out << " Nodi
on
out << " Nodi
on
num1 , num2 ) ;
0 f i g l i : " << num0 <<e n d l ;
1 f i g l i o : " << num1 <<e n d l ;
2 f i g l i : " << num2 <<e n d l ;
void
}
NumeroElementi ( A l b e r o B i n a r i o & a ) {
out << " I l numero d i e l e m e n t i e ' : " << a . NumElem ( ) << e n d l ;
void
}
PreOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n pre o r d i n e : \ n" ;
a . PreOrdine ( ) ;
out << e n d l ;
void
}
InOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n o r d i n e : \ n" ;
a . InOrdine ( ) ;
out << e n d l ;
void
}
PostOrdine ( A l b e r o B i n a r i o & a ) {
out << "Stampa i n post o r d i n e : \ n" ;
a . PostOrdine ( ) ;
out << e n d l ;
106
Capitolo SP
Soluzioni degli eser
izi su pile
SP.1 Push Greater
Tra
ia a pag. 25
TElem e l ;
PRe
s u
;
lass P i l a {
private :
PRe
top ;
int nelem ;
publi
:
Pila ( ) ;
~Pila ( ) ;
void
bool
};
onst
onst
onst
Push (
TElem& e ) ;
PushGreater (
TElem& e ) ;
TElem Top ( )
;
TElem Pop ( ) ;
Svuota ( ) ;
Count ( )
;
Empty ( )
;
void
int
bool
onst
onst
P i l a : : P i l a ( ) : top ( 0 ) , nelem ( 0 ) {
}
Pila ::~ Pila () {
Svuota ( ) ;
}
107
void
onst
P i l a : : Push (
TElem& e ) {
PRe
p =
Re
ord ;
p>e l = e ;
p>s u
= top ;
top = p ;
nelem++;
bool
if
}
}
new
onst
P i l a : : PushGreater (
TElem& e ) {
( Empty ( ) | | ( e > Top ( ) ) ) {
Push ( e ) ;
;
return true
else
return false ;
onst
TElem P i l a : : Top ( )
( top )
top >e l ;
if
108
return
TElem P i l a : : Pop ( ) {
( top ) {
TElem e = top >e l ; // memorizza i l v a l o r e d i t e s t a per r e s t i t u i r l o a l l a f i n e
if
// memorizza i l p u n t a t o r e a l l a t e s t a : e s s a dovr e s s e r e
a n
e l l a t a
PRe
p = top ;
top = top >s u
; // p o r t a l a t e s t a a l s u
e s s i v o
p ; // e l i m i n a l a v e
h i a t e s t a
delete
}
}
nelem ;
e;
return
void P i l a : : Svuota ( )
while ( top ) {
PRe
p = top ;
top = top >s u
;
p;
delete
}
nelem = 0 ;
void
void
void
void
void
stampaMenu ( ) ;
Push ( P i l a& p ) ;
PushGreater ( P i l a& p ) ;
Top ( P i l a& p ) ;
Pop ( P i l a& p ) ;
do
{
stampaMenu ( ) ;
i n >>
;
in . ignore ( ) ;
swit
h (
) {
ase ' 1 ' :
Push ( p i l a ) ;
break ;
ase ' 2 ' :
PushGreater ( p i l a ) ;
break ;
ase ' 3 ' :
Top ( p i l a ) ;
break ;
ase ' 4 ' :
Pop ( p i l a ) ;
break ;
ase ' 5 ' :
Svuota ( p i l a ) ;
break ;
ase ' 6 ' :
Count ( p i l a ) ;
break ;
ase ' 7 ' :
Empty ( p i l a ) ;
break ;
ase ' 8 ' :
break ;
default :
out << " S
e l t a non
break ;
}
} while (
!= ' 8 ' ) ;
return 0 ;
void
v a l i d a . \ n" ;
stampaMenu ( ) {
out << e n d l ;
out << " 1 . Push" << e n d l ;
out << " 2 . PushGreater " << e n d l ;
out << " 3 . Top" << e n d l ;
out << " 4 . Pop" << e n d l ;
out << " 5 . Svuota " << e n d l ;
out << " 6 . Count " << e n d l ;
out << " 7 . Empty" << e n d l ;
out << " 8 . E s
i " << e n d l ;
out << e n d l ;
out << " S
e l t a : " ;
void
Push ( P i l a& p ) {
109
TElem e l ;
out << " I n s e r i r e elemento : " ;
i n >> e l ;
p . Push ( e l ) ;
void
PushGreater ( P i l a& p ) {
TElem e l ;
out << " I n s e r i r e elemento : " ;
i n >> e l ;
( p . PushGreater ( e l ) )
out << " Elemento i n s e r i t o . \ n" ;
if
else
void
}
Svuota ( P i l a& p ) {
p . Svuota ( ) ;
out << " P i l a s v u o t a t a . \ n" ;
void
}
Count ( P i l a& p ) {
out << "Numero e l e m e n t i : " << p . Count ( ) << e n d l ;
endl ;
110
Capitolo SC
Soluzioni degli eser
izi su
ode
SC.1 Coda
Tra
ia a pag. 27
TElem e l ;
PRe
s u
;
Re
ord ( ) {
out << " C o s t r u t t o r e i n v o
a t o . \ n" ; }
~Re
ord ( ) {
out << " D i s t r u t t o r e i n v o
a t o . \ n" ; }
lass Coda
private :
PRe
head ;
PRe
t a i l ;
nelem ;
:
Coda ( ) ;
~Coda ( ) ;
int
publi
void
};
onst TElem&
onst ;
onst ;
onst ;
onst ;
Push (
TElem Top ( )
TElem Pop ( ) ;
TElem Somma ( )
Svuota ( ) ;
Count ( )
Empty ( )
void
int
bool
e );
112
void
onst
new
Coda : : Push (
TElem& e ) {
// Creo un nuovo elemento n e l l ' heap
PRe
temp =
Re
ord ;
temp>e l = e ;
temp>s u
= 0 ;
if
if
nelem++;
onst
if
return
if
if
nelem ;
temp ;
el_temp ;
delete
return
onst
for
return
sum ;
{
{
delete
head = t a i l = 0 ;
nelem = 0 ;
void stampaMenu ( ) ;
void Push ( Coda&
) ;
void Top ( Coda&
) ;
void Pop ( Coda&
) ;
void Somma( Coda&
) ;
void Svuota ( Coda&
) ;
void Count ( Coda&
) ;
void Empty ( Coda&
) ;
int main ( )
{
har
;
Coda
oda ;
do
{
stampaMenu ( ) ;
i n >>
;
in . ignore ( ) ;
swit
h (
) {
ase ' 1 ' :
Push (
oda ) ;
break ;
ase ' 2 ' :
Top (
oda ) ;
break ;
ase ' 3 ' :
Pop (
oda ) ;
break ;
ase ' 4 ' :
Somma(
oda ) ;
break ;
ase ' 5 ' :
Svuota (
oda ) ;
break ;
ase ' 6 ' :
Count (
oda ) ;
break ;
ase ' 7 ' :
Empty (
oda ) ;
break ;
ase ' 8 ' :
break ;
default :
out << " S
e l t a
break ;
}
} while (
!= ' 8 ' ) ;
return 0 ;
non v a l i d a . \ n" ;
113
void
stampaMenu ( ) {
out << e n d l ;
out << " 1 . Push" << e n d l ;
out << " 2 . Top" << e n d l ;
out << " 3 . Pop" << e n d l ;
out << " 4 . Somma" << e n d l ;
out << " 5 . Svuota " << e n d l ;
out << " 6 . Count " << e n d l ;
out << " 7 . Empty" << e n d l ;
out << " 8 . E s
i " << e n d l ;
out << e n d l ;
out << " S
e l t a : " ;
void
}
Push ( Coda&
) {
TElem e l ;
out << " I n s e r i r e elemento : " ;
i n >> e l ;
. Push ( e l ) ;
void
}
Somma( Coda&
) {
out << "Somma e l e m e n t i : " <<
. Somma( ) << e n d l ;
void
}
Svuota ( Coda&
) {
. Svuota ( ) ;
out << "Coda s v u o t a t a . \ n" ;
void
}
Count ( Coda&
) {
out << "Numero e l e m e n t i : " <<
. Count ( ) << e n d l ;
endl ;
114
115
TElem e l ;
PRe
s u
;
lass Coda
private :
PRe
head ;
PRe
t a i l ;
unsigned int p o s t i ;
int nelem ;
publi
:
Coda ( unsigned int _posti ) ;
~Coda ( ) ;
bool
};
onst TElem& e ) ;
onst ;
unsigned int n ) ;
onst ;
onst ;
Push (
TElem Top ( )
TElem Pop ( ) ;
TElem Pop (
Svuota ( ) ;
Count ( )
Empty ( )
void
int
bool
Coda : : Coda (
}
unsigned int
Coda : : ~ Coda ( ) {
Svuota ( ) ;
}
bool
if
onst
Coda : : Push (
TElem& e ) {
( nelem == p o s t i )
;
return false
new
if
if
116
head = temp ;
nelem++;
}
return true ;
onst
if
return
if
if
nelem ;
temp ;
el_temp ;
delete
return
unsigned int
if
n) {
for int
return
el ;
{
{
delete
head = t a i l = 0 ;
nelem = 0 ;
void stampaMenu ( ) ;
void Push ( Coda&
) ;
void Top ( Coda&
) ;
void Pop ( Coda&
) ;
void PopMany( Coda&
) ;
void Svuota ( Coda&
) ;
void Count ( Coda&
) ;
void Empty ( Coda&
) ;
int main ( )
{
har
;
unsigned int i ;
out << " I n s e r i r e i l numero massimo d i e l e m e n t i i n
oda : " ;
i n >> i ;
Coda
oda ( i ) ;
do
{
stampaMenu ( ) ;
i n >>
;
in . ignore ( ) ;
swit
h (
) {
ase ' 1 ' :
Push (
oda ) ;
break ;
ase ' 2 ' :
Top (
oda ) ;
break ;
ase ' 3 ' :
Pop (
oda ) ;
break ;
ase ' 4 ' :
PopMany(
oda ) ;
break ;
ase ' 5 ' :
Svuota (
oda ) ;
break ;
ase ' 6 ' :
Count (
oda ) ;
break ;
ase ' 7 ' :
Empty (
oda ) ;
break ;
ase ' 8 ' :
break ;
default :
out << " S
e l t a
break ;
}
} while (
!= ' 8 ' ) ;
return 0 ;
void
non v a l i d a . \ n" ;
stampaMenu ( ) {
out << e n d l ;
out << " 1 . Push" << e n d l ;
out << " 2 . Top" << e n d l ;
out << " 3 . Pop" << e n d l ;
117
out
out
out
out
out
out
out
<<
<<
<<
<<
<<
<<
<<
void
Push ( Coda&
) {
TElem e l ;
out << " I n s e r i r e elemento : " ;
i n >> e l ;
(
. Push ( e l ) )
out << " Elemento i n s e r i t o . \ n" ;
if
else
) {
if ( !
. Empty ( ) )
out << " Elemento
else
void
}
Svuota ( Coda&
) {
. Svuota ( ) ;
out << "Coda s v u o t a t a . \ n" ;
void
}
Count ( Coda&
) {
out << "Numero e l e m e n t i : " <<
. Count ( ) << e n d l ;
endl ;
118
119
Il prelievo di
Appro
io 1
La
oda a priorit pu essere immaginata formata da una sequenza di elementi
ostituita a sua volta da due sotto-sequenze (vedi Figura SC.1):
una su essiva sotto-sequenza, he si estende no alla oda, he omprende gli elementi a priorit bassa.
File PriorityQueue.h
t
Figura SC.1:
120
onse utive
PRe
head ;
PRe
t a i l ; // p u n t a t o r e a l l a
oda
PRe
t a i l _ h ; // p u n t a t o r e a l l a
oda d e l l a s o t t o s e q u e n z a ad a l t a p r i o r i t
Push (
TElem& e ) ; //
l a s s i
o Push i n
oda
:
PriorityQueue ( ) ;
~P r i o r i t y Q u e u e ( ) ;
void
publi
};
onst
TElem e l ;
PRe
s u
;
P r i o r i t y Q u e u e : : P r i o r i t y Q u e u e ( ) : head ( 0 ) , t a i l ( 0 ) , t a i l _ h ( 0 ) {}
PriorityQueue : :~ PriorityQueue () {
Clear ( ) ;
}
void
onst
P r i o r i t y Q u e u e : : Push (
TElem& e ) { //
l a s s i
o Push i n
oda
// Creo un nuovo elemento n e l l ' heap
PRe
temp =
Re
ord ;
temp>e l = e ;
temp>s u
= 0 ;
new
if
if
void
}
121
onst
P r i o r i t y Q u e u e : : PushLow (
TElem& e ) {
Push ( e ) ; // s i r i d u
e ad un
l a s s i
o i n s e r i m e n t o i n
oda
void
if
P r i o r i t y Q u e u e : : PushHigh (
( ! tail_h ) {
onst
TElem& e ) {
//non
i sono e l e m e n t i ad a l t a p r i o r i t : a g g i u n t a i n t e s t a
( ! head ) // l a
oda vuota ?
Push ( e ) ; // i n s e r i s
o
on Push ( )
{
PRe
temp =
Re
ord ;
temp>e l = e ;
temp>s u
= head ;
head = temp ;
}
if
else
new
else
{
// i n s e r i s
o
PRe
temp =
Re
ord ;
temp>e l = e ;
temp>s u
= tail_h >s u
;
tail_h >s u
= temp ;
new
t a i l _ h = temp ; // a g g i o r n o i l p u n t a t o r e t a i l _ h
// a g g i o r n o t a i l s e l ' elemento a g g i u n t o i n u l t i m a p o s i z i o n e
( ! tail_h >s u
)
t a i l = tail_h ;
if
TElem P r i o r i t y Q u e u e : : Pop ( ) {
( head ) {
PRe
temp = head ;
TElem el_temp = temp>e l ;
if
if
if
delete
return
temp ;
el_temp ;
void P r i o r i t y Q u e u e : : C l e a r ( )
while ( head != 0) {
PRe
tbd = head ;
122
CODA A PRIORIT
SOTTO-CODA AD
ALTA PRIORIT
SOTTO-CODA A
BASSA PRIORIT
Figura SC.2: Coda a priorit formata da due lassi he ode aan ate
}
}
delete
head = t a i l = t a i l _ h = 0 ;
Appro
io 2
La
oda a priorit pu essere immaginata
omposta di due
ode sempli
i
aan
ate (vedi Figura SC.2).
bassa priorit. Il metodo
Il metodo
Pop()
Il metodo
PushHigh(),
PushLow()
no visibili dall'esterno della struttura (information hiding ), la quale
ontinuer ad apparire ai suoi utenti
ome un'uni
a
oda dotata dei me
anismi
di priorit ri
hiesti.
Coda
oda_l ;
Coda
oda_h ;
onst P r i o r i t y Q u e u e&
) ; // i n i b i s
e
operator=(
onst P r i o r i t y Q u e u e&
) ;
PriorityQueue (
P r i o r i t y Q u e u e&
:
publi
la
opia . . .
// . . e l ' a s s e g n a z i o n e
123
PriorityQueue ( ) ;
};
oda_l . Push ( e ) ;
void
}
P r i o r i t y Q u e u e : : PushHigh (
oda_h . Push ( e ) ;
onst
TElem& e ) {
TElem& e ) {
TElem P r i o r i t y Q u e u e : : Pop ( ) {
( !
oda_h . Empty ( ) )
oda_h . Pop ( ) ;
}
if
return
else
return
oda_l . Pop ( ) ;
void
}
PriorityQueue : : Clear () {
oda_h . Svuota ( ) ;
oda_l . Svuota ( ) ;
Appro
io 3
La
oda a priorit pu essere una normale
oda in
ui i re
ord, disposti
se
ondo l'ordine di inserimento, vengono eti
hettati
on la loro priorit
di Figura SC.3).
PushLow(),
il metodo
PushHigh()
(ve-
he il metodo
Pop()
giusto elemento. Tale operazione viene eettuata s
orrendo tutta la struttura alla ri
er
a del primo elemento ad alta priorit e restituendolo dopo
averlo eliminato dalla
oda. In assenza di un elemento ad alta priorit viene
restituito l'eventuale elemento di testa.
Tale implementazione, pur prestandosi a diverse ottimizzazioni, non risulta parti
olarmente e
iente, ri
hiedendo un
i
lo di ri
er
a per ogni operazione
Pop()
1 Questo
124
el
el
el
el
el
el
el
el
el
SC.4 PopMinMax
Tra
ia a pag. 30
// Metodo p r i v a t o
// I l metodo s e g u e n t e e s t r a e n e l e m e n t i e ne r e s t i t u i s
e i l minimo ed i l massimo
Coda : : _PopMinMax(
n , TElem& min , TElem& max) {
min = max = Pop ( ) ; // Assegno min e max a l l ' elemento d i t e s t a
void
unsigned int
unsigned int
i = 1;
//
o n f r o n t o
on g l i a l t r i n1 e l e m e n t i s e g u e n t i : n1 i t e r a z i o n i
// ( s e l a
oda non s i s v u o t a prima )
( ( i < n ) && ! Empty ( ) ) {
TElem e l = Pop ( ) ;
( e l < min )
min = e l ;
( e l > max)
max = e l ;
i ++;
}
while
if
if
// Metodi p u b b l i
i
// I l metodo s e g u e n t e
hiama _PopMinMax ( ) e r e s t i t u i s
e i l massimo
TElem Coda : : PopMax (
n) {
TElem min , max ;
_PopMinMax( n , min , max ) ;
max ;
}
unsigned int
return
unsigned int
return
Capitolo SX
Soluzioni degli altri eser
izi
SX.1 A
umulatore
Tra
ia a pag. 32
int
};
a ; };
main ( )
A
umulatore a ;
f;
h ;
float
har
out
out
out
out
do
<<
<<
<<
<<
add \n" ;
r e s e t \n" ;
show\n" ;
e x i t \n" ;
{
i n >>
h ;
(
h ) {
'a ' :
out << " I n s e r t v a l u e : " ;
i n >> f ;
a . Add( f ) ;
out << " Value added . \ n" ;
;
'r ':
a . Reset ( ) ;
out << " Reset . \ n" ;
swit
h
ase
break
ase
125
break ;
ase ' s ' :
out << "The
break ;
ase ' e ' :
break ;
default :
while (
h
return 0 ;
}
126
!= ' e ' ) ;
ti olarmente onveniente nel aso di metodi molto sempli i ( ome questi ostituiti da una sola riga), ed equivalente a rendere i metodi
inline attraverso
l'appro io lassi o alla stesura dei metodi di una lasse e l'uso della keyword
inline.
SX.2 Cifratore
Tra
ia a pag. 32
onst ;
ifra )
};
Cifratore : : Cifratore (
}
int
) : hiave ( ) {
har C i f r a t o r e : : C i f r a C a r a t t e r e (
har
if (
i f r a )
return
+
h i a v e ;
else
return
h i a v e ;
bool
void C i f r a t o r e : : C i f r a (
har
while ( s t r ) {
str )
s t r = C i f r a C a r a t t e r e ( str ,
void
s t r ++;
Cifratore : : De ifra (
har
onst {
true ) ;
str )
onst
ifra )
onst
while
( s t r ) {
s t r = C i f r a C a r a t t e r e ( str ,
s t r ++;
127
false ) ;
int main ( )
har s t r [ 1 0 0 ;
int
h i a v e ;
system ( "PAUSE" ) ;
0;
return
Nome n ;
Quantita q ;
stru
t Re
ord ;
typedef Re
ord
stru
t Re
ord {
};
PRe ;
lass L i s t a D e l l a S p e s a {
private :
PRe
f i r s t ;
bool R i
e r
a (
onst Nome n , PRe
& p )
onst ;
bool _Elimina ( PRe
& p ,
onst Nome n ) ;
bool S t r i n g h e U g u a l i (
onst
har s1 ,
onst
har
s2 )
onst
return
128
( str mp ( s1 , s2 ) == 0 ) ;
// i n i b i s
e l a
o p i a mediante
o s t r u t t o r e
ListaDellaSpesa (
L i s t a D e l l a S p e s a &) { } ;
// i n i b i s
e l a
o p i a mediante a s s e g n a z i o n e
= (
L i s t a D e l l a S p e s a &) { } ;
:
ListaDellaSpesa ( ) ;
~ListaDellaSpesa ( ) ;
onst
void operator
onst
publi
};
Quantita q ) ;
n)
onst ;
bool
onst
onst
ListaDellaSpesa : : Ri
er
a (
Nome n , PRe
& p )
{
// Questo metodo
e r
a l ' a r t i
o l o a v e n t e i l nome s p e
i f i
a t o e r e s t i t u i s
e :
// t r u e o f a l s e , a se
onda
he l ' a r t i
o l o s i a s t a t o t r o v a t o o meno ;
// i l p u n t a t o r e a l l ' u l t i m o r e
o r d v i s i t a t o .
( first ) {
p = first ;
( S t r i n g h e U g u a l i ( p>a r t . n , n ) )
;
{
( p>s u
) {
p = p>s u
;
( S t r i n g h e U g u a l i ( p>a r t . n , n ) )
;
}
}
}
if
if
return true
else
while
if
return true
return false ;
bool
if
onst
if
delete
return true
else
return _Elimina ( p>su
,
n);
return false ;
Quantita L i s t a D e l l a S p e s a : : Aggiungi (
onst
Nome n , Quantita q ) {
if
129
(! first ) {
first =
Re
ord ;
f i r s t >s u
= 0 ;
s t r
p y ( f i r s t >a r t . n , n ) ;
f i r s t >a r t . q = q ;
q;
new
return
else {
PRe p ;
if
}
( R i
e r
a ( n , p ) ) { // e s i s t e n e l l a l i s t a un elemento a v e n t e i l nome n?
// t r o v a t o => ora p punta a l l ' elemento a v e n t e nome n
p>a r t . q += q ;
else
{
// non t r o v a t o => ora p punta a l l ' u l t i m o elemento d e l l a l i s t a
Re
ord ;
p>s u
=
p = p>s u
;
s t r
p y ( p>a r t . n , n ) ;
p>a r t . q = q ;
p>s u
= 0 ;
return
new
p>a r t . q ;
Nome n ) {
Quantita L i s t a D e l l a S p e s a : : GetQuantita (
PRe
p ;
( Ri
er
a (n , p ))
p>a r t . q ;
}
if
return
else
return
void
if
onst
Nome n )
0;
L i s t a D e l l a S p e s a : : Svuota ( ) {
( first ) {
PRe
tbd = f i r s t ;
PRe
p ;
while ( tbd ) {
p = tbd >s u
;
delete tbd ;
}
tbd = p ;
}
f i r s t = 0;
void
stampa_menu( ) {
onst
out
out
out
out
out
out
out
<<
<<
<<
<<
<<
<<
<<
" 1:
" 2:
" 3:
" 4:
" 5:
" 6:
" 7:
Aggiungi a r t i
o l o . \ n" ;
Elimina a r t i
o l o . \ n" ;
Quantita ' a r t i
o l o . \ n" ;
Svuota l i s t a . \ n" ;
Stampa l i s t a . \ n" ;
E s e g u i t e s t v e l o
e . \ n" ;
E s
i . \ n" ;
ListaDellaSpesa l ;
int s
e l t a ;
do {
stampa_menu ( ) ;
i n >> s
e l t a ;
( s
elta ) {
1:
Aggiungi ( l ) ;
;
2:
Elimina ( l ) ;
;
3:
GetQuantita ( l ) ;
;
4:
Svuota ( l ) ;
;
5:
Stampa ( l ) ;
;
6:
TestVelo
e ( l ) ;
;
7:
;
:
out << " S
e l t a non v a l i d a . \ n" ;
;
}
}
( s
e l t a != 7 ) ;
swit
h
ase
break
ase
break
ase
break
ase
break
ase
break
ase
break
ase
break
default
break
while
return 0 ;
void
Aggiungi ( L i s t a D e l l a S p e s a & l ) {
Nome n ;
Quantita q , qq ;
out << "Nome a r t i
o l o : " ;
i n >> n ;
out << " Quantita ' : " ;
i n >> q ;
qq = l . Aggiungi ( n , q ) ;
130
131
void
Elimina ( L i s t a D e l l a S p e s a& l ) {
Nome n ;
out << "Nome a r t i
o l o : " ;
i n >> n ;
if ( l . Elimina ( n ) )
out << " A r t i
o l o
else
e l i m i n a t o . " << e n d l ;
void
GetQuantita ( L i s t a D e l l a S p e s a & l ) {
Nome n ;
Quantita q ;
out << "Nome a r t i
o l o : " ;
i n >> n ;
q = l . GetQuantita ( n ) ;
out << "La q u a n t i t a ' e ' " << q << e n d l ;
void
}
Svuota ( L i s t a D e l l a S p e s a& l ) {
l . Svuota ( ) ;
out << " L i s t a s v u o t a t a . " << e n d l ;
void
}
Stampa ( L i s t a D e l l a S p e s a& l ) {
out << " L i s t a : " << e n d l ;
l . Stampa ( ) ;
void
T e s t V e l o
e ( L i s t a D e l l a S p e s a& l ) {
l . Svuota ( ) ;
l . Aggiungi ( "Pane " , 1 ) ;
l . Aggiungi ( " L a t t e " , 1 . 5 ) ;
l . Aggiungi ( " Zu
hero " , 1 ) ;
l . Aggiungi ( " P r o s
i u t t o " , 0 . 3 ) ;
l . Stampa ( ) ;
out << " L a t t e : " << l . Aggiungi ( " L a t t e " , 0 . 5 ) << e n d l ;
l . Elimina ( "Pane " ) ;
l . Elimina ( " Zu
hero " ) ;
l . Elimina ( " P r o s
i u t t o " ) ;
out << " L a t t e : " << l . Aggiungi ( " L a t t e " , 0 . 5 ) << e n d l ;
l . Svuota ( ) ;
l . Stampa ( ) ;
Il metodo
EstimateTemp()
temperatura basandosi sui dati delle ultime due letture
omuni
ate.
formula da utilizzare la seguente:
La
132
T2 T1
(t t1 ) + T1 ;
T =
t2 t1
dove
t; T1 , T2 , t1
t2
sono
time1 ;
time2 ;
temp1 ;
temp2 ;
publi
:
TempPredi
tor ( Time time , Temp temp ) ;
void SetTemp ( Time time , Temp temp ) ;
Temp EstimateTemp ( Time time )
onst ;
};
void
}
onst
return
int
main ( )
133
// Posso
o s t r u i r e i l p r e d i t t o r e
on q u e s t i d a t i .
TempPredi
tor tp ( 0 , 1 4 ) ;
out << " Stima : l a temperatura a l l ' i s t a n t e 10 s a r a ' "
<< tp . EstimateTemp ( 1 0 ) << e n d l ;
out << " Stima : l a temperatura a l l ' i s t a n t e 20 s a r a ' "
<< tp . EstimateTemp ( 2 0 ) << e n d l ;
out << " L e t t u r a : a l l ' i s t a n t e 5 : l a temperatura v a l e 16\ n" ;
//Comuni
o l a l e t t u r a a l p r e d i t t o r e
tp . SetTemp ( 5 , 1 6 ) ;
out << " Stima : l a temperatura a l l ' i s t a n t e 10 s a r a ' "
<< tp . EstimateTemp ( 1 0 ) << e n d l ;
out << " Stima : l a temperatura a l l ' i s t a n t e 12 s a r a ' "
<< tp . EstimateTemp ( 1 2 ) << e n d l ;
out << " L e t t u r a : a l l ' i s t a n t e 1 0 : l a temperatura v a l e 16\ n" ;
//Comuni
o l a l e t t u r a a l p r e d i t t o r e
tp . SetTemp ( 1 0 , 1 6 ) ;
out << " Stima : l a temperatura a l l ' i s t a n t e 15 s a r a ' "
<< tp . EstimateTemp ( 1 5 ) << e n d l ;
out << " Stima : l a temperatura a l l ' i s t a n t e 20 s a r a ' "
<< tp . EstimateTemp ( 2 0 ) << e n d l ;
system ( "PAUSE" ) ;
0;
return
SX.5 Contenitore
Tra
ia a pag. 35
Nome n ;
Peso p ;
stru
t C e l l a ;
typedef C e l l a
stru
t C e l l a {
};
PCella ;
Oggetto elem ;
PCella s u
;
lass
Contenitore {
134
private :
PCella f i r s t ;
Peso
a p a
i t a ;
Peso somma_pesi ;
nelem ;
:
C o n t e n i t o r e ( Peso max ) ;
~Contenitore ( ) ;
unsigned int
publi
bool
void
har
n , Peso p ) ;
Inseris
i (
Svuota ( ) ;
Peso PesoComplessivo ( )
;
Peso PesoResiduo ( )
;
NumElem ( )
;
Stampa ( )
;
unsigned int
void
onst
};
onst
onst
onst
bool
if
har
Contenitore : : I n s e r i s
i (
n , Peso p ) {
( p <=
a p a
i t a somma_pesi ) {
PCella
=
Cella ;
s t r
p y (
>elem . n , n ) ;
>elem . p = p ;
>s u
= f i r s t ;
first =
;
new
return true ;
return false ;
void C o n t e n i t o r e : : Svuota ( )
while ( f i r s t ) {
}
PCella tbd = f i r s t ;
f i r s t = f i r s t >s u
;
tbd ;
delete
somma_pesi = 0 ;
nelem = 0 ;
Peso C o n t e n i t o r e : : PesoComplessivo ( )
somma_pesi ;
}
return
Peso C o n t e n i t o r e : : PesoResiduo ( )
a p a
i t a somma_pesi ;
}
return
unsigned int
onst
onst
C o n t e n i t o r e : : NumElem ( )
onst
return
nelem ;
void
void
void
void
void
void
void
I n s e r i s
i ( Contenitore&
) ;
Svuota ( C o n t e n i t o r e &
) ;
PesoComplessivo ( C o n t e n i t o r e &
) ;
PesoResiduo ( C o n t e n i t o r e &
) ;
NumeroElementi ( C o n t e n i t o r e &
) ;
Stampa ( C o n t e n i t o r e &
) ;
stampa_menu( ) {
out << " 1 : I n s e r i s
i . \ n" ;
out << " 2 : Svuota . \ n" ;
out << " 3 : Peso Complessivo . \ n" ;
out << " 4 : Peso Residuo . \ n" ;
out << " 5 : Numero Elementi . \ n" ;
out << " 6 : Stampa . \ n" ;
out << " 7 : E s
i . \ n" ;
int
135
main ( )
Peso p ;
out << " I n s e r i s
i peso MAX
o n t e n i t o r e : " ;
i n >> p ;
Contenitore
(p ) ;
int s
e l t a ;
do {
stampa_menu ( ) ;
i n >> s
e l t a ;
( s
elta ) {
1:
Inseris
i (
);
;
2:
Svuota (
) ;
;
3:
PesoComplessivo (
) ;
;
4:
PesoResiduo (
) ;
;
5:
NumeroElementi (
) ;
;
6:
Stampa (
) ;
;
7:
;
:
out << " S
e l t a non v a l i d a . \ n" ;
swit
h
ase
break
ase
break
ase
break
ase
break
ase
break
ase
break
ase
break
default
break ;
} while ( s
e l t a
return 0 ;
}
136
!= 7 ) ;
void I n s e r i s
i ( C o n t e n i t o r e &
har n [NMAX ;
) {
Peso p ;
if
else
void
}
Svuota ( C o n t e n i t o r e &
) {
. Svuota ( ) ;
out << " C o n t e n i t o r e s v u o t a t o . \ n" ;
void
}
PesoComplessivo ( C o n t e n i t o r e &
) {
out << " I l peso
o m p l e s s i v o e ' : " <<
. PesoComplessivo ( ) << e n d l ;
void
}
PesoResiduo ( C o n t e n i t o r e &
) {
out << " I l peso r e s i d u o e ' : " <<
. PesoResiduo ( ) << e n d l ;
void
}
NumeroElementi ( C o n t e n i t o r e &
) {
out << "N. Elem : " <<
. NumElem ( ) << e n d l ;
void
}
Stampa ( C o n t e n i t o r e &
) {
out << " I l
o n t e n u t o d e l
o n t e n i t o r e e ' : \ n" ;
. Stampa ( ) ;
out << e n d l ;
};
137
Nome nom ;
lass L i s t a P r e n o t a z i o n i {
private :
P r e n o t a z i o n e pv ; // p u n t a t o r e a v e t t o r e p r e n o t a z i o n i
int p o s t i ; //numero d i p o s t i d i s p o n i b i l i
int nelem ; // riempimento d e l v e t t o r e
int R i
e r
a ( M a t r i
o l a m)
onst ;
publi
:
L i s t a P r e n o t a z i o n i ( int n ) ;
dinami amente a l l o a t o
~ListaPrenotazioni ( ) ;
};
ListaPrenotazioni : : ListaPrenotazioni (
pv =
Prenotazione [ posti ;
}
new
int
n ) : p o s t i ( n ) , nelem ( 0 ) {
delete
int L i s t a P r e n o t a z i o n i : : R i
e r
a ( M a t r i
o l a m)
onst
for ( int i = 0 ; i < nelem ; i ++)
if ( pv [ i . mat == m)
return i ;
return 1;
bool
if
L i s t a P r e n o t a z i o n i : : Prenota ( M a t r i
o l a m, Nome n ) {
( ( G e t P o s t i D i s p o n i b i l i ( ) > 0) && ( ! E s i s t e P r e n o t a z i o n e (m) ) ) {
pv [ nelem . mat = m;
s t r
p y ( pv [ nelem . nom , n ) ;
nelem++;
}
}
return true ;
return false ;
bool L i s t a P r e n o t a z i o n i : : E l i m i n a P r e n o t a z i o n e ( M a t r i
o l a m)
int i = R i
e r
a (m) ;
if ( i >= 0) {
for ( int j = i ; j < nelem 1 ; j ++)
pv [ j = pv [ j + 1 ;
nelem ;
}
return true ;
138
return false ;
int L i s t a P r e n o t a z i o n i : : G e t P o s t i D i s p o n i b i l i ( )
onst
return p o s t i nelem ;
bool L i s t a P r e n o t a z i o n i : : E s i s t e P r e n o t a z i o n e ( M a t r i
o l a m)
onst
return ( R i
e r
a (m) >= 0 ) ;
}
void
}
L i s t a P r e n o t a z i o n i : : Svuota ( ) {
nelem = 0 ;
void L i s t a P r e n o t a z i o n i : : Stampa ( ) {
for ( int i = 0 ; i < nelem ; i ++)
out << e n d l ;
void
stampa_menu( ) {
out << " 1 : Prenota . \ n" ;
out << " 2 : Elimina p r e n o t a z i o n e . \ n" ;
out << " 3 : P o s t i d i s p o n i b i l i . \ n" ;
out << " 4 : E s i s t e P r e n o t a z i o n e . \ n" ;
out << " 5 : Svuota . \ n" ;
out << " 6 : Stampa . \ n" ;
out << " 7 : E s
i . \ n" ;
int s
e l t a ;
do {
stampa_menu ( ) ;
i n >> s
e l t a ;
( s
elta ) {
1:
Prenota ( l ) ;
;
2:
Elimina ( l ) ;
;
3:
GetPostiDisponibili ( l );
;
4:
swit
h
ase
break
ase
break
ase
break
ase
break ;
ase 5 :
Svuota ( l ) ;
break ;
ase 6 :
Stampa ( l ) ;
break ;
ase 7 :
break ;
default :
out << " S
e l t a non
break ;
}
} while ( s
e l t a != 7 ) ;
return 0 ;
v a l i d a . \ n" ;
void
Prenota ( L i s t a P r e n o t a z i o n i& l ) {
M a t r i
o l a m;
Nome n ;
out << " I n s e r i s
i M a t r i
o l a : " ;
i n >> m;
out << " I n s e r i s
i nome : " ;
i n >> n ;
if ( l . Prenota (m, n ) )
out << " P r e n o t a z i o n e
else
e f f e t t u a t a . \ n" ;
void
Elimina ( L i s t a P r e n o t a z i o n i& l ) {
M a t r i
o l a m;
if ( l . E l i m i n a P r e n o t a z i o n e (m) )
out << " P r e n o t a z i o n e e l i m i n a t a . \ n" ;
else
void
}
G e t P o s t i D i s p o n i b i l i ( L i s t a P r e n o t a z i o n i& l ) {
out << " I p o s t i d i s p o n i b i l i sono : " ;
out << l . G e t P o s t i D i s p o n i b i l i ( ) << e n d l ;
void
E s i s t e P r e n o t a z i o n e ( L i s t a P r e n o t a z i o n i& l ) {
M a t r i
o l a m;
if ( l . E s i s t e P r e n o t a z i o n e (m) )
out << " P r e n o t a z i o n e e s i s t e n t e . \ n" ;
else
}
139
140
void
}
Svuota ( L i s t a P r e n o t a z i o n i& l ) {
l . Svuota ( ) ;
out << " L i s t a s v u o t a t a . \ n" ;
void
}
Stampa ( L i s t a P r e n o t a z i o n i& l ) {
l . Stampa ( ) ;
SX.7 Classi
a
Tra
ia a pag. 38
typedef Squadra
stru
t Re
ord {
};
TElem e l ;
PRe
s u
;
TElem ;
// S i n g o l o elemento ( e l l a ) d e l l a s t r u t t u r a
lass C l a s s i f i
a {
private :
PRe
f i r s t ;
unsigned int nelem ;
C l a s s i f i
a (
onst C l a s s i f i
a &); // i n i b i s
e l a
o p i a mediante
o s t r u t t o r e
void operator= (
onst C l a s s i f i
a &); // i n i b i s
e l ' a s s e g n a z i o n e
unsigned int Elimina (
onst Nome& n ) ;
void I n s e r i m e n t o O r d i n a t o (
onst Nome& n , unsigned int p u n t i ) ;
publi
:
Classifi
a ();
~Classifi
a ();
};
Nome& n ,
unsigned int
C l a s s i f i a : : C l a s s i f i a ( ) : f i r s t ( 0 ) , nelem ( 0 ) {
punti ) ;
141
}
Classifi
a ::~ Classifi
a () {
Svuota ( ) ;
}
unsigned int
onst
C l a s s i f i
a : : Elimina (
Nome& n ) {
// Questo metodo e l i m i n a d a l l a s t r u t t u r a un e v e n t u a l e elemento a v e n t e nome p a r i
// ad n . In
aso d i e s i s t e n z a ne r e s t i t u i s
e i l p u n t e g g i o , a l t r i m e n t i
// r e s t i t u i s
e 0 .
if
unsigned int
delete
return
while
if
unsigned int
delete
return
p = p>s u ;
// Elemento non t r o v a t o
0;
return
void
onst
unsigned int
C l a s s i f i
a : : InserimentoOrdinato (
Nome& n ,
punti ) {
// Questo metodo e f f e t t u a un i n s e r i m e n t o o r d i n a t o n e l l a s t r u t t u r a , i n b a s e a l
//
ampo p u n t e g g i o . S i pro
ede a t t r a v e r s o i s e g u e n t i p a s s i :
// s e l a l i s t a vuota s i i n s e r i s
e l ' elemento e s i e s
e ;
// s i
o n t r o l l a s e i n s e r i r e i n t e s t a : s e s , s i i n s e r i s
e e s i e s
e ;
// s i
e r
a i l punto d i i n s e r i m e n t o a t t r a v e r s o una v i s i t a , s i i n s e r i s
e
//
( e v e n t u a l m e n t e i n
oda ) e s i e s
e .
// In o g n i
aso a l l o
o un nuovo r e
o r d
PRe
nuovo =
Re
ord ;
s t r
p y ( nuovo>e l . n , n ) ;
nuovo>e l . p u n t e g g i o = p u n t i ;
nelem++;
new
if
( ! f i r s t ) { // Se l a l i s t a vuota
f i r s t = nuovo ; // I n s e r i s
o a l l a t e s t a
nuovo>s u
= 0 ;
}
{
// Se i l p u n t e g g i o d e l l a nuova squadra maggiore d e l l a t e s t a
( p u n t i >= f i r s t >e l . p u n t e g g i o ) {
nuovo>s u
= f i r s t ; // I n s e r i s
o i n t e s t a
else
if
f i r s t = nuovo ;
p?
nuovo>s u
= p>s u
;
p>s u
= nuovo ;
;
return
142
p = p>s u ;
return
p u n t i + p ; // R e s t i t u i s e i l g i u s t o p u n t e g g i o
void C l a s s i f i
a : : Svuota ( )
while ( f i r s t ) {
PRe
tbd = f i r s t ;
f i r s t = f i r s t >s u
;
tbd ;
delete
}
nelem = 0 ;
void
stampa_menu( ) {
out << " 1 : I n s e r i s
i . \ n" ;
out << " 2 : Svuota . \ n" ;
out << " 3 : Stampa . \ n" ;
out << " 4 : Count . \ n" ;
out << " 5 : E s
i . \ n" ;
void
void
void
void
Aggiungi ( C l a s s i f i
a& l ) ;
Svuota ( C l a s s i f i
a& l ) ;
Stampa ( C l a s s i f i
a& l ) ;
Count ( C l a s s i f i
a& l ) ;
int
143
main ( )
Classifi a l ;
int s
e l t a ;
do {
stampa_menu ( ) ;
i n >> s
e l t a ;
( s
elta ) {
1:
Aggiungi ( l ) ;
;
2:
Svuota ( l ) ;
;
3:
Stampa ( l ) ;
;
4:
Count ( l ) ;
;
5:
;
:
out << " S
e l t a non v a l i d a . \ n" ;
;
}
}
( s
e l t a != 5 ) ;
swit
h
ase
break
ase
break
ase
break
ase
break
ase
break
default
break
while
return 0 ;
l) {
void
}
Svuota ( C l a s s i f i
a& l ) {
l . Svuota ( ) ;
out << " C l a s s i f i
a s v u o t a t a . \ n" ;
void
}
Stampa ( C l a s s i f i
a& l ) {
l . Stampa ( ) ;
out << e n d l ;
void
}
Count ( C l a s s i f i
a& l ) {
out << " I l numero d i e l e m e n t i e ' : " << l . Count ( ) << e n d l ;
144
Persona o n i u g e ;
typedef Persona
stru
t Re
ord ;
typedef Re
ord
stru
t Re
ord {
};
//Nome Persona
TElem e l ;
PRe
s u
;
TElem ;
PRe
;
// S i n g o l o elemento (
e l l a ) d e l l a s t r u t t u r a
lass AgenziaMatrimoniale
private :
PRe f i r s t ;
ostruttore
AgenziaMatrimoniale ( ) ;
~AgenziaMatrimoniale ( ) ;
};
AgenziaMatrimoniale : : AgenziaMatrimoniale ( ) : f i r s t ( 0 ) {
}
AgenziaMatrimoniale : : ~ AgenziaMatrimoniale ( ) {
Svuota ( ) ;
}
onst
while
if
145
return
}
}
p;
p = p>s u
;
return
bool
if
0;
return false
bool
mas hio ) {
// I n s e r i m e n t o i n t e s t a
PRe
p =
Re
ord ;
s t r
p y ( p>e l . n , n ) ;
p>e l . mas
hio = mas
hio ;
p>e l .
o n i u g e = 0 ;
p>s u
= f i r s t ;
first = p;
new
return true ;
bool
if
return false
PRe
p2 = Cer
a ( n2 ) ;
// s e i l se
ondo nome non s t a t o t r o v a t o r e s t i t u i s
e f a l s e
( ! p2 )
;
if
return false
if
return false
// s e i l s e s s o u g u a l e r e s t i t u i s
e f a l s e
( p1>e l . mas
hio == p2>e l . mas
hio )
;
if
return false
if
return false
p1>e l .
o n i u g e = &p2>e l ;
p2>e l .
o n i u g e = &p1>e l ;
}
return true ;
bool
if
bool&
oniugato )
( ! p)
return false ;
o n i u g a t o = ( p>e l . o n i u g e != 0 ) ;
return true ;
unsigned int
AgenziaMatrimoniale : : NumeroSposi ( )
onst
onst
146
unsigned int
ount = 0 ;
PRe
p = f i r s t ;
while ( p ) {
if ( p>e l .
o n i u g e
}
}
ount++;
p = p>s u
;
return
!= 0)
ount ;
PRe
tbd = f i r s t ;
f i r s t = f i r s t >s u
;
tbd ;
delete
if
( p>e l .
o n i u g e )
out << "
o n i u g e : " << p>e l .
oniuge >n << " . " ;
out << e n d l ;
p = p>s u ;
void
stampa_menu( ) {
out << " 1 : AggiungiPersona . \ n" ;
out << " 2 : Sposa . \ n" ;
out << " 3 : Coniugato . \ n" ;
out << " 4 : NumeroSposi . \ n" ;
out << " 5 : NumeroCoppie . \ n" ;
out << " 6 : Svuota . \ n" ;
out << " 7 : Stampa . \ n" ;
out << " 8 : E s
i . \ n" ;
void
void
void
void
void
void
void
AggiungiPersona ( AgenziaMatrimoniale& am ) ;
Sposa ( AgenziaMatrimoniale& am ) ;
Coniugato ( AgenziaMatrimoniale& am ) ;
NumeroSposi ( AgenziaMatrimoniale& am ) ;
NumeroCoppie( AgenziaMatrimoniale& am ) ;
Svuota ( AgenziaMatrimoniale& am ) ;
Stampa ( AgenziaMatrimoniale& am ) ;
int
147
main ( )
AgenziaMatrimoniale am;
int s
e l t a ;
do {
stampa_menu ( ) ;
i n >> s
e l t a ;
( s
elta ) {
1:
AggiungiPersona (am ) ;
;
2:
Sposa (am ) ;
;
3:
Coniugato (am ) ;
;
4:
NumeroSposi (am ) ;
;
5:
NumeroCoppie(am ) ;
;
6:
Svuota (am ) ;
;
7:
Stampa (am ) ;
;
8:
;
:
out << " S
e l t a non v a l i d a . \ n" ;
;
}
}
( s
e l t a != 8 ) ;
swit
h
ase
break
ase
break
ase
break
ase
break
ase
break
ase
break
ase
break
ase
break
default
break
while
return 0 ;
void
har
do
while
bool mas
hio = ( s e s s o == 'M' | | s e s s o
if (am. AggiungiPersona ( n , mas
hio ) )
out << " Persona a g g i u n t a . \ n" ;
else
out << " Persona non a g g i u n t a . \ n" ;
void
== 'm' ) ;
148
i n >> n1 ;
out << " I n s e r i r e se
ondo nome : " ;
i n >> n2 ;
if (am. Sposa ( n1 , n2 ) )
out << " Matrimonio
else
r e g i s t r a t o . \ n" ;
if
else
if (
o n i u g a t o )
out << n << "
else
ha o n i u g e . \ n" ;
void
}
void
}
void
}
void
}
La struttura dati pu essere realizzata
ome una lista dinami
a sempli
emente
ollegata in
ui ogni elemento rappresenta lo stato di tutti i pattini di una
data taglia. La generi
a
ella della struttura
ontiene dunque:
44
149
43
42
Numero pattini
disponibili
stru
t Re
ord ;
typedef Re
ord PRe
;
typedef stru
t Re
ord
};
Pattini pattini ;
PRe
s u
;
lass P a r
o P a t t i n i {
private :
PRe
f i r s t ;
unsigned int t o t ;
onst
operator
onst
onst ;
Par
oPattini (
P a r
o P a t t i n i &); // i n i b i s
e l a
o p i a mediatne
o s t r u t t o r e
P a r
o P a t t i n i&
=(
P a r
o P a t t i n i &); // i n i b i s
e l ' a s s e g n a z i o n e
:
Par
oPattini ( ) ;
~Par
oPattini ( ) ;
AggiungiPattini ( Taglia t ) ;
Svuota ( ) ;
NumeroTotPattini ( )
;
Fitta ( Taglia t ) ;
publi
void
void
unsigned int
bool
onst
};
unsigned int D i s p o n i b i l i t a ( T a g l i a
unsigned int NumeroPattini ( T a g l i a
bool R e s t i t u z i o n e ( T a g l i a t ) ;
void Stampa ( )
onst ;
150
t)
t)
onst ;
onst ;
P a r
o P a t t i n i : : P a r
o P a t t i n i ( ) : f i r s t ( 0 ) , t o t ( 0 ) {}
Par
oPattini ::~ Par
oPattini () {
Svuota ( ) ;
}
onst
while ( p ) {
if ( p>p a t t i n i . t a g l i a == t ) // t r o v a t o ?
return p ; // r e s t i t u i s
e i l p u n t a t o r e
else
}
}
return
0 ; // non t r o v a t o .
void
if
}
(p) {
p>p a t t i n i . t o t a l i ++;
p>p a t t i n i . d i s p o n i b i l i ++;
else
}
}
{
PRe
p =
p>p a t t i n i
p>p a t t i n i
p>p a t t i n i
p>s u
=
first = p;
new
Re
ord ;
. taglia = t ;
. t o t a li = 1;
. disp on ibili = 1;
first ;
t o t ++;
void P a r
o P a t t i n i : : Svuota ( )
while ( f i r s t ) {
}
}
PRe
tbd = f i r s t ;
f i r s t = f i r s t >s u
;
tbd ;
delete
tot = 0;
151
bool
//
i sono p a t t i n i d e l l a t a g l i a s p e
i f i
a t a , e s e s ,
e ne sono d i d i s p o n i b i l i ?
( p && ( p>p a t t i n i . d i s p o n i b i l i > 0 ) ) {
p>p a t t i n i . d i s p o n i b i l i ; // de
rementa l a d i s p o n i b i l i t
;
}
if
return true
else
return false ;
unsigned int
if ( p )
return
else
return
onst
0;
P a r
o P a t t i n i : : NumeroPattini ( T a g l i a t )
PRe
p = GetRe
ordByTaglia ( t ) ;
p>p a t t i n i . d i s p o n i b i l i ;
unsigned int
if ( p )
return
else
return
onst
p>p a t t i n i . t o t a l i ;
0;
bool
//
i sono p a t t i n i d e l l a t a g l i a s p e
i f i
a t a , e s e s ,
e ne sono d i f i t t a t i ?
( p && ( p>p a t t i n i . d i s p o n i b i l i < p>p a t t i n i . t o t a l i ) ) {
p>p a t t i n i . d i s p o n i b i l i ++;
;
}
if
return true
else
return false ;
void
P a r
o P a t t i n i : : Stampa ( )
PRe
p = f i r s t ;
onst
while
void
void
void
void
void
void
void
void
(p) {
out << " T a g l i a " << p>p a t t i n i . t a g l i a << " : " ;
out << " T o t a l e : " << p>p a t t i n i . t o t a l i << " " ;
out << " F i t t a t i : " << p>p a t t i n i . t o t a l i p>p a t t i n i . d i s p o n i b i l i << " . \ n" ;
p = p>s u
;
A g g i u n g i P a t t i n i ( P a r
o P a t t i n i& p ) ;
Svuota ( P a r
o P a t t i n i& p ) ;
NumeroTotPattini ( P a r
o P a t t i n i& p ) ;
F i t t a ( P a r
o P a t t i n i& p ) ;
D i s p o n i b i l i t a ( P a r
o P a t t i n i& p ) ;
NumeroPattini ( P a r
o P a t t i n i& p ) ;
R e s t i t u z i o n e ( P a r
o P a t t i n i& p ) ;
Stampa ( P a r
o P a t t i n i& p ) ;
void
stampa_menu( ) {
out << " \n" ;
out << " 1 : A g g i u n g i P a t t i n i . \ n" ;
out << " 2 : Svuota . \ n" ;
out << " 3 : NumeroTotPattini . \ n" ;
out << " 4 : F i t t a . \ n" ;
out << " 5 : D i s p o n i b i l i t a . \ n" ;
out << " 6 : NumeroPattini . \ n" ;
out << " 7 : R e s t i t u z i o n e . \ n" ;
out << " 8 : Stampa . \ n" ;
out << " 9 : E s
i . \ n" ;
out << " S
e l t a : " ;
int
main ( )
P a r o P a t t i n i par o ;
int s
e l t a ;
do {
stampa_menu ( ) ;
i n >> s
e l t a ;
( s
elta ) {
1:
A g g i u n g i P a t t i n i ( par
o ) ;
;
2:
Svuota ( par
o ) ;
;
3:
NumeroTotPattini ( par
o ) ;
;
4:
F i t t a ( par
o ) ;
;
5:
D i s p o n i b i l i t a ( par
o ) ;
;
6:
NumeroPattini ( par
o ) ;
;
7:
R e s t i t u z i o n e ( par
o ) ;
;
8:
Stampa ( par
o ) ;
;
9:
;
:
out << " S
e l t a non v a l i d a . \ n" ;
;
}
}
( s
e l t a != 9 ) ;
swit
h
ase
break
ase
break
ase
break
ase
break
ase
break
ase
break
ase
break
ase
break
ase
break
default
break
while
return 0 ;
void
A g g i u n g i P a t t i n i ( P a r
o P a t t i n i& p ) {
Taglia t ;
152
153
i n >> t ;
p . AggiungiPattini ( t ) ;
out << " P a t t i n i a g g i u n t i a l par
o . \ n" ;
void
}
Svuota ( P a r
o P a t t i n i& p ) {
p . Svuota ( ) ;
out << " Par
o s v u o t a t o . \ n" ;
void
}
NumeroTotPattini ( P a r
o P a t t i n i& p ) {
out << " I l par
o p a t t i n i
o n t i e n e " << p . NumeroTotPattini ( ) <<
" p a i a d i p a t t i n i i n t o t a l e . \ n" ;
void
F i t t a ( P a r
o P a t t i n i& p ) {
Taglia t ;
if
else
void
D i s p o n i b i l i t a ( P a r
o P a t t i n i& p ) {
Taglia t ;
void
NumeroPattini ( P a r
o P a t t i n i& p ) {
Taglia t ;
void
R e s t i t u z i o n e ( P a r
o P a t t i n i& p ) {
Taglia t ;
if
else
void
}
Stampa ( P a r
o P a t t i n i& p ) {
p . Stampa ( ) ;
154
SX.10 Timer
Tra
ia a pag. 42
publi
void
void
void
};
onst ;
Timer : : Timer ( ) {
reset ();
}
void
}
Timer : : s t a r t ( ) {
startTime = time ( 0 ) ;
stopTime = 0 ;
void
}
Timer : : s t o p ( ) {
stopTime = time ( 0 ) ;
void
}
Timer : : r e s e t ( ) {
startTime = 0 ;
stopTime = 0 ;
onst
if
if ( stopTime == 0) // i l timer i n
return time ( 0 ) startTime ; // s
else
return stopTime startTime ; // no
int
return
main ( )
Timer t ;
h ;
har
out
out
out
out
out
<<
<<
<<
<<
<<
"'s '
" 'x '
"'r '
" 'p '
" 'e '
s t a r t \n" ;
s t o p \n" ;
r e s e t \n" ;
show t i m e r \n" ;
e x i t \n" ;
moto?
do
155
{
i n >>
h ;
(
h ) {
's ':
t . start ();
out << " Timer s t a r t e d . \ n" ;
;
'x ' :
t . stop ( ) ;
out << " Timer stopped . \ n" ;
;
'r ':
t . reset ();
out << " Timer r e s e t . \ n" ;
;
'p ' :
out << " Timer shows : " << t . getTime ( ) << e n d l ;
;
'e ' :
;
:
out << " I n v a l i d
ommand . \ n" ;
}
}
(
h != ' e ' ) ;
swit
h
ase
break
ase
break
ase
break
ase
break
ase
break
default
while
return 0 ;
Il primo dei requisiti aggiuntivi imposti dalla tra
ia suggeris
e intuitivamente
he il timer una sorta di a
umulatore
he tiene memoria della durata
omplessiva degli intervalli di tempo
ronometrati no ad un
erto istante.
Infatti l'ese
uzione di un nuovo
onteggio fornis
e un
ontributo
he va a
sommarsi a tutti gli eventuali
ontributi pre
edenti.
Ai ni dello svolgimento di questo eser
izio, il valore
orrente del
ronometro pu essere pertanto
onsiderato
ome la
omposizione di due
ontributi:
dunque possibile pensare al timer
ome una
lasse dotata di due membri
privati:
storedTime:
156
0 se il timer inattivo.
In questo modo, all'arrivo del messaggio GETTIME, su
iente restituire
bool
publi
void
void
void
};
onst { return
( startTime != 0 ) ; } ;
onst ;
Timer : : Timer ( ) {
reset ();
}
void
if
}
void
if
}
Timer : : s t a r t ( ) {
( ! isRunning ( ) )
startTime = time ( 0 ) ;
Timer : : s t o p ( ) {
( isRunning ( ) ) {
storedTime += time ( 0 ) startTime ; // a
umula i l tempo d e l
o n t e g g i o i n
o r s o
startTime = 0 ; // ferma i l
o n t e g g i o
void
}
Timer : : r e s e t ( ) {
storedTime = 0 ;
startTime = 0 ;
onst
157
Time t = storedTime ;
if
}
return
int
( isRunning ( ) )
t += time ( 0 ) startTime ; // a g g i u n g e i l
o n t r i b u t o d e l
o n t e g g i o i n
o r s o
t;
main ( )
Timer t ;
h ;
har
out
out
out
out
out
do
<<
<<
<<
<<
<<
"'s '
" 'x '
"'r '
" 'p '
" 'e '
s t a r t \n" ;
s t o p \n" ;
r e s e t \n" ;
show t i m e r \n" ;
e x i t \n" ;
{
i n >>
h ;
(
h ) {
's ':
t . start ();
out << " Timer s t a r t e d . \ n" ;
;
'x ' :
t . stop ( ) ;
out << " Timer stopped . \ n" ;
;
'r ':
t . reset ();
out << " Timer r e s e t . \ n" ;
;
'p ' :
out << " Timer shows : " << t . getTime ( ) << e n d l ;
;
'e ' :
;
:
out << " I n v a l i d
ommand . \ n" ;
}
}
(
h != ' e ' ) ;
swit
h
ase
break
ase
break
ase
break
ase
break
ase
break
default
while
return 0 ;
SX.12 Votazioni
Tra
ia a pag. 44
//un p a r t i t o per s e m p l i i t i d e n t i f i a t o
158
// da un o d i e d i t i p o i n t e r o .
stru
t Oggetto
Codi
e i d ;
unsigned int
};
stru
t C e l l a ;
typedef C e l l a
stru
t C e l l a {
};
{
voti ;
PCella ;
Oggetto elem ;
PCella s u
;
lass V o t a z i o n i {
private :
PCella f i r s t ;
unsigned int numVoti ;
PCella C e r
a P a r t i t o ( Codi
e
publi
:
id )
onst ;
Votazioni ( ) ;
~Votazioni ( ) ;
};
V o t a z i o n i : : V o t a z i o n i ( ) : f i r s t ( 0 ) , numVoti ( 0 ) {
}
Votazioni ::~ Votazioni () {
Svuota ( ) ;
}
onst
PCella V o t a z i o n i : : C e r
a P a r t i t o ( Codi
e i d )
{
//La s t r u t t u r a g e s t i b i l e
on metodo t a b e l l a r e : i n f a t t i i l
o d i
e
// p a r t i t o r a p p r e s e n t a una
h i a v e per l a t a b e l l a d e i v o t i .
// Questo metodo r e s t i t u i s
e i l p u n t a t o r e a l l a
e l l a a v e n t e i d p a r i a q u e l l o
// s p e
i f i
a t o i n i n g r e s s o , 0 a l t r i m e n t i .
PCella p = f i r s t ;
trovato =
;
bool
false
while ( ( p ) && ! t r o v a t o )
if ( p>elem . i d == i d )
t r o v a t o = true ;
else
}
p = p>s u ;
return
p ; // s e t r o v a t o vero , p punta a l l a e l l a r i e r a t a , a l t r i m . p z e r o
unsigned int
V o t a z i o n i : : AggiungiVoto ( Codi
e i d ) {
numVoti++; // in
remento i l numero d i v o t i
o m p l e s s i v i
PCella p = C e r
a P a r t i t o ( i d ) ;
if
(p) {
p>elem . v o t i ++;
return
else {
159
p>elem . v o t i ;
new
PCella p =
Cella ;
p>elem . i d = i d ;
p>elem . v o t i = 1 ;
p>s u
= f i r s t ;
first = p;
1;
return
void V o t a z i o n i : : Svuota ( )
while ( f i r s t ) {
PCella tbd = f i r s t ;
f i r s t = f i r s t >s u
;
tbd ;
delete
}
numVoti = 0 ;
unsigned int
V o t a z i o n i : : G e t V o t i P a r t i t o ( Codi
e i d )
PCella p = C e r
a P a r t i t o ( i d ) ;
if ( p )
return
else
return
onst
p>elem . v o t i ;
0;
void
Votazioni : : GetSituazione ()
PCella p = f i r s t ;
while
onst
(p) {
out << " P a r t i t o " << p>elem . i d << " : v o t i " << " " << p>elem . v o t i ;
out << " ( " << (
) p>elem . v o t i /numVoti 100 << "%)" << e n d l ;
void
void
void
void
void
void
p = p>s u ;
AggiungiVoto ( V o t a z i o n i& v ) ;
Svuota ( V o t a z i o n i& v ) ;
G e t V o t i P a r t i t o ( V o t a z i o n i& v ) ;
GetNumeroVoti ( V o t a z i o n i& v ) ;
G e t S i t u a z i o n e ( V o t a z i o n i& v ) ;
stampa_menu( ) {
out << " 1 : Aggiungi voto . \ n" ;
out << " 2 : Svuota . \ n" ;
out << " 3 : Voti p a r t i t o . \ n" ;
out << " 4 : Numero v o t i . \ n" ;
out << " 5 : S i t u a z i o n e . \ n" ;
out << " 6 : E s
i . \ n" ;
int
float
main ( )
Votazioni v ;
160
int s
e l t a ;
do {
stampa_menu ( ) ;
i n >> s
e l t a ;
( s
elta ) {
1:
AggiungiVoto ( v ) ;
;
2:
Svuota ( v ) ;
;
3:
GetVotiPartito ( v ) ;
;
4:
GetNumeroVoti ( v ) ;
;
5:
GetSituazione (v ) ;
;
6:
;
:
out << " S
e l t a non v a l i d a . \ n" ;
;
}
}
( s
e l t a != 6 ) ;
swit
h
ase
break
ase
break
ase
break
ase
break
ase
break
ase
break
default
break
while
return 0 ;
void
AggiungiVoto ( V o t a z i o n i& v ) {
Codi
e i d ;
out << " I n d i
a r e i l p a r t i t o : " ;
i n >> i d ;
out << " Voto Aggiunto . Ora i l p a r t i t o " << i d << " ha v o t i " <<
v . AggiungiVoto ( i d ) << " . \ n" ;
void
}
Svuota ( V o t a z i o n i& v ) {
v . Svuota ( ) ;
out << " S t r u t t u r a s v u o t a t a . " << e n d l ;
void
G e t V o t i P a r t i t o ( V o t a z i o n i& v ) {
Codi
e i d ;
out << " I n d i
a r e i l p a r t i t o : " ;
i n >> i d ;
out << " I l p a r t i t o " << i d << " ha o t t e n u t o v o t i " <<
v . G e t V o t i P a r t i t o ( i d ) << " . \ n" ;
void
}
GetNumeroVoti ( V o t a z i o n i& v ) {
out << " I v o t i
o m p l e s s i v i sono : " << v . GetNumeroVoti ( ) << e n d l ;
void
}
G e t S i t u a z i o n e ( V o t a z i o n i& v ) {
v . GetSituazione ( ) ;
Appendi
e A
GNU Free Do
umentation Li
ense
Version 1.2, November 2002
2000,2001,2002 Free Software Foundation, In
.
Copyright
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to
opy and distribute verbatim
opies of this li
ense do
ument,
but
hanging it is not allowed.
Preamble
The purpose of this Li
ense is to make a manual, textbook, or other fun
tional and
useful do
ument free in the sense of freedom: to assure everyone the ee
tive freedom to
opy and redistribute it, with or without modifying it, either
ommer
ially or non
ommer
ially. Se
ondarily, this Li
ense preserves for the author and publisher a way to get
redit
for their work, while not being
onsidered responsible for modi
ations made by others.
This Li
ense is a kind of
opyleft, whi
h means that derivative works of the do
ument
must themselves be free in the same sense. It
omplements the GNU General Publi
Li
ense, whi
h is a
opyleft li
ense designed for free software.
We have designed this Li
ense in order to use it for manuals for free software, be
ause
free software needs free do
umentation: a free program should
ome with manuals providing the same freedoms that the software does. But this Li
ense is not limited to software
manuals; it
an be used for any textual work, regardless of subje
t matter or whether it
is published as a printed book. We re
ommend this Li
ense prin
ipally for works whose
purpose is instru
tion or referen
e.
161
162
a
ept the li
ense if you
opy, modify or distribute the work in a way requiring permission
under
opyright law.
A Modied Version of the Do
ument means any work
ontaining the Do
ument
or a portion of it, either
opied verbatim, or with modi
ations and/or translated into
another language.
A Se
ondary Se
tion is a named appendix or a front-matter se
tion of the Do
ument
that deals ex
lusively with the relationship of the publishers or authors of the Do
ument
to the Do
ument's overall subje
t (or to related matters) and
ontains nothing that
ould
fall dire
tly within that overall subje
t. (Thus, if the Do
ument is in part a textbook of
mathemati
s, a Se
ondary Se
tion may not explain any mathemati
s.) The relationship
ould be a matter of histori
al
onne
tion with the subje
t or with related matters, or of
legal,
ommer
ial, philosophi
al, ethi
al or politi
al position regarding them.
The Invariant Se
tions are
ertain Se
ondary Se
tions whose titles are designated,
as being those of Invariant Se
tions, in the noti
e that says that the Do
ument is released
under this Li
ense. If a se
tion does not t the above denition of Se
ondary then it is not
allowed to be designated as Invariant. The Do
ument may
ontain zero Invariant Se
tions.
If the Do
ument does not identify any Invariant Se
tions then there are none.
The Cover Texts are
ertain short passages of text that are listed, as Front-Cover
Texts or Ba
k-Cover Texts, in the noti
e that says that the Do
ument is released under
this Li
ense. A Front-Cover Text may be at most 5 words, and a Ba
k-Cover Text may
be at most 25 words.
A Transparent
opy of the Do
ument means a ma
hine-readable
opy, represented
in a format whose spe
i
ation is available to the general publi
, that is suitable for revising the do
ument straightforwardly with generi
text editors or (for images
omposed
of pixels) generi
paint programs or (for drawings) some widely available drawing editor,
and that is suitable for input to text formatters or for automati
translation to a variety
of formats suitable for input to text formatters. A
opy made in an otherwise Transparent le format whose markup, or absen
e of markup, has been arranged to thwart or
dis
ourage subsequent modi
ation by readers is not Transparent. An image format is not
Transparent if used for any substantial amount of text. A
opy that is not Transparent is
alled Opaque.
Examples of suitable formats for Transparent
opies in
lude plain ASCII without markup, Texinfo input format, LaTeX input format, SGML or XML using a publi
ly available
DTD, and standard-
onforming simple HTML, PostS
ript or PDF designed for human
modi
ation. Examples of transparent image formats in
lude PNG, XCF and JPG. Opaque formats in
lude proprietary formats that
an be read and edited only by proprietary
word pro
essors, SGML or XML for whi
h the DTD and/or pro
essing tools are not generally available, and the ma
hine-generated HTML, PostS
ript or PDF produ
ed by some
word pro
essors for output purposes only.
The Title Page means, for a printed book, the title page itself, plus su
h following
pages as are needed to hold, legibly, the material this Li
ense requires to appear in the title
page. For works in formats whi
h do not have any title page as su
h, Title Page means
the text near the most prominent appearan
e of the work's title, pre
eding the beginning
of the body of the text.
A se
tion Entitled XYZ means a named subunit of the Do
ument whose title either
is pre
isely XYZ or
ontains XYZ in parentheses following text that translates XYZ in
another language. (Here XYZ stands for a spe
i
se
tion name mentioned below, su
h as
A
knowledgements, Dedi
ations, Endorsements, or History.) To Preserve the
163
Title of su
h a se
tion when you modify the Do
ument means that it remains a se
tion
Entitled XYZ a
ording to this denition.
The Do
ument may in
lude Warranty Dis
laimers next to the noti
e whi
h states that
this Li
ense applies to the Do
ument. These Warranty Dis
laimers are
onsidered to be
in
luded by referen
e in this Li
ense, but only as regards dis
laiming warranties: any other
impli
ation that these Warranty Dis
laimers may have is void and has no ee
t on the
meaning of this Li
ense.
164
165
166
A.8 Translation
Translation is
onsidered a kind of modi
ation, so you may distribute translations of the
Do
ument under the terms of se
tion 4. Repla
ing Invariant Se
tions with translations
requires spe
ial permission from their
opyright holders, but you may in
lude translations
of some or all Invariant Se
tions in addition to the original versions of these Invariant
Se
tions. You may in
lude a translation of this Li
ense, and all the li
ense noti
es in
the Do
ument, and any Warranty Dis
laimers, provided that you also in
lude the original
English version of this Li
ense and the original versions of those noti
es and dis
laimers.
In
ase of a disagreement between the translation and the original version of this Li
ense
or a noti
e or dis
laimer, the original version will prevail.
If a se
tion in the Do
ument is Entitled A
knowledgements, Dedi
ations, or History,
the requirement (se
tion 4) to Preserve its Title (se
tion 1) will typi
ally require
hanging
the a
tual title.
A.9 Termination
You may not
opy, modify, subli
ense, or distribute the Do
ument ex
ept as expressly provided for under this Li
ense. Any other attempt to
opy, modify, subli
ense or distribute
the Do
ument is void, and will automati
ally terminate your rights under this Li
ense.
167
However, parties who have re
eived
opies, or rights, from you under this Li
ense will not
have their li
enses terminated so long as su
h parties remain in full
omplian
e.
Bibliograa
[1 Bjarne Stroustrup.
edition, 2000.
[2 Bru
e E
kel. Thinking in C++, Volume 1: Introdu
tion to Standard C++. Prenti
e
Hall, 2nd edition, 2000. Liberamente s
ari
abile da http://www.bru
ee
kel.
om.
Disponibile an
he in versione italiana edita da Apogeo.
[3 Carlo Savy.
Patterns: Elements of
168