This repository has been archived by the owner on Mar 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrestrictionmodule.c
298 lines (278 loc) · 10.6 KB
/
restrictionmodule.c
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*-----------------------------------------------------------------------------------------------------+
| Restrictionmodules.c |Free memory module for covid19_main.c |
| |All free memory related functions can be found here. |
| | |
+------------------------------------------------------------------------------------------------------+
| Authors: Joao Barreiros C. Rodrigues nº99968, Henrique Ramos Delfino nº99957 |
| MEEC-IST |
| Date: 19 May 2021 |
+-----------------------------------------------------------------------------------------------------*/
#include "covid19.h"
/*Function Name: restricting
Input:a pointer for the header of the list and a pointer for the argument string of -P
Output:void function
Date Created: 19 May 2021
Last Revised: 23 May 2021
Definition:
*/
country_list *restricting(country_list *heade, char *string)
{
country_list *aux = NULL, *aux2 = NULL;
week_list *auxweek = NULL, *auxweek2 = NULL;
aux = heade;
aux2 = aux->next;
long min_valor = 0, max_valor = 0, ano = 0, mes = 0, ano2 = 0, mes2 = 0;
int variaveis_lidas = -1;
variaveis_lidas = sscanf(string, "min %ld", &min_valor);
// verify if the string on the command line was min n,
if (variaveis_lidas == 1)
{
min_valor = min_valor*1000;
//while the population of the header is smaller than n*1000 the country will be deleted
while(heade->population <= min_valor)
{
aux = aux->next;
aux2 = heade;
free_node(aux2);
heade = aux;
}
//while cicle to verify if all countries got a population bigger than n*1000
while (aux->next != NULL)
{
//if the population is bigger than n*1000 the cicle will verify the next country, else the country will be deleted
if (aux->next->population > min_valor)
{
aux = aux->next;
}
else
{
aux2 = aux->next;
aux->next = aux->next->next;
free_node(aux2);
}
}
//if the last country got a population <= than n*1000 the country will be deleted
if (aux->population <= min_valor)
{
free_node(aux);
}
}
variaveis_lidas = sscanf(string, "max %ld", &max_valor);
//in case if the flag is "max n"
if (variaveis_lidas == 1)
{
max_valor = max_valor*1000;
//while the population of the header is bigger than n*1000 the country will be deleted
while(heade->population >= max_valor)
{
aux = aux->next;
aux2 = heade;
free_node(aux2);
heade = aux;
}
//while cicle to verify if all countries got a population smaller than n*1000
while (aux->next != NULL)
{
//if the population is smaller than n*1000 the cicle will verify the next country, else the country will be deleted
if (aux->next->population < max_valor)
{
aux = aux->next;
}
else
{
aux2 = aux->next;
aux->next = aux->next->next;
free_node(aux2);
}
}
//if the last country got a population >= than n*1000 the country will be deleted
if (aux->population >= max_valor)
{
free_node(aux);
}
}
variaveis_lidas = sscanf(string, "date %ld-%ld", &ano, &mes);
//in case of the respective flag is "date yyyy-ww"
if (variaveis_lidas == 2)
{
country_list *beforecountry = NULL;
aux = heade;
auxweek = aux->week_pointer;
//condition for the head
while(aux != NULL)
{
//verify all weeks of the respective country
while (aux->week_pointer != NULL)
{ //if the week is the same of the week we declared on the comand line, that week will be the unique week of the respective country
if (aux->week_pointer->year == ano && aux->week_pointer->week == mes)
{
auxweek = aux->week_pointer;
auxweek = auxweek->next;
aux->week_pointer->next = NULL;
//delete all the weeks that we dont want since we found the week we want with the if cause of line 108
while(auxweek != NULL)
{
auxweek2 = auxweek;
auxweek = auxweek->next;
auxweek2->next = NULL;
free(auxweek2);
}
break;
}
auxweek = aux->week_pointer;
aux->week_pointer = aux->week_pointer->next;
auxweek->next = NULL;
free(auxweek);
}
//if the head has no week the head will be deleted else the auxiliar pointers for the countries will advance on the list
if (heade->week_pointer == NULL)
{
aux2 = heade;
heade = heade->next;
aux = heade;
beforecountry = heade;
free(aux2);
}
else
{
aux = heade->next;
beforecountry = heade;
break;
}
}
//condition for the next country
while(aux != NULL)
{
//verify if exist any week for the country we are pointing with aux
while (aux->week_pointer != NULL)
{
//if the week is the same we declared on the command line we will delete all weeks forward on the list
if (aux->week_pointer->year == ano && aux->week_pointer->week == mes)
{
auxweek = aux->week_pointer;
auxweek = auxweek->next;
aux->week_pointer->next = NULL;
while(auxweek != NULL)
{
auxweek2 = auxweek;
auxweek = auxweek->next;
auxweek2->next = NULL;
free(auxweek2);
}
break;
}
auxweek = aux->week_pointer;
aux->week_pointer = aux->week_pointer->next;
auxweek->next = NULL;
free(auxweek);
}
//if theres no week for the respective country the country will be deleted
if (aux->week_pointer == NULL)
{
aux2 = aux;
aux = aux->next;
beforecountry->next = aux; //
aux2->next = NULL;
free(aux2);
}
else
{
beforecountry = aux;
aux = aux->next;
}
}
}
//in case of the respective flag is "dates yyyy-ww yyyy-ww"
variaveis_lidas = sscanf(string, "dates %ld-%ld %ld-%ld\n", &ano, &mes, &ano2, &mes2);
if (variaveis_lidas == 4)
{
long maxvalue = 0, minvalue = 0;
aux = heade;
//all verify what date is recent
if(ano < ano2)
{
maxvalue = ano2*100 + mes2;
minvalue = ano*100 + mes;
}
else if (ano > ano2)
{
maxvalue = ano*100 + mes;
minvalue = ano2*100 + mes2;
}
else if(mes < mes2)
{
maxvalue = ano2*100 + mes2;
minvalue = ano*100 + mes;
}
else if(mes > mes2)
{
maxvalue = ano*100 + mes;
minvalue = ano2*100 + mes2;
}
week_list *beforeweek = NULL; //creation of an auxiliar pointer for weeks
// while cicle to verify if all the countries respect the restriction
while( aux != NULL)
{
auxweek = aux->week_pointer;
//while cycle to verify which weeks are between the weeks we pretend
while (auxweek != NULL)
{
long totalvalueweek = 0, totalvalueaux = 0;
totalvalueaux = aux->week_pointer->week + aux->week_pointer->year*100;
totalvalueweek = auxweek->week + auxweek->year*100;
//verify if the week we are reading is between the weeks we pretend
if ( minvalue > totalvalueaux || maxvalue < totalvalueaux)
{
auxweek2 = aux->week_pointer;
aux->week_pointer = aux->week_pointer->next;
auxweek = aux->week_pointer;
auxweek2->next = NULL;
free(auxweek2);
}
else if(minvalue > totalvalueweek || maxvalue < totalvalueweek)
{
beforeweek->next = auxweek->next;
auxweek2 = auxweek;
auxweek = auxweek->next;
auxweek2->next = NULL;
free(auxweek2);
}
else
{
beforeweek = auxweek;
auxweek = auxweek->next;
}
}
aux = aux->next;
}
aux = heade;
country_list *beforecountry = NULL;
//while cycle to verify if exist at least 1 week for the respective country, if not the country will be deleted
while(aux != NULL)
{
if (heade->week_pointer == NULL)
{
aux2 = heade;
heade = heade->next;
aux = heade;
if(aux !=NULL)
aux->next = NULL;
free(aux2);
}
else if(aux->week_pointer == NULL)
{
aux2 = aux;
aux = aux->next;
beforecountry->next = aux;
aux2->next = NULL;
free(aux2);
}
else
{
beforecountry = aux;
aux = aux->next;
}
}
}
return heade;
}