-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoluzioneEsamePEL.cpp
213 lines (176 loc) · 4.64 KB
/
SoluzioneEsamePEL.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include<iostream>
#include<string>
using namespace std;
class Dict{
public:
Dict();
~Dict();
int& at(string pos);
void print() const;
bool operator<=(const Dict& d) const;
bool operator==(const Dict& d) const;
Dict& operator+=(const Dict& d);
private:
struct Cella{
string key;
int val;
Cella* next;
};
typedef Cella* pCella;
pCella head;
Cella* append(string str, int val);
bool presente(pCella lui) const;
bool chiave_presente(pCella lui) const;
};
//Costruttore di default. Non cambiare
Dict::Dict() {
head = nullptr;
}
//Distruttore: Non cambiare
Dict::~Dict() {
pCella pc;
while (head) {
pc = head;
head = head->next;
delete pc;
}
}
//Stampa gli elementi. Non cambare
void Dict:: print() const {
cout<<"OUTPUT"<<endl;
pCella pc = head;
while (pc) {
cout<<pc->key<<":"<<pc->val<<endl;
pc = pc->next;
}
cout<<"---"<<endl;
}
Dict::Cella* Dict::append(string str, int val){
pCella nuova = new Cella;
if (head == nullptr) {
head = nuova;
head->val = val;
head->key = str;
head->next = nullptr;
return head;
}
pCella aux = head;
while (aux->next) aux = aux->next;
aux->next = nuova;
nuova->next = nullptr;
nuova->key = str;
nuova->val = val;
return nuova;
}
//Esercizio 1
//Implementare: il metodo at ritorna una reference al
//campo value del nodo con la key uguale a pos se questa esiste
//altrimenti crea un nuovo nodo in coda alla lista che contiene
//come key pos e come valore 0, e ritorna la reference al campo
//value di questo nuovo nodo
int& Dict::at(string pos) {
if (head == nullptr){
append(pos, 0);
return head->val;
}
bool trovata = false;
pCella aux = head;
while(!trovata && aux){
if (aux->key != pos) aux=aux->next;
else trovata = true;
}
if (trovata) return aux->val;
aux = append(pos, 0);
return aux->val;
}
bool Dict::presente(Dict::pCella lui) const {
//controllo se una cella è presente in me
bool trovata = false;
pCella aux = head;
while(!trovata && aux){
if (aux->key == lui->key && aux->val == lui->val) trovata = true;
else aux=aux->next;
}
return trovata;
}
//Esercizio 2
//Ritorna true se *this ha un sottoinsieme degli elementi
//di d, cioe' tutte le coppie key,value di *this sono anche
//in Dict (anche in ordine diverso)
bool Dict::operator<=(const Dict& d) const {
pCella aux = head;
if (aux == nullptr) return true;
if (head == nullptr && aux != nullptr) return false;
bool contenuti = true;
while (aux && contenuti){
if (!d.presente(aux)) contenuti = false;
aux = aux->next;
}
return contenuti;
}
//Esercizio 2
//Ritorna true se *this ha gli stessi elementi
//di d anche in ordine diverso
bool Dict::operator==(const Dict& d) const {
if (head == nullptr && d.head == nullptr) return true;
if (head == nullptr && d.head != nullptr) return false;
if (d.head == nullptr && head != nullptr) return false;
bool contenuti = true;
//prima controllo se d è contenuto in this
pCella aux = d.head;
while (aux && contenuti){
if (!presente(aux)) contenuti = false;
aux = aux->next;
}
if (contenuti == false) return false;
//ora faccio il contrario
aux = head;
while (aux && contenuti){
if (!d.presente(aux)) contenuti = false;
aux = aux->next;
}
return contenuti;
}
//Esercizio 3
//Aggiunge tutti gli elementi del parametro formale la cui chiave
//non e' gia' presente in *this.
//In altre parole, per ogni elemento di d, se la sua key e' gia'
//presente in *this, allora viene ignorato, se invece non e'
//presente, va aggiunto in coda. Al termine si ritorna *this
bool Dict::chiave_presente(Dict::pCella lui) const{
//controllo se una chiave è presente in me
bool trovata = false;
pCella aux = head;
while(!trovata && aux){
if (aux->key == lui->key) trovata = true;
else aux=aux->next;
}
return trovata;
}
Dict& Dict::operator+=(const Dict& d) {
pCella aux = d.head;
if (aux == nullptr) return *this;
if (head == nullptr) {
while(aux){
append(aux->key, aux->val);
aux = aux->next;
}
return *this;
}
while (aux){
if (!chiave_presente(aux)) append(aux->key, aux->val);
aux = aux->next;
}
return *this;
}
void leggi(Dict& s) {
int nelem;
string k;
int v;
cin>>nelem;
for (int i=0; i<nelem; i++) {
cin>>k;
cin>>v;
s.at(k)=v;
}
}