Skip to content

Commit 342cf2c

Browse files
committed
commit
1 parent cb19f8a commit 342cf2c

File tree

5 files changed

+201
-64
lines changed

5 files changed

+201
-64
lines changed

Mt_1/MT1_17_18/ex1/ex1

56 Bytes
Binary file not shown.

Mt_1/MT1_17_18/ex1/ex1.c

+134-51
Original file line numberDiff line numberDiff line change
@@ -4,108 +4,188 @@
44
#include "lista.h"
55
#include "pilha.h"
66

7-
87
/****************************************************/
98
/* Funcoes a implementar */
109
/****************************************************/
1110

1211
/*** problema 1.1 ***/
13-
lista* filtra_titulos(lista *lista1, lista *lista2)
12+
lista *filtra_titulos(lista *lista1, lista *lista2)
1413
{
15-
/* complexidade do algoritmo: ______________ */
16-
17-
return NULL;
14+
if (lista1 == NULL || lista2 == NULL)
15+
{
16+
return NULL;
17+
}
18+
lista *lista_n = lista_nova();
19+
if (lista_n == NULL)
20+
{
21+
return NULL;
22+
}
23+
l_elemento *aponta = lista1->inicio;
24+
//l_elemento *aponta2= lista2->inicio;
25+
while (aponta != NULL)
26+
{
27+
if (lista_pesquisa(lista2, aponta->str, LISTA_INICIO) != NULL)
28+
{
29+
aponta = aponta->proximo;
30+
}
31+
else
32+
{
33+
lista_insere(lista_n, aponta->str, NULL);
34+
aponta = aponta->proximo;
35+
}
36+
}
37+
if (lista_tamanho(lista_n) > lista_tamanho(lista1))
38+
{
39+
return NULL;
40+
}
1841

42+
return lista_n;
1943
}
2044

2145
/*** problema 1.2 ***/
2246
int retira_comecados_por(lista *lst, char *inicio)
2347
{
24-
return 0;
48+
int retirados = 0;
49+
if (lst == NULL || inicio == NULL)
50+
{
51+
return 0;
52+
}
53+
l_elemento *aponta = lst->inicio;
54+
char string[100];
55+
56+
while (aponta != NULL)
57+
{
58+
if (strstr(aponta->str, inicio) != NULL)
59+
{
60+
61+
aponta = lista_remove(lst, aponta);
62+
retirados++;
63+
}
64+
else
65+
aponta = aponta->proximo;
66+
}
67+
return retirados;
2568
}
2669

2770
/*** problema 1.3 ***/
71+
// sempre que faco pop tenho que ver se o tilulo é igual
2872
int insere_na_pilha(pilha *p, char *titulo)
2973
{
30-
return 0;
74+
char string[120];
75+
pilha *po = pilha_nova();
76+
if (p == NULL || titulo == NULL || po == NULL)
77+
{
78+
return 0;
79+
}
80+
81+
char *aux;
82+
while (strcmp(titulo, pilha_top(p)) <= 0)
83+
{
84+
if (pilha_vazia(p))
85+
{
86+
break;
87+
}
88+
89+
if (!strcmp(titulo, pilha_top(p)))
90+
{
91+
return 0;
92+
}
93+
if (strcmp(titulo, pilha_top(p)) < 0)
94+
{
95+
aux = pilha_top(p);
96+
pilha_pop(p);
97+
pilha_push(po, aux);
98+
}
99+
}
100+
pilha_push(p, titulo);
101+
102+
for (int i = 0; i < pilha_tamanho(po); i++)
103+
{
104+
pilha_push(p, pilha_top(po));
105+
pilha_pop(po);
106+
}
107+
pilha_apaga(po);
108+
return 1;
31109
}
32110

33111
/****************************************************/
34112
/* Funcoes ja implementadas (nao modificar) */
35113
/****************************************************/
36114

37-
lista* lerParaLista(FILE* ficheiro)
115+
lista *lerParaLista(FILE *ficheiro)
38116
{
39117
char buffer[256], *nlptr;
40-
lista* l;
118+
lista *l;
41119

42120
if (ficheiro == NULL)
43121
return NULL;
44122

45123
l = lista_nova();
46124

47-
while(fgets(buffer, 255, ficheiro) != NULL)
125+
while (fgets(buffer, 255, ficheiro) != NULL)
48126
{
49127
nlptr = strchr(buffer, '\n');
50128
if (nlptr)
51-
*nlptr = '\0';
52-
lista_insere(l,buffer,NULL);
129+
*nlptr = '\0';
130+
lista_insere(l, buffer, NULL);
53131
}
54132

55133
return l;
56134
}
57135

58-
pilha* lerParaPilha(FILE* ficheiro)
136+
pilha *lerParaPilha(FILE *ficheiro)
59137
{
60138
char buffer[256], *nlptr;
61-
pilha* p;
139+
pilha *p;
62140

63141
if (ficheiro == NULL)
64142
return NULL;
65143

66144
p = pilha_nova();
67145

68-
while(fgets(buffer, 255, ficheiro) != NULL)
146+
while (fgets(buffer, 255, ficheiro) != NULL)
69147
{
70148
nlptr = strchr(buffer, '\n');
71149
if (nlptr)
72-
*nlptr = '\0';
73-
pilha_push(p,buffer);
150+
*nlptr = '\0';
151+
pilha_push(p, buffer);
74152
}
75153
return p;
76154
}
77155

78-
void imprimeLista(lista *l, unsigned int n) {
79-
if (l->tamanho<n)
80-
printf("ERRO... Lista possui menos de %d elementos\n",n);
156+
void imprimeLista(lista *l, unsigned int n)
157+
{
158+
if (l->tamanho < n)
159+
printf("ERRO... Lista possui menos de %d elementos\n", n);
81160
unsigned int i;
82161
l_elemento *aux = l->inicio;
83-
for (i=0; i<n; i++) {
84-
printf("%dº Livro: %s\n", i+1,aux->str);
162+
for (i = 0; i < n; i++)
163+
{
164+
printf("%dº Livro: %s\n", i + 1, aux->str);
85165
aux = aux->proximo;
86166
}
87167
}
88168

89169
int main()
90170
{
91171
FILE *f;
92-
lista *l1=NULL, *l2=NULL, *l=NULL;
172+
lista *l1 = NULL, *l2 = NULL, *l = NULL;
93173
pilha *p;
94174
int nmov, res;
95175
char *tit1;
96176

97177
/* testes */
98-
f = fopen("livros1.txt","r");
99-
if(f == NULL)
178+
f = fopen("livros1.txt", "r");
179+
if (f == NULL)
100180
{
101181
printf("Erro ao ler ficheiro de entrada.\n");
102182
return 1;
103183
}
104184
l1 = lerParaLista(f);
105185
fclose(f);
106186

107-
f = fopen("livros2.txt","r");
108-
if(f == NULL)
187+
f = fopen("livros2.txt", "r");
188+
if (f == NULL)
109189
{
110190
printf("Erro ao ler ficheiro de entrada.\n");
111191
return 1;
@@ -115,21 +195,21 @@ int main()
115195

116196
/* inicio teste prob1.1 */
117197
l = filtra_titulos(l1, l2);
118-
if(l) {
198+
if (l)
199+
{
119200
printf("\nLista resultante contem %d livros.\n", lista_tamanho(l));
120-
if(lista_tamanho(l) != 12)
201+
if (lista_tamanho(l) != 12)
121202
printf("ERRO.. Tamanho da lista incorreto(tamanho: %d; esperado: 12)\n", lista_tamanho(l));
122203
else
123-
imprimeLista(l,lista_tamanho(l));
204+
imprimeLista(l, lista_tamanho(l));
124205
}
125206
else
126207
printf("\nERRO.. filtra_titulos retornou NULL\n");
127208
/* fim teste prob1.1 */
128209

129-
130210
/* inicio teste prob1.2 */
131-
f = fopen("livros.txt","r");
132-
if(f == NULL)
211+
f = fopen("livros.txt", "r");
212+
if (f == NULL)
133213
{
134214
printf("Erro ao ler ficheiro de entrada.\n");
135215
return 1;
@@ -141,52 +221,55 @@ int main()
141221
nmov = retira_comecados_por(l, "A");
142222
printf("\nForam retirados %d livros\n", nmov);
143223
printf("A lista contém %d livros\n", lista_tamanho(l));
144-
if (nmov!=146)
145-
printf("ERRO.. Numero de livros removidos incorreto (removidos: %d; esperado: 146\n",nmov);
146-
else if(lista_tamanho(l) != 389)
224+
if (nmov != 146)
225+
printf("ERRO.. Numero de livros removidos incorreto (removidos: %d; esperado: 146\n", nmov);
226+
else if (lista_tamanho(l) != 389)
147227
printf("ERRO.. Lista incorreta (tamanho: %d; esperado: 389)\n", lista_tamanho(l));
148228
else
149-
printf("9º livro da lista: %s\n", lista_elemento(l,8)->str);
229+
printf("9º livro da lista: %s\n", lista_elemento(l, 8)->str);
150230
/* fim teste prob1.2 */
151231

152-
153232
/* inicio teste prob1.3 */
154-
f = fopen("livros2.txt","r");
155-
if(f == NULL)
233+
f = fopen("livros2.txt", "r");
234+
if (f == NULL)
156235
{
157236
printf("Erro ao ler ficheiro de entrada.\n");
158237
return 1;
159238
}
160239
p = lerParaPilha(f);
161240
fclose(f);
162241

163-
tit1="Fora de Horas";
242+
tit1 = "Fora de Horas";
164243
res = insere_na_pilha(p, tit1);
165244

166-
if (res==0) {
245+
if (res == 0)
246+
{
167247
printf("\n'%s' já existe na pilha\n", tit1);
168248
printf("Numero de elementos na pilha: %d\n", pilha_tamanho(p));
169249
}
170250
else if (pilha_tamanho(p) != 32)
171-
printf("\nERRO.. Numero de elementos na pilha incorreto (existentes: %d; esperado: 32)\n",pilha_tamanho(p));
172-
else {
251+
printf("\nERRO.. Numero de elementos na pilha incorreto (existentes: %d; esperado: 32)\n", pilha_tamanho(p));
252+
else
253+
{
173254
printf("\n'%s' foi inserido na pilha\n", tit1);
174255
printf("Numero de elementos na pilha: %d\n", pilha_tamanho(p));
175256
}
176257

177-
tit1="Mundo incompleto";
258+
tit1 = "Mundo incompleto";
178259
res = insere_na_pilha(p, tit1);
179260

180-
if (res==0) {
261+
if (res == 0)
262+
{
181263
printf("'%s' já existe na pilha\n", tit1);
182264
printf("Numero de elementos na pilha: %d\n", pilha_tamanho(p));
183265
}
184266
else if (pilha_tamanho(p) != 32)
185-
printf("ERRO.. Numero de elementos na pilha incorreto (existentes: %d; esperado: 32)\n",pilha_tamanho(p));
186-
else {
187-
printf("'%s' foi inserido na pilha\n", tit1);
188-
printf("Numero de elementos na pilha: %d\n", pilha_tamanho(p));
189-
}
267+
printf("ERRO.. Numero de elementos na pilha incorreto (existentes: %d; esperado: 32)\n", pilha_tamanho(p));
268+
else
269+
{
270+
printf("'%s' foi inserido na pilha\n", tit1);
271+
printf("Numero de elementos na pilha: %d\n", pilha_tamanho(p));
272+
}
190273
puts("");
191274
/* fim teste prob1.3 */
192275

Mt_1/MT1_17_18/ex2/ex2

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)