From 3150eee6e7453ce19f926d1df3f3e088d44c169e Mon Sep 17 00:00:00 2001
From: Pedro Folloni Pesserl <fpesserl7@gmail.com>
Date: Thu, 24 Nov 2022 00:54:29 -0300
Subject: [PATCH] start lib_conjunto.c

---
 tp4/lib_conjunto.c   | 133 +++++++++++++++++++++++++++++++++++++++++++
 tp4/lib_conjunto.h   |   2 +-
 tp4/makefile         |   8 +--
 tp4/testa_conjunto.c |   1 +
 4 files changed, 139 insertions(+), 5 deletions(-)
 create mode 100644 tp4/lib_conjunto.c

diff --git a/tp4/lib_conjunto.c b/tp4/lib_conjunto.c
new file mode 100644
index 0000000..574829a
--- /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 bae2d7a..ccfa5aa 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 3efd261..74de621 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 9cfeaf2..0f8c883 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
 
-- 
GitLab