diff --git a/tp4/lib_conjunto.c b/tp4/lib_conjunto.c new file mode 100644 index 0000000000000000000000000000000000000000..574829a0519cd747204c7c5a93bd0f82cc730b32 --- /dev/null +++ b/tp4/lib_conjunto.c @@ -0,0 +1,133 @@ +#include <stdio.h> +#include <stdio.h> +#include "lib_conjunto.h" + +/* retorna a posicao de um elemento em um conjunto. + * como um conjunto nao necessariamente eh ordenado, eh uma busca linear. + * se o elemento nao esta no conjunto, retorna -1. */ +int busca_cjt(conjunto_t *c, int elemento) { + int pos = -1; + int i; + for (i = 0; i < c->card; i++) { + if (c->v[i] == elemento) { + pos = i; + break; + } + } + return pos; +} + +conjunto_t *cria_cjt(int max) { + conjunto_t *c; + c->max = max; + c->card = 0; + c->ptr; + if ( !(c->v = malloc(sizeof(int) * max)) ) + return NULL; + return c; +} + +conjunto_t *destroi_cjt(conjunto_t *c) { + c->card = 0; + free(c->v); + c->v = NULL; + return NULL; +} + +int vazio_cjt(conjunto_t *c) { + return c->card == 0; +} + +int cardinalidade_cjt(conjunto_t *c) { + return c->card; +} + +int insere_cjt(conjunto_t *c, int elemento) { + if (c->card == c->max) + return 0; + + if (pertence_cjt(c, elemento)) + return 1; + + c->v[card] = elemento; + c->card++; + return 1; +} + +int retira_cjt(conjunto_t *c, int elemento) { + int pos = busca_cjt(c, elemento); + if (pos == -1) + return 0; + + int i; + for(i = pos; i < c->card-1; i++) { + c->v[i] = c->v[i + 1]; + } + + c->card--; + return 1; +} + +int pertence_cjt(conjunto_t *c, int elemento) { + return busca_cjt(c, elemento) != -1; +} + +int contido_cjt(conjunto_t *c1, conjunto_t *c2) { + int i; + for (i = 0; i < c1->card; i++) { + if (!(pertence_cjt(c2, c1->v[i]))) + return 0; + } + return 1; +} + +int sao_iguais_cjt(conjunto_t *c1, conjunto_t *c2) { + return c1->card == c2->card && contido_cjt(c1, c2); +} + +/* conjunto_t *diferenca_cjt(conjunto_t *c1, conjunto_t *c2) { */ + +/* } */ + +/* conjunto_t *interseccao_cjt(conjunto_t *c1, conjunto_t *c2) { */ + +/* } */ + +/* conjunto_t *uniao_cjt(conjunto_t *c1, conjunto_t *c2) { */ + +/* } */ + +conjunto_t *copia_cjt(conjunto_t *c) { + conjunto_t *copia; + + if ( !(copia = cria_cjt(c->max)) ) + return NULL; + copia->card = c->card; + + int i; + for (i = 0; i < copia->card; i++) { + copia->v[i] = c->v[i]; + } + + return copia; +} + +/* conjunto_t *cria_subcjt_cjt(conjunto_t *c, int n) { */ + +/* } */ + +/* void imprime_cjt(conjunto_t *c) { */ + +/* } */ + +/* void inicia_iterador_cjt(conjunto_t *c) { */ + +/* } */ + +/* int incrementa_iterador_cjt(conjunto_t *c, int *ret_iterador) { */ + +/* } */ + +/* int retira_um_elemento_cjt(conjunto_t *c) { */ + +/* } */ diff --git a/tp4/lib_conjunto.h b/tp4/lib_conjunto.h index bae2d7af7b107a6652cba9e4315ea88826dfc912..ccfa5aafdb25a01c61af5ebfc5f9e9879c9e00ac 100644 --- a/tp4/lib_conjunto.h +++ b/tp4/lib_conjunto.h @@ -81,7 +81,7 @@ conjunto_t *diferenca_cjt (conjunto_t *c1, conjunto_t *c2); conjunto_t *interseccao_cjt (conjunto_t *c1, conjunto_t *c2); /* - * Cria e retorna o conjunto uniao entre os conjunto c1 e c2. + * Cria e retorna o conjunto uniao entre os conjuntos c1 e c2. * Retorna NULL se a operacao falhou. */ conjunto_t *uniao_cjt (conjunto_t *c1, conjunto_t *c2); diff --git a/tp4/makefile b/tp4/makefile index 3efd261ca18b7855414cbc3cd423e28434c84f76..74de621f7c5714396294ec28fea98bc625f9e69a 100644 --- a/tp4/makefile +++ b/tp4/makefile @@ -8,16 +8,16 @@ CC = gcc # arquivos-objeto objects = testa_conjunto.o lib_conjunto.o -all: tp4.o lib_conjunto.o - $(CC) -o tp4 tp4.o lib_conjunto.o $(LDFLAGS) +all: testa_conjunto.o lib_conjunto.o + $(CC) -o testa_conjunto testa_conjunto.o lib_conjunto.o $(LDFLAGS) lib_conjunto.o: lib_conjunto.c $(CC) -c $(CFLAGS) lib_conjunto.c tp4.o: tp4.c - $(CC) -c $(CFLAGS) tp4.c + $(CC) -c $(CFLAGS) testa_conjunto.c clean: - rm -f $(objects) tp4 + rm -f $(objects) diff --git a/tp4/testa_conjunto.c b/tp4/testa_conjunto.c index 9cfeaf21416b433f12aa8ec32822bdf6f8ebfed8..0f8c883a435ef3bf768e38b235f3b8afa1e6e98e 100644 --- a/tp4/testa_conjunto.c +++ b/tp4/testa_conjunto.c @@ -6,6 +6,7 @@ */ #include <stdio.h> +#include <stdlib.h> #include "lib_conjunto.h" #define TAM 21