Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "vetor.h"
- #include "lista.h"
- /****************************************************/
- /* Funcoes a implementar */
- /****************************************************/
- void retira_duplicados(vetor* vec)
- {
- int i , j ;
- for (i = 1 ; i < (*vec).tamanho ; i++)
- for (j = i - 1 ; j >= 0 ; j--)
- if (!strcmp ((*vec).elementos[j] , (*vec).elementos[i]))
- vetor_remove(vec , i--) ;
- }
- vetor* interseta(lista* l1, lista* l2)
- {
- vetor *resultado = vetor_novo() ;
- listaItem *p1 = l1->raiz , *p2 = l2->raiz ;
- while (p1)
- {
- while (p2)
- {
- if (!strcmp (p1->elemento , p2->elemento)) vetor_insere(resultado , p1->elemento , -1) ;
- p2 = p2->proximo ;
- }
- p1 = p1->proximo ;
- p2 = l2->raiz ;
- }
- return resultado ;
- }
- /****************************************************/
- /* Funcoes ja implementadas (nao modificar) */
- /****************************************************/
- vetor* lerAtores(FILE* ficheiro)
- {
- char buffer[256], *nlptr;
- vetor* actores;
- if (ficheiro == NULL)
- return NULL;
- actores = vetor_novo();
- while(!feof(ficheiro))
- {
- fgets(buffer, 255, ficheiro);
- nlptr = strchr(buffer, '\n');
- if (nlptr)
- *nlptr = '\0';
- vetor_insere(actores,buffer,-1);
- }
- return actores;
- }
- lista* lerFilmes(FILE* ficheiro)
- {
- char buffer[256], *nlptr;
- lista* filmes;
- if (ficheiro == NULL)
- return NULL;
- filmes = lista_nova();
- while(!feof(ficheiro))
- {
- fgets(buffer, 255, ficheiro);
- nlptr = strchr(buffer, '\n');
- if (nlptr)
- *nlptr = '\0';
- lista_insere(filmes,buffer,-1);
- }
- return filmes;
- }
- int main()
- {
- FILE *fa, *fa2, *ff, *ff2;
- vetor *atores , *atores2, *resultado;
- lista *filmes, *filmes2;
- /* testes */
- fa = fopen("actores.txt","r");
- fa2 = fopen("actores2.txt","r");
- ff = fopen("filmes.txt","r");
- ff2 = fopen("filmes2.txt","r");
- atores = lerAtores(fa);
- atores2 = lerAtores(fa2);
- filmes = lerFilmes(ff);
- filmes2 = lerFilmes(ff2);
- /* testa retira_duplicados */
- printf("Total inicial: %d\n", atores2->tamanho);
- retira_duplicados(atores2);
- printf("Unicos: %d (esperado: 30)\n\n", atores2->tamanho);
- /* testa interseta */
- resultado = interseta(filmes,filmes2);
- if(resultado)
- printf("Intersecao: %d (esperado: 75)\n",resultado->tamanho);
- else
- printf("Intersecao: vetor NULL\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment